-
-
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.
chrono feature and time utils, dlock internal extension management fo…
…r arbitrary futures" (#38) * Exporting redis from bb * Namespace added to dlock * New chrono feature for formatting and timing utilities, dlock contains new expires_at property * dlock new hold_for_fut on lock and on wrapper dlock_for_fut to allow internalising the extension lock when running an arbitrary future that should be locked * omit empty zrem from batch automatically
- Loading branch information
Showing
11 changed files
with
330 additions
and
58 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,52 @@ | ||
/// Formats a [`chrono::Duration`]` in the present case. | ||
/// Arguments: | ||
/// - `td`: The duration to format. | ||
/// - `precise`: If true, smaller units will be included. | ||
pub fn chrono_format_td(td: chrono::Duration, precise: bool) -> String { | ||
// The rough formatting does some funkiness with "now" when less than 10 seconds, so just handle simple seconds case manually: | ||
if !precise && td.num_seconds() <= 59 { | ||
let secs = td.num_seconds().max(1); | ||
format!("{} second{}", secs, if secs > 1 { "s" } else { "" }) | ||
} else { | ||
chrono_humanize::HumanTime::from(td).to_text_en( | ||
if precise { | ||
chrono_humanize::Accuracy::Precise | ||
} else { | ||
chrono_humanize::Accuracy::Rough | ||
}, | ||
chrono_humanize::Tense::Present, | ||
) | ||
} | ||
} | ||
|
||
/// Convert a chrono datetime to the local timezone. | ||
/// | ||
/// Arguments: | ||
/// - `dt`: The datetime to convert. | ||
pub fn chrono_dt_to_local(dt: chrono::DateTime<chrono::Utc>) -> chrono::DateTime<chrono::Local> { | ||
dt.with_timezone(&chrono::Local) | ||
} | ||
|
||
/// Formats a [`chrono::DateTime`], also localising it to the user's timezone. | ||
/// | ||
/// Arguments: | ||
/// - `dt`: The datetime to format. | ||
pub fn chrono_format_dt(dt: chrono::DateTime<chrono::Utc>) -> String { | ||
let dt = chrono_dt_to_local(dt); | ||
|
||
let date = dt.date_naive(); | ||
let today = chrono::Local::now().date_naive(); | ||
let yesterday = today.pred_opt(); | ||
|
||
// If today then: "Today, 12:34" | ||
if date == today { | ||
format!("Today, {}", dt.format("%H:%M")) | ||
} | ||
// Same for yesterday: | ||
else if yesterday.is_some() && date == yesterday.unwrap() { | ||
format!("Yesterday, {}", dt.format("%H:%M")) | ||
} else { | ||
// Otherwise e.g. 15 March 2021, 12:34 | ||
dt.format("%-e %B %Y, %H:%M").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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod chrono_formatting; | ||
|
||
pub use chrono_formatting::*; |
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
Oops, something went wrong.