summary history branches tags files
commit:9b83d9e1816a00a022a98b91bf2a7ef4d0b07e77
author:Trevor Bentley
committer:Trevor Bentley
date:Sat Mar 18 17:23:34 2023 +0100
parents:c2b47c3986a786097b194dd4fca3a71789c10b16
update time dependency (major API change)
diff --git a/Cargo.lock b/Cargo.lock
line changes: +37/-11
index 883d4f3..bb2cc8d
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -131,7 +131,7 @@ checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
 dependencies = [
  "cfg-if",
  "libc",
- "wasi 0.11.0+wasi-snapshot-preview1",
+ "wasi",
 ]
 
 [[package]]
@@ -235,6 +235,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "num_threads"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
+dependencies = [
+ "libc",
+]
+
+[[package]]
 name = "once_cell"
 version = "1.17.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -346,6 +355,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "serde"
+version = "1.0.157"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "707de5fcf5df2b5788fca98dd7eab490bc2fd9b7ef1404defc462833b83f25ca"
+
+[[package]]
 name = "static_assertions"
 version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -379,13 +394,30 @@ dependencies = [
 
 [[package]]
 name = "time"
-version = "0.1.45"
+version = "0.3.20"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
+checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890"
 dependencies = [
  "libc",
- "wasi 0.10.0+wasi-snapshot-preview1",
- "winapi",
+ "num_threads",
+ "serde",
+ "time-core",
+ "time-macros",
+]
+
+[[package]]
+name = "time-core"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
+
+[[package]]
+name = "time-macros"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36"
+dependencies = [
+ "time-core",
 ]
 
 [[package]]
@@ -412,12 +444,6 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
 
 [[package]]
 name = "wasi"
-version = "0.10.0+wasi-snapshot-preview1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
-
-[[package]]
-name = "wasi"
 version = "0.11.0+wasi-snapshot-preview1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

diff --git a/Cargo.toml b/Cargo.toml
line changes: +1/-1
index cbead05..88692dd
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -41,6 +41,6 @@ regex = "1.7.1"
 glob = "0.3.1"
 rust-ini = "0.18.0"
 clap = { version = "4.1.10", features = ["derive"] }
-time = "0.1.45"
+time = { version = "0.3.20", features = ["macros", "parsing", "local-offset"] }
 nix = "0.26.2"
 users = "0.11.0"

diff --git a/src/main.rs b/src/main.rs
line changes: +42/-33
index f37f98c..32f7ef3
--- a/src/main.rs
+++ b/src/main.rs
@@ -40,9 +40,10 @@ extern crate nix;
 use nix::sys::signal;
 
 extern crate time;
+use time::macros::*;
 
 extern crate users;
-use users::{get_user_by_name};
+use users::get_user_by_name;
 
 use std::io::Write;
 use std::path::PathBuf;
@@ -136,8 +137,13 @@ impl From<nix::Error> for CircadianError {
         CircadianError(error.to_string().to_owned())
     }
 }
