-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving date formatting functionality to a separate utility module
- Loading branch information
1 parent
a83a3a8
commit 0297a40
Showing
3 changed files
with
31 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! Contains miscellaneous utilities for formatting dates. | ||
|
||
use chrono::{DateTime, NaiveDateTime, Utc}; | ||
use chrono_tz::America::Chicago; | ||
|
||
/// Format a raw date with the format `%Y-%m-%dT%H:%M:%S%.3f` into an more human-readable format. | ||
pub fn format_date(raw_date: &str) -> String { | ||
let parsed_datetime = NaiveDateTime::parse_from_str(raw_date, "%Y-%m-%dT%H:%M:%S%.3f") | ||
.ok() | ||
.map(|datetime| DateTime::<Utc>::from_utc(datetime, Utc)); | ||
|
||
let chicago_datetime = parsed_datetime.map(|datetime| datetime.with_timezone(&Chicago)); | ||
|
||
let formatted_str = | ||
chicago_datetime.map(|datetime| datetime.format("%Y/%m/%d %H:%M:%S %Z").to_string()); | ||
|
||
if let Some(formatted_date) = formatted_str { | ||
formatted_date | ||
} else { | ||
raw_date.to_string() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//! Contains miscellaneous utilities for the site. | ||
|
||
pub mod background; | ||
pub mod date; | ||
pub mod open_graph; |