Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

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

Marshall Bowers7f5aa1fca67c
diff --git a/Cargo.lock b/Cargo.lock
index 57b487470a8..9a402fbe74f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -9436,7 +9436,6 @@ dependencies = [
"anyhow",
"core-foundation",
"core-foundation-sys 0.8.6",
- "lazy_static",
"sys-locale",
"time",
]
diff --git a/crates/time_format/Cargo.toml b/crates/time_format/Cargo.toml
index 5cdaaa5b5a6..ea1fb336d56 100644
--- a/crates/time_format/Cargo.toml
+++ b/crates/time_format/Cargo.toml
@@ -11,7 +11,6 @@ doctest = false
[dependencies]
anyhow.workspace = true
-lazy_static.workspace = true
sys-locale.workspace = true
time.workspace = true
diff --git a/crates/time_format/src/time_format.rs b/crates/time_format/src/time_format.rs
index 1a6002b700d..b80d52ab657 100644
--- a/crates/time_format/src/time_format.rs
+++ b/crates/time_format/src/time_format.rs
@@ -1,4 +1,5 @@
-use lazy_static::lazy_static;
+use std::sync::OnceLock;
+
use time::{OffsetDateTime, UtcOffset};
/// Formats a timestamp, which respects the user's date and time preferences/custom format.
@@ -36,18 +37,18 @@ fn format_timestamp_fallback(
timestamp: OffsetDateTime,
timezone: UtcOffset,
) -> String {
- lazy_static! {
- static ref CURRENT_LOCALE: String =
- 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());
+ static CURRENT_LOCALE: OnceLock<String> = OnceLock::new();
+ let current_locale = CURRENT_LOCALE
+ .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());
format_timestamp_naive(reference, timestamp, timezone, is_12_hour_time)
}
/// Formats a timestamp, which is either in 12-hour or 24-hour time format.
/// Note:
/// 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(
reference: 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 {
[
"es-MX", "es-CO", "es-SV", "es-NI",