-impl From<time::ParseError> for CircadianError {
-    fn from(error: time::ParseError) -> Self {
+impl From<time::error::Parse> for CircadianError {
+    fn from(error: time::error::Parse) -> Self {
+        CircadianError(error.to_string().to_owned())
+    }
+}
+impl From<time::error::ComponentRange> for CircadianError {
+    fn from(error: time::error::ComponentRange) -> Self {
         CircadianError(error.to_string().to_owned())
     }
 }
@@ -893,7 +899,7 @@ fn read_config(file_path: &str) -> Result<CircadianConfig, CircadianError> {
 }
 
 fn test_idle(config: &CircadianConfig, start: i64) -> IdleResponse {
-    let now = time::now_utc().to_timespec().sec as i64;
+    let now = time::OffsetDateTime::now_utc().unix_timestamp();
     let tty = idle_w();
     let xssstate = idle_xssstate();
     let xprintidle = idle_xprintidle();
@@ -980,8 +986,8 @@ fn is_rtc_utc() -> Result<bool, CircadianError> {
         let t = String::from_utf8(output.stdout)?;
         Ok(t.split(":").take(2).collect::<Vec<&str>>().join(":"))
     }
-    let utc_tm = time::now_utc();
-    let utc_time = format!("{:02}:{:02}", utc_tm.tm_hour, utc_tm.tm_min);
+    let utc_tm = time::OffsetDateTime::now_utc().time();
+    let utc_time = format!("{:02}:{:02}", utc_tm.hour(), utc_tm.minute());
     let rtc_time = get_rtc_time()?;
     let is_utc = utc_time == rtc_time;
     let is_synced = utc_time.split(":").nth(1) == rtc_time.split(":").nth(1);
@@ -992,28 +998,28 @@ fn is_rtc_utc() -> Result<bool, CircadianError> {
 }
 
 fn auto_wake_to_epoch(auto_wake: &str) -> Result<AutoWakeEpoch, CircadianError> {
-    let is_utc = is_rtc_utc()?;
-    let auto_wake_tm = time::strptime(auto_wake, "%H:%M")?;
-    let now_local = time::now();
-    let mut target_time_local = now_local.clone();
-    target_time_local.tm_hour = auto_wake_tm.tm_hour;
-    target_time_local.tm_min = auto_wake_tm.tm_min;
-    target_time_local.tm_sec = 0;
+    let _ = is_rtc_utc()?; // just to detect RTC sync errors
+    let format = format_description!("[hour]:[minute]");
+    let auto_wake_tm = time::Time::parse(auto_wake, &format)?;
+    let now_local = match time::OffsetDateTime::now_local() {
+        Ok(l) => l,
+        _ => time::OffsetDateTime::now_utc(),
+    };
+    let target_time_local = now_local.clone();
+    let target_time_local = target_time_local.replace_hour(auto_wake_tm.hour())?;
+    let target_time_local = target_time_local.replace_minute(auto_wake_tm.minute())?;
+    let target_time_local = target_time_local.replace_second(0)?;
     let target_time_local = match target_time_local < now_local {
-        true => (target_time_local + time::Duration::days(1)).to_local(),
+        true => target_time_local + time::Duration::days(1),
         false => target_time_local,
     };
-    let target_time_utc = target_time_local.to_utc();
-    match is_utc {
-        true => Ok(AutoWakeEpoch {
-            epoch: target_time_utc.to_timespec().sec,
-            is_utc: true
-        }),
-        false => Ok(AutoWakeEpoch {
-            epoch: target_time_local.to_timespec().sec,
-            is_utc: false
-        }),
-    }
+
+    // UNIX timestamps are defined as being in UTC, and Linux's RTC is
+    // defined as taking UTC timestamps.
+    Ok(AutoWakeEpoch {
+        epoch: target_time_local.unix_timestamp(),
+        is_utc: true
+    })
 }
 fn set_rtc_wakealarm(timestamp: i64) -> Result<(), CircadianError> {
     {
@@ -1051,8 +1057,11 @@ fn reschedule_auto_wake(auto_wake: Option<&String>, current_epoch: Option<AutoWa
     }
     let epoch = current_epoch.unwrap();
     let now = match epoch.is_utc {
-        true => time::now_utc().to_timespec().sec as i64,
-        false => time::now().to_timespec().sec as i64,
+        true => time::OffsetDateTime::now_utc().unix_timestamp(),
+        false => match time::OffsetDateTime::now_local() {
+            Ok(l) => l.unix_timestamp(),
+            _ => time::OffsetDateTime::now_utc().unix_timestamp(),
+        }
     };
     if now >= epoch.epoch {
         new_rtc = match set_auto_wake(auto_wake) {
@@ -1100,7 +1109,7 @@ fn main() {
 
     if launch_opts.test {
         println!("Got --test: running idle test and exiting.");
-        let start = time::now_utc().to_timespec().sec as i64;
+        let start = time::OffsetDateTime::now_utc().unix_timestamp();
         let idle = test_idle(&config, start);
         let tests = test_nonidle(&config);
         println!("Idle Detection Summary:\n{}{}", idle, tests);
@@ -1155,8 +1164,8 @@ fn main() {
     println!("Configuration valid.  Idle detection starting.");
 
     let mut idle_triggered = false;
-    let mut start = time::now_utc().to_timespec().sec as i64;
-    let mut watchdog = time::now_utc().to_timespec().sec as i64;
+    let mut start = time::OffsetDateTime::now_utc().unix_timestamp();
+    let mut watchdog = time::OffsetDateTime::now_utc().unix_timestamp();
     loop {
         let idle = test_idle(&config, start);
         // If it's idle, the idle command hasn't already run, and it has been
@@ -1197,11 +1206,11 @@ fn main() {
                 println!("Idle Detection Summary:\n{}{}", idle, tests);
             }
 
-            let now = time::now_utc().to_timespec().sec as i64;
+            let now = time::OffsetDateTime::now_utc().unix_timestamp();
             // Look for clock jumps that indicate the system slept
             if watchdog + 30 < now {
                 println!("Watchdog missed.  Wake from sleep!");
-                start = time::now_utc().to_timespec().sec as i64;
+                start = time::OffsetDateTime::now_utc().unix_timestamp();
                 let idle = test_idle(&config, start);
                 let tests = test_nonidle(&config);
                 println!("Idle state on wake:\n{}{}", idle, tests);
@@ -1220,7 +1229,7 @@ fn main() {
             // Kick watchdog timer frequently, and possibly reschedule auto-wake timer
             if watchdog + 10 < now {
                 current_rtc = reschedule_auto_wake(config.auto_wake.as_ref(), current_rtc);
-                watchdog = time::now_utc().to_timespec().sec as i64;
+                watchdog = time::OffsetDateTime::now_utc().unix_timestamp();
             }
             std::thread::sleep(std::time::Duration::from_millis(sleep_chunk));
         }