Replace lazy_static! with OnceLock in time_format crate (#8648)

This PR replaces a `lazy_static!` usage in the `time_format` crate with
`OnceLock` from the standard library.

This allows us to drop the `lazy_static` dependency from this crate.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-02-29 23:58:45 -05:00 committed by GitHub
parent 0d0ce95eae
commit 7f5aa1fca6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 10 deletions

1
Cargo.lock generated
View file

@ -9436,7 +9436,6 @@ dependencies = [
"anyhow", "anyhow",
"core-foundation", "core-foundation",
"core-foundation-sys 0.8.6", "core-foundation-sys 0.8.6",
"lazy_static",
"sys-locale", "sys-locale",
"time", "time",
] ]

View file

@ -11,7 +11,6 @@ doctest = false
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true
lazy_static.workspace = true
sys-locale.workspace = true sys-locale.workspace = true
time.workspace = true time.workspace = true

View file

@ -1,4 +1,5 @@
use lazy_static::lazy_static; use std::sync::OnceLock;
use time::{OffsetDateTime, UtcOffset}; use time::{OffsetDateTime, UtcOffset};
/// Formats a timestamp, which respects the user's date and time preferences/custom format. /// Formats a timestamp, which respects the user's date and time preferences/custom format.
@ -36,18 +37,18 @@ fn format_timestamp_fallback(
timestamp: OffsetDateTime, timestamp: OffsetDateTime,
timezone: UtcOffset, timezone: UtcOffset,
) -> String { ) -> String {
lazy_static! { static CURRENT_LOCALE: OnceLock<String> = OnceLock::new();
static ref CURRENT_LOCALE: String = let current_locale = CURRENT_LOCALE
sys_locale::get_locale().unwrap_or_else(|| String::from("en-US")); .get_or_init(|| sys_locale::get_locale().unwrap_or_else(|| String::from("en-US")));
}
let is_12_hour_time = is_12_hour_time_by_locale(CURRENT_LOCALE.as_str()); let is_12_hour_time = is_12_hour_time_by_locale(current_locale.as_str());
format_timestamp_naive(reference, timestamp, timezone, is_12_hour_time) format_timestamp_naive(reference, timestamp, timezone, is_12_hour_time)
} }
/// Formats a timestamp, which is either in 12-hour or 24-hour time format. /// Formats a timestamp, which is either in 12-hour or 24-hour time format.
/// Note: /// Note:
/// This function does not respect the user's date and time preferences. /// This function does not respect the user's date and time preferences.
/// This should only be used as a fallback mechanism when the os time formatting fails. /// This should only be used as a fallback mechanism when the OS time formatting fails.
pub fn format_timestamp_naive( pub fn format_timestamp_naive(
reference: OffsetDateTime, reference: OffsetDateTime,
timestamp: OffsetDateTime, timestamp: OffsetDateTime,
@ -109,7 +110,7 @@ pub fn format_timestamp_naive(
} }
} }
/// Returns true if the locale is recognized as a 12-hour time locale. /// Returns `true` if the locale is recognized as a 12-hour time locale.
fn is_12_hour_time_by_locale(locale: &str) -> bool { fn is_12_hour_time_by_locale(locale: &str) -> bool {
[ [
"es-MX", "es-CO", "es-SV", "es-NI", "es-MX", "es-CO", "es-SV", "es-NI",