How can I print the date / time from time :: strftime without leading zeros?

How to print date / time without leading zeros? For example, Jul 5, 9:15 .

According to docs, it uses the same syntax as strftime , however suppressing leading zeros

 time::strftime("%b %-d, %-I:%M", &time::now()).unwrap() 

leads to an error:

thread '' panarked at 'called Result::unwrap() by value Err : InvalidFormatSpecifier (' - ')', .. / src / libcore / result.rs: 746

I suspect that Rust does not support the glibc extensions that provide this flag (and several others); however, there is no syntax for dates / times with a prefix; the alternative ( %l ) is just prefixes with empty space that is equally useless.

I could create the string manually, but this defeats the purpose of the function.

+6
source share
1 answer

By looking at the code, we can confirm that the time box does not support the - flag. Also note that the time box is on rust-lang-deprecated by rust-lang-deprecated , so it is deprecated.


However, I recommend you use the chrono box. In addition to supporting the format specifiers you want, the chronograph also has time zone support and much more.

 let now = chrono::Utc::now(); println!("{}", now.format("%b %-d, %-I:%M").to_string()); 
+8
source

All Articles