refactor: replace chrono with jiff
This commit is contained in:
parent
619910c5cc
commit
ec1b80534a
4 changed files with 39 additions and 88 deletions
52
Cargo.lock
generated
52
Cargo.lock
generated
|
|
@ -875,24 +875,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
|
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
"js-sys",
|
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"pure-rust-locales",
|
|
||||||
"serde",
|
"serde",
|
||||||
"wasm-bindgen",
|
|
||||||
"windows-link 0.2.1",
|
"windows-link 0.2.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chrono-tz"
|
|
||||||
version = "0.10.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a6139a8597ed92cf816dfb33f5dd6cf0bb93a6adc938f11039f371bc5bcd26c3"
|
|
||||||
dependencies = [
|
|
||||||
"chrono",
|
|
||||||
"phf 0.12.1",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clang-sys"
|
name = "clang-sys"
|
||||||
version = "1.8.1"
|
version = "1.8.1"
|
||||||
|
|
@ -1244,8 +1231,6 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-fn-stream",
|
"async-fn-stream",
|
||||||
"chrono",
|
|
||||||
"chrono-tz",
|
|
||||||
"clap_lex",
|
"clap_lex",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"cosmic-applets-config",
|
"cosmic-applets-config",
|
||||||
|
|
@ -1268,6 +1253,8 @@ dependencies = [
|
||||||
"i18n-embed-fl",
|
"i18n-embed-fl",
|
||||||
"icu",
|
"icu",
|
||||||
"image",
|
"image",
|
||||||
|
"jiff",
|
||||||
|
"jiff-icu",
|
||||||
"kdl",
|
"kdl",
|
||||||
"libcosmic",
|
"libcosmic",
|
||||||
"logind-zbus",
|
"logind-zbus",
|
||||||
|
|
@ -3599,6 +3586,17 @@ dependencies = [
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jiff-icu"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e67c2beaae8b10a82d849b9aabb698a43a682f32b17bcdc035d5ecadb44d646"
|
||||||
|
dependencies = [
|
||||||
|
"icu_calendar",
|
||||||
|
"icu_time",
|
||||||
|
"jiff",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jiff-static"
|
name = "jiff-static"
|
||||||
version = "0.2.23"
|
version = "0.2.23"
|
||||||
|
|
@ -4886,15 +4884,6 @@ dependencies = [
|
||||||
"phf_shared 0.11.3",
|
"phf_shared 0.11.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "phf"
|
|
||||||
version = "0.12.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "913273894cec178f401a31ec4b656318d95473527be05c0752cc41cdc32be8b7"
|
|
||||||
dependencies = [
|
|
||||||
"phf_shared 0.12.1",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "phf"
|
name = "phf"
|
||||||
version = "0.13.1"
|
version = "0.13.1"
|
||||||
|
|
@ -4962,15 +4951,6 @@ dependencies = [
|
||||||
"siphasher",
|
"siphasher",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "phf_shared"
|
|
||||||
version = "0.12.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "06005508882fb681fd97892ecff4b7fd0fee13ef1aa569f8695dae7ab9099981"
|
|
||||||
dependencies = [
|
|
||||||
"siphasher",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "phf_shared"
|
name = "phf_shared"
|
||||||
version = "0.13.1"
|
version = "0.13.1"
|
||||||
|
|
@ -5238,12 +5218,6 @@ dependencies = [
|
||||||
"syn 2.0.117",
|
"syn 2.0.117",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pure-rust-locales"
|
|
||||||
version = "0.8.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "869675ad2d7541aea90c6d88c81f46a7f4ea9af8cd0395d38f11a95126998a0d"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pwd"
|
name = "pwd"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@ vergen = { version = "8", features = ["git", "gitcl"] }
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
async-fn-stream = "0.3"
|
async-fn-stream = "0.3"
|
||||||
icu = { version = "2.0.0", features = ["compiled_data"] }
|
icu = { version = "2.0.0", features = ["compiled_data"] }
|
||||||
chrono-tz = "0.10"
|
jiff = "0.2"
|
||||||
chrono = { version = "0.4", features = ["unstable-locales"] }
|
jiff-icu = "0.2"
|
||||||
cosmic-applets-config.workspace = true
|
cosmic-applets-config.workspace = true
|
||||||
cosmic-bg-config.workspace = true
|
cosmic-bg-config.workspace = true
|
||||||
cosmic-comp-config.workspace = true
|
cosmic-comp-config.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ pub enum Message {
|
||||||
Prompt(String, bool, Option<String>),
|
Prompt(String, bool, Option<String>),
|
||||||
SessionLockEvent(SessionLockEvent),
|
SessionLockEvent(SessionLockEvent),
|
||||||
Tick,
|
Tick,
|
||||||
Tz(chrono_tz::Tz),
|
Tz(jiff::tz::TimeZone),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M: From<Message> + Send + 'static> Common<M> {
|
impl<M: From<Message> + Send + 'static> Common<M> {
|
||||||
|
|
|
||||||
69
src/time.rs
69
src/time.rs
|
|
@ -1,6 +1,5 @@
|
||||||
use anyhow::bail;
|
use anyhow::bail;
|
||||||
use async_fn_stream::StreamEmitter;
|
use async_fn_stream::StreamEmitter;
|
||||||
use chrono::{Datelike, Timelike};
|
|
||||||
use cosmic::{
|
use cosmic::{
|
||||||
Element, Task, style,
|
Element, Task, style,
|
||||||
widget::{column, text},
|
widget::{column, text},
|
||||||
|
|
@ -8,12 +7,13 @@ use cosmic::{
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use icu::{
|
use icu::{
|
||||||
datetime::{
|
datetime::{
|
||||||
DateTimeFormatter, DateTimeFormatterPreferences, fieldsets,
|
DateTimeFormatter, DateTimeFormatterPreferences, fieldsets, input::DateTime,
|
||||||
input::{Date, DateTime, Time as IcuTime},
|
|
||||||
options::TimePrecision,
|
options::TimePrecision,
|
||||||
},
|
},
|
||||||
locale::{Locale, preferences::extensions::unicode::keywords::HourCycle},
|
locale::{Locale, preferences::extensions::unicode::keywords::HourCycle},
|
||||||
};
|
};
|
||||||
|
use jiff::tz::TimeZone;
|
||||||
|
use jiff_icu::ConvertFrom;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use timedate_zbus::TimeDateProxy;
|
use timedate_zbus::TimeDateProxy;
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
|
|
@ -21,8 +21,8 @@ use tokio::time;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Time {
|
pub struct Time {
|
||||||
locale: Locale,
|
locale: Locale,
|
||||||
timezone: Option<chrono_tz::Tz>,
|
timezone: Option<TimeZone>,
|
||||||
now: chrono::DateTime<chrono::FixedOffset>,
|
now: jiff::Zoned,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Time {
|
impl Time {
|
||||||
|
|
@ -53,7 +53,7 @@ impl Time {
|
||||||
}
|
}
|
||||||
|
|
||||||
let locale = get_local();
|
let locale = get_local();
|
||||||
let now = chrono::Local::now().fixed_offset();
|
let now = jiff::Zoned::now();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
locale,
|
locale,
|
||||||
|
|
@ -62,7 +62,7 @@ impl Time {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tz(&mut self, tz: chrono_tz::Tz) {
|
pub fn set_tz(&mut self, tz: TimeZone) {
|
||||||
self.timezone = Some(tz);
|
self.timezone = Some(tz);
|
||||||
self.tick();
|
self.tick();
|
||||||
}
|
}
|
||||||
|
|
@ -70,30 +70,19 @@ impl Time {
|
||||||
pub fn tick(&mut self) {
|
pub fn tick(&mut self) {
|
||||||
self.now = self
|
self.now = self
|
||||||
.timezone
|
.timezone
|
||||||
.map(|tz| chrono::Local::now().with_timezone(&tz).fixed_offset())
|
.as_ref()
|
||||||
.unwrap_or_else(|| chrono::Local::now().into());
|
.map(|tz| jiff::Timestamp::now().to_zoned(tz.clone()))
|
||||||
|
.unwrap_or_else(|| jiff::Zoned::now());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_date<D: Datelike>(&self, date: &D) -> String {
|
pub fn format_date(&self) -> String {
|
||||||
let prefs = DateTimeFormatterPreferences::from(&self.locale);
|
let prefs = DateTimeFormatterPreferences::from(&self.locale);
|
||||||
let dtf = DateTimeFormatter::try_new(prefs, fieldsets::MDE::long()).unwrap();
|
let dtf = DateTimeFormatter::try_new(prefs, fieldsets::MDE::long()).unwrap();
|
||||||
|
dtf.format(&DateTime::convert_from(self.now.datetime()))
|
||||||
let datetime = DateTime {
|
.to_string()
|
||||||
date: Date::try_new_gregorian(date.year(), date.month() as u8, date.day() as u8)
|
|
||||||
.unwrap(),
|
|
||||||
time: IcuTime::try_new(
|
|
||||||
self.now.hour() as u8,
|
|
||||||
self.now.minute() as u8,
|
|
||||||
self.now.second() as u8,
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
dtf.format(&datetime).to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_time<D: Datelike>(&self, date: &D, military_time: bool) -> String {
|
pub fn format_time(&self, military_time: bool) -> String {
|
||||||
let mut prefs = DateTimeFormatterPreferences::from(&self.locale);
|
let mut prefs = DateTimeFormatterPreferences::from(&self.locale);
|
||||||
prefs.hour_cycle = Some(if military_time {
|
prefs.hour_cycle = Some(if military_time {
|
||||||
HourCycle::H23
|
HourCycle::H23
|
||||||
|
|
@ -105,30 +94,18 @@ impl Time {
|
||||||
fieldsets::T::medium().with_time_precision(TimePrecision::Minute),
|
fieldsets::T::medium().with_time_precision(TimePrecision::Minute),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
dtf.format(&DateTime::convert_from(self.now.datetime()))
|
||||||
let datetime = DateTime {
|
.to_string()
|
||||||
date: Date::try_new_gregorian(date.year(), date.month() as u8, date.day() as u8)
|
|
||||||
.unwrap(),
|
|
||||||
time: IcuTime::try_new(
|
|
||||||
self.now.hour() as u8,
|
|
||||||
self.now.minute() as u8,
|
|
||||||
self.now.second() as u8,
|
|
||||||
0,
|
|
||||||
)
|
|
||||||
.unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
dtf.format(&datetime).to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn date_time_widget<'a, M: 'a>(&self, military_time: bool) -> cosmic::Element<'a, M> {
|
pub fn date_time_widget<'a, M: 'a>(&self, military_time: bool) -> Element<'a, M> {
|
||||||
Element::from(
|
Element::from(
|
||||||
column::with_capacity(2)
|
column::with_capacity(2)
|
||||||
.padding(16.)
|
.padding(16.)
|
||||||
.spacing(12.0)
|
.spacing(12.0)
|
||||||
.push(text::title2(self.format_date(&self.now)).class(style::Text::Accent))
|
.push(text::title2(self.format_date()).class(style::Text::Accent))
|
||||||
.push(
|
.push(
|
||||||
text(self.format_time(&self.now, military_time))
|
text(self.format_time(military_time))
|
||||||
.size(if military_time { 112. } else { 75. })
|
.size(if military_time { 112. } else { 75. })
|
||||||
.class(style::Text::Accent),
|
.class(style::Text::Accent),
|
||||||
),
|
),
|
||||||
|
|
@ -136,7 +113,7 @@ impl Time {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tz_updates() -> Task<chrono_tz::Tz> {
|
pub fn tz_updates() -> Task<TimeZone> {
|
||||||
Task::stream(async_fn_stream::fn_stream(|emitter| async move {
|
Task::stream(async_fn_stream::fn_stream(|emitter| async move {
|
||||||
loop {
|
loop {
|
||||||
if let Err(err) = tz_stream(&emitter).await {
|
if let Err(err) = tz_stream(&emitter).await {
|
||||||
|
|
@ -159,7 +136,7 @@ pub fn tick() -> Task<()> {
|
||||||
|
|
||||||
// Calculate a delta if we're ticking per minute to keep ticks stable
|
// Calculate a delta if we're ticking per minute to keep ticks stable
|
||||||
// Based on i3status-rust
|
// Based on i3status-rust
|
||||||
let current = chrono::Local::now().second() as u64 % 60;
|
let current = jiff::Zoned::now().second() as u64 % 60;
|
||||||
if current != 0 {
|
if current != 0 {
|
||||||
timer.reset_after(time::Duration::from_secs(60 - current));
|
timer.reset_after(time::Duration::from_secs(60 - current));
|
||||||
}
|
}
|
||||||
|
|
@ -167,7 +144,7 @@ pub fn tick() -> Task<()> {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn tz_stream(emitter: &StreamEmitter<chrono_tz::Tz>) -> anyhow::Result<()> {
|
pub async fn tz_stream(emitter: &StreamEmitter<TimeZone>) -> anyhow::Result<()> {
|
||||||
let Ok(conn) = zbus::Connection::system().await else {
|
let Ok(conn) = zbus::Connection::system().await else {
|
||||||
bail!("No zbus system connection.");
|
bail!("No zbus system connection.");
|
||||||
};
|
};
|
||||||
|
|
@ -184,7 +161,7 @@ pub async fn tz_stream(emitter: &StreamEmitter<chrono_tz::Tz>) -> anyhow::Result
|
||||||
let Ok(tz) = property.get().await else {
|
let Ok(tz) = property.get().await else {
|
||||||
bail!("Failed to get property");
|
bail!("Failed to get property");
|
||||||
};
|
};
|
||||||
let Ok(tz) = tz.parse::<chrono_tz::Tz>() else {
|
let Ok(tz) = TimeZone::get(&tz) else {
|
||||||
bail!("Failed to parse timezone.");
|
bail!("Failed to parse timezone.");
|
||||||
};
|
};
|
||||||
emitter.emit(tz).await;
|
emitter.emit(tz).await;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue