Browse Source

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

lazy_fetch
Manos Pitsidianakis 11 months ago
parent
commit
3dba6fdf60
Signed by: epilys GPG Key ID: 73627C2F690DF710
  1. 2
      melib/src/addressbook.rs
  2. 34
      melib/src/datetime.rs
  3. 2
      melib/src/email/compose.rs
  4. 6
      melib/src/lib.rs
  5. 3
      melib/src/logging.rs
  6. 1
      src/components/mail/listing/conversations.rs
  7. 2
      src/components/mail/listing/plain.rs
  8. 8
      src/state.rs

2
melib/src/addressbook.rs

@ -192,7 +192,7 @@ impl Card {
self.key.as_str()
}
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) {

34
melib/src/datetime.rs

@ -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() };
unsafe {
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") }
};
let mut vec: [u8; 256] = [0; 256];
let ret = unsafe {
strftime(
vec.as_mut_ptr() as *mut _,
256,
format.as_ptr(),
&new_tm as *const _,
)
let ret = {
let _with_locale: Option<Result<Locale>> = if posix {
Some(
Locale::new(
::libc::LC_TIME,
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()
@ -402,7 +418,7 @@ pub fn now() -> UnixTimestamp {
#[test]
fn test_timestamp() {
timestamp_to_string(0, None);
timestamp_to_string(0, None, false);
}
#[test]

2
melib/src/email/compose.rs

@ -53,7 +53,7 @@ impl Default for Draft {
let mut headers = HeaderMap::default();
headers.insert(
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("To"), "".into());

6
melib/src/lib.rs

@ -41,7 +41,11 @@ pub mod dbg {
() => {
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()
.name()
.map(std::string::ToString::to_string)

3
melib/src/logging.rs

@ -85,7 +85,8 @@ pub fn log<S: AsRef<str>>(val: S, level: LoggingLevel) {
if level <= b.level {
b.dest
.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();
b.dest.write_all(b" [").unwrap();

1
src/components/mail/listing/conversations.rs

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

2
src/components/mail/listing/plain.rs

@ -1010,7 +1010,7 @@ impl PlainListing {
n if n < 4 * 24 * 60 * 60 => {
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),
}
}

8
src/state.rs

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

Loading…
Cancel
Save