Browse Source

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 11 months ago
committed by Manos Pitsidianakis
parent
commit
f7cbd9a64d
Signed by: epilys GPG Key ID: 73627C2F690DF710
  1. 14
      melib/src/datetime.rs

14
melib/src/datetime.rs

@ -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;
}

Loading…
Cancel
Save