-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Refactoring ls and date #7194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring ls and date #7194
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| mod timezone_date { | ||
|
||
| /// Get the alphabetic abbreviation of the current timezone. | ||
| /// | ||
| /// For example, "UTC" or "CET" or "PDT". | ||
| fn timezone_abbrev() -> &str { | ||
|
||
| let tz = match std::env::var("TZ") { | ||
| // TODO Support other time zones... | ||
| Ok(s) if s == "UTC0" || s.is_empty() => Tz::Etc__UTC, | ||
| _ => match get_timezone() { | ||
| Ok(tz_str) => tz_str.parse().unwrap(), | ||
| Err(_) => Tz::Etc__UTC, | ||
| }, | ||
| }; | ||
| let offset = tz.offset_from_utc_date(&Utc::now().date_naive()); | ||
| offset.abbreviation().unwrap_or("UTC").to_string() | ||
| } | ||
|
|
||
| /// Format the given time according to a custom format string. | ||
|
||
| pub fn custom_time_format(fmt: &str, time: DateTime<Local>) -> String { | ||
| // TODO - Revisit when chrono 0.5 is released. https://github.com/chronotope/chrono/issues/970 | ||
| // GNU `date` uses `%N` for nano seconds, however the `chrono` crate uses `%f`. | ||
| let fmt = fmt.replace("%N", "%f").replace("%Z", timezone_abbrev()); | ||
| time.format(&fmt).to_string() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this comment is no longer needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right