melib/datetime: add format string constants

lazy_fetch
Manos Pitsidianakis 2021-01-05 21:32:56 +02:00
parent 4cd3e28244
commit 441dcb62ca
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 11 additions and 8 deletions

View File

@ -42,6 +42,11 @@ use std::convert::TryInto;
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
pub type UnixTimestamp = u64; pub type UnixTimestamp = u64;
pub const RFC3339_FMT_WITH_TIME: &str = "%Y-%m-%dT%H:%M:%S\0";
pub const RFC3339_FMT: &str = "%Y-%m-%d\0";
pub const RFC822_FMT_WITH_TIME: &str = "%a, %e %h %Y %H:%M:%S \0";
pub const RFC822_FMT: &str = "%e %h %Y %H:%M:%S \0";
pub const DEFAULT_FMT: &str = "%a, %d %b %Y %R\0";
extern "C" { extern "C" {
fn strptime( fn strptime(
@ -114,8 +119,9 @@ pub fn timestamp_to_string(timestamp: UnixTimestamp, fmt: Option<&str>, posix: b
let format: &CStr = if let Some(ref s) = fmt { let format: &CStr = if let Some(ref s) = fmt {
&s &s
} else { } else {
unsafe { CStr::from_bytes_with_nul_unchecked(b"%a, %d %b %Y %T %z\0") } unsafe { CStr::from_bytes_with_nul_unchecked(DEFAULT_FMT.as_bytes()).into() }
}; };
let mut vec: [u8; 256] = [0; 256]; let mut vec: [u8; 256] = [0; 256];
let ret = { let ret = {
let _with_locale: Option<Result<Locale>> = if posix { let _with_locale: Option<Result<Locale>> = if posix {
@ -259,12 +265,9 @@ where
{ {
let s = CString::new(s)?; let s = CString::new(s)?;
let mut new_tm: libc::tm = unsafe { std::mem::zeroed() }; let mut new_tm: libc::tm = unsafe { std::mem::zeroed() };
for fmt in &[ for fmt in &[RFC822_FMT_WITH_TIME, RFC822_FMT] {
&b"%a, %e %h %Y %H:%M:%S \0"[..],
&b"%e %h %Y %H:%M:%S \0"[..],
] {
unsafe { unsafe {
let fmt = CStr::from_bytes_with_nul_unchecked(fmt); let fmt = CStr::from_bytes_with_nul_unchecked(fmt.as_bytes());
let ret = { let ret = {
let _with_locale = Locale::new( let _with_locale = Locale::new(
@ -325,9 +328,9 @@ where
{ {
let s = CString::new(s)?; let s = CString::new(s)?;
let mut new_tm: libc::tm = unsafe { std::mem::zeroed() }; let mut new_tm: libc::tm = unsafe { std::mem::zeroed() };
for fmt in &[&b"%Y-%m-%dT%H:%M:%S\0"[..], &b"%Y-%m-%d\0"[..]] { for fmt in &[RFC3339_FMT_WITH_TIME, RFC3339_FMT] {
unsafe { unsafe {
let fmt = CStr::from_bytes_with_nul_unchecked(fmt); let fmt = CStr::from_bytes_with_nul_unchecked(fmt.as_bytes());
let ret = { let ret = {
let _with_locale = Locale::new( let _with_locale = Locale::new(
libc::LC_TIME, libc::LC_TIME,