Skip to content

Commit 1c725b9

Browse files
time: Tick per sec if strftime has seconds spec
It's possible to have a discrepancy between strftime and the show_seconds setting. If show_seconds is disabled but a user's strftime formatter has a seconds specifier, then the time applet won't update seconds because it's internally ticking per minute. The fix is to ensure that the time applet updates per second regardless of the user's setting if strftime has a seconds specifier. This is an internal flag and shouldn't affect the user's show_seconds setting.
1 parent 0ed1e8c commit 1c725b9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

cosmic-applet-time/src/window.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ use icu::{
4343

4444
static AUTOSIZE_MAIN_ID: LazyLock<Id> = LazyLock::new(|| Id::new("autosize-main"));
4545

46+
// Specifiers for strftime that indicate seconds. Subsecond precision isn't supported by the applet
47+
// so those specifiers aren't listed here. This list is non-exhaustive, and it's possible that %X
48+
// and other specifiers have to be added depending on locales.
49+
const STRFTIME_SECONDS: &[char] = &['S', 'T', '+', 's'];
50+
4651
fn get_system_locale() -> Locale {
4752
for var in ["LC_TIME", "LC_ALL", "LANG"] {
4853
if let Ok(locale_str) = std::env::var(var) {
@@ -604,7 +609,17 @@ impl cosmic::Application for Window {
604609
Message::ConfigChanged(c) => {
605610
// Don't interrupt the tick subscription unless necessary
606611
self.show_seconds_tx.send_if_modified(|show_seconds| {
607-
if *show_seconds == c.show_seconds {
612+
if c.format_strftime
613+
.split('%')
614+
.any(|s| STRFTIME_SECONDS.contains(&s.chars().next().unwrap_or_default()))
615+
&& !*show_seconds
616+
{
617+
// The strftime formatter contains a seconds specifier. Force enable
618+
// ticking per seconds internally regardless of the user setting.
619+
// This does not change the user's setting. It's invisible to the user.
620+
*show_seconds = true;
621+
true
622+
} else if *show_seconds == c.show_seconds {
608623
false
609624
} else {
610625
*show_seconds = c.show_seconds;

0 commit comments

Comments
 (0)