Skip to content

Commit 06f385f

Browse files
Setting to format date and time with strftime
Depends on: pop-os/cosmic-applets#1129
1 parent 20bb8d0 commit 06f385f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

cosmic-settings/src/pages/time/date.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub struct Page {
4545
show_seconds: bool,
4646
ntp_enabled: bool,
4747
show_date_in_top_panel: bool,
48+
format_strftime: String,
4849
timezone_context: bool,
4950
local_time: Option<DateTime<Gregorian>>,
5051
timezone: Option<usize>,
@@ -102,6 +103,15 @@ impl Default for Page {
102103
true
103104
});
104105

106+
let format_strftime = cosmic_applet_config
107+
.get("format_strftime")
108+
.inspect_err(|err| {
109+
if err.is_err() {
110+
error!(?err, "Failed to read config 'format_strftime'");
111+
}
112+
})
113+
.unwrap_or_default();
114+
105115
Self {
106116
entity: page::Entity::null(),
107117
cosmic_applet_config,
@@ -112,6 +122,7 @@ impl Default for Page {
112122
show_seconds,
113123
ntp_enabled: false,
114124
show_date_in_top_panel,
125+
format_strftime,
115126
timezone: None,
116127
timezone_context: false,
117128
timezone_list: Vec::new(),
@@ -242,6 +253,17 @@ impl Page {
242253
}
243254
}
244255

256+
Message::Strftime(format) => {
257+
self.format_strftime = format;
258+
259+
if let Err(err) = self
260+
.cosmic_applet_config
261+
.set("format_strftime", &self.format_strftime)
262+
{
263+
error!(?err, "Failed to set config 'format_strftime'");
264+
}
265+
}
266+
245267
Message::TimezoneSearch(text) => {
246268
self.timezone_search = text;
247269
}
@@ -382,9 +404,14 @@ impl Page {
382404
fn update_local_time(&mut self) {
383405
self.local_time = Some(update_local_time());
384406

385-
self.formatted_date = match self.local_time {
386-
Some(ref time) => format_date(time, self.military_time, self.show_seconds),
387-
None => fl!("unknown"),
407+
self.formatted_date = if !self.format_strftime.is_empty() {
408+
chrono::Local::now()
409+
.format(&self.format_strftime)
410+
.to_string()
411+
} else if let Some(time) = self.local_time.as_ref() {
412+
format_date(time, self.military_time, self.show_seconds)
413+
} else {
414+
fl!("unknown")
388415
}
389416
}
390417
}
@@ -398,6 +425,7 @@ pub enum Message {
398425
FirstDayOfWeek(usize),
399426
Refresh(Info),
400427
ShowDate(bool),
428+
Strftime(String),
401429
Timezone(usize),
402430
TimezoneContext,
403431
TimezoneSearch(String),
@@ -435,6 +463,7 @@ fn format() -> Section<crate::pages::Message> {
435463
let show_seconds = descriptions.insert(fl!("time-format", "show-seconds"));
436464
let first = descriptions.insert(fl!("time-format", "first"));
437465
let show_date = descriptions.insert(fl!("time-format", "show-date"));
466+
let format_strftime = descriptions.insert(fl!("time-format", "format-strftime"));
438467

439468
Section::default()
440469
.title(fl!("time-format"))
@@ -486,6 +515,12 @@ fn format() -> Section<crate::pages::Message> {
486515
settings::item::builder(&section.descriptions[show_date])
487516
.toggler(page.show_date_in_top_panel, Message::ShowDate),
488517
)
518+
// Format with strftime
519+
.add(
520+
settings::item::builder(&section.descriptions[format_strftime]).control(
521+
widget::text_input("", &page.format_strftime).on_input(Message::Strftime),
522+
),
523+
)
489524
.apply(cosmic::Element::from)
490525
.map(crate::pages::Message::DateAndTime)
491526
})

i18n/en/cosmic_settings.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ time-format = Date & Time Format
783783
.show-seconds = Show seconds
784784
.first = First day of week
785785
.show-date = Show date in the time applet
786+
.format-strftime = Format date and time with strftime
786787
.friday = Friday
787788
.saturday = Saturday
788789
.sunday = Sunday

0 commit comments

Comments
 (0)