From f7cbd9a64da0efd0908d175bc17055752d1aec09 Mon Sep 17 00:00:00 2001 From: Zisu Andrei Date: Fri, 1 Jan 2021 12:48:14 +0000 Subject: [PATCH] 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. --- melib/src/datetime.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/melib/src/datetime.rs b/melib/src/datetime.rs index dd2e7e83..b66403d5 100644 --- a/melib/src/datetime.rs +++ b/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; }