melib/datetime: Set C locale for strptime parsing

This is the locale that should be used for computer interoperability
when doing date and time parsing and formatting.

Without this, on systems which don't have the US locale set, the parsing
returns 0.
jmap-eventsource
Zisu Andrei 2021-01-01 12:48:14 +00:00 committed by Manos Pitsidianakis
parent 829f1243fb
commit f7cbd9a64d
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 13 additions and 1 deletions

View File

@ -43,7 +43,7 @@ use std::ffi::{CStr, CString};
pub type UnixTimestamp = u64;
use libc::{timeval, timezone};
use libc::{timeval, timezone, uselocale};
extern "C" {
fn strptime(
@ -214,7 +214,19 @@ where
] {
unsafe {
let fmt = CStr::from_bytes_with_nul_unchecked(fmt);
let locale = ::libc::newlocale(
::libc::LC_TIME,
b"C\0".as_ptr() as *const i8,
std::ptr::null_mut(),
);
let old_locale = uselocale(locale);
let ret = strptime(s.as_ptr(), fmt.as_ptr(), &mut new_tm as *mut _);
uselocale(old_locale);
::libc::freelocale(locale);
if ret.is_null() {
continue;
}