melib/datetime: add posix locale arg in timestamp_to_string()

lazy_fetch
Manos Pitsidianakis 2021-01-05 20:44:57 +02:00
parent 50cd81772f
commit 3dba6fdf60
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
8 changed files with 39 additions and 19 deletions

View File

@ -192,7 +192,7 @@ impl Card {
self.key.as_str() self.key.as_str()
} }
pub fn last_edited(&self) -> String { pub fn last_edited(&self) -> String {
datetime::timestamp_to_string(self.last_edited, None) datetime::timestamp_to_string(self.last_edited, None, false)
} }
pub fn set_id(&mut self, new_val: CardId) { pub fn set_id(&mut self, new_val: CardId) {

View File

@ -103,7 +103,7 @@ impl Locale {
} }
} }
pub fn timestamp_to_string(timestamp: UnixTimestamp, fmt: Option<&str>) -> String { pub fn timestamp_to_string(timestamp: UnixTimestamp, fmt: Option<&str>, posix: bool) -> String {
let mut new_tm: ::libc::tm = unsafe { std::mem::zeroed() }; let mut new_tm: ::libc::tm = unsafe { std::mem::zeroed() };
unsafe { unsafe {
let i: i64 = timestamp.try_into().unwrap_or(0); let i: i64 = timestamp.try_into().unwrap_or(0);
@ -119,13 +119,29 @@ pub fn timestamp_to_string(timestamp: UnixTimestamp, fmt: Option<&str>) -> Strin
unsafe { CStr::from_bytes_with_nul_unchecked(b"%a, %d %b %Y %T %z\0") } unsafe { CStr::from_bytes_with_nul_unchecked(b"%a, %d %b %Y %T %z\0") }
}; };
let mut vec: [u8; 256] = [0; 256]; let mut vec: [u8; 256] = [0; 256];
let ret = unsafe { let ret = {
strftime( let _with_locale: Option<Result<Locale>> = if posix {
vec.as_mut_ptr() as *mut _, Some(
256, Locale::new(
format.as_ptr(), ::libc::LC_TIME,
&new_tm as *const _, b"C\0".as_ptr() as *const i8,
) std::ptr::null_mut(),
)
.chain_err_summary(|| "Could not set locale for datetime conversion")
.chain_err_kind(crate::error::ErrorKind::External),
)
} else {
None
};
unsafe {
strftime(
vec.as_mut_ptr() as *mut _,
256,
format.as_ptr(),
&new_tm as *const _,
)
}
}; };
String::from_utf8_lossy(&vec[0..ret]).into_owned() String::from_utf8_lossy(&vec[0..ret]).into_owned()
@ -402,7 +418,7 @@ pub fn now() -> UnixTimestamp {
#[test] #[test]
fn test_timestamp() { fn test_timestamp() {
timestamp_to_string(0, None); timestamp_to_string(0, None, false);
} }
#[test] #[test]

View File

@ -53,7 +53,7 @@ impl Default for Draft {
let mut headers = HeaderMap::default(); let mut headers = HeaderMap::default();
headers.insert( headers.insert(
HeaderName::new_unchecked("Date"), HeaderName::new_unchecked("Date"),
crate::datetime::timestamp_to_string(crate::datetime::now(), None), crate::datetime::timestamp_to_string(crate::datetime::now(), None, true),
); );
headers.insert(HeaderName::new_unchecked("From"), "".into()); headers.insert(HeaderName::new_unchecked("From"), "".into());
headers.insert(HeaderName::new_unchecked("To"), "".into()); headers.insert(HeaderName::new_unchecked("To"), "".into());

View File

@ -41,7 +41,11 @@ pub mod dbg {
() => { () => {
eprint!( eprint!(
"[{}][{:?}] {}:{}_{}: ", "[{}][{:?}] {}:{}_{}: ",
crate::datetime::timestamp_to_string(crate::datetime::now(), Some("%Y-%m-%d %T")), crate::datetime::timestamp_to_string(
crate::datetime::now(),
Some("%Y-%m-%d %T"),
false
),
std::thread::current() std::thread::current()
.name() .name()
.map(std::string::ToString::to_string) .map(std::string::ToString::to_string)

View File

@ -85,7 +85,8 @@ pub fn log<S: AsRef<str>>(val: S, level: LoggingLevel) {
if level <= b.level { if level <= b.level {
b.dest b.dest
.write_all( .write_all(
crate::datetime::timestamp_to_string(crate::datetime::now(), None).as_bytes(), crate::datetime::timestamp_to_string(crate::datetime::now(), None, false)
.as_bytes(),
) )
.unwrap(); .unwrap();
b.dest.write_all(b" [").unwrap(); b.dest.write_all(b" [").unwrap();

View File

@ -1002,6 +1002,7 @@ impl ConversationsListing {
.as_ref() .as_ref()
.map(String::as_str) .map(String::as_str)
.or(Some("%Y-%m-%d %T")), .or(Some("%Y-%m-%d %T")),
false,
), ),
} }
} }

View File

@ -1010,7 +1010,7 @@ impl PlainListing {
n if n < 4 * 24 * 60 * 60 => { n if n < 4 * 24 * 60 * 60 => {
format!("{} days ago{}", n / (24 * 60 * 60), " ".repeat(9)) format!("{} days ago{}", n / (24 * 60 * 60), " ".repeat(9))
} }
_ => melib::datetime::timestamp_to_string(envelope.datetime(), None), _ => melib::datetime::timestamp_to_string(envelope.datetime(), None, false),
} }
} }

View File

@ -683,11 +683,9 @@ impl State {
} }
} }
let ((x, mut y), box_displ_area_bottom_right) = box_displ_area; let ((x, mut y), box_displ_area_bottom_right) = box_displ_area;
for line in msg_lines for line in msg_lines.into_iter().chain(Some(String::new())).chain(Some(
.into_iter() melib::datetime::timestamp_to_string(*timestamp, None, false),
.chain(Some(String::new())) )) {
.chain(Some(melib::datetime::timestamp_to_string(*timestamp, None)))
{
write_string_to_grid( write_string_to_grid(
&line, &line,
&mut self.overlay_grid, &mut self.overlay_grid,