]> git.proxmox.com Git - proxmox-backup.git/commitdiff
tools/systemd/tm_editor: add setter/getter for months/years/days
authorDominik Csapak <d.csapak@proxmox.com>
Fri, 4 Sep 2020 12:33:32 +0000 (14:33 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 4 Sep 2020 13:34:27 +0000 (15:34 +0200)
add_* are modeled after add_days

subtract one for set_mon to have a consistent interface for all fields
(i.e. getter/setter return/expect the 'real' number, not the ones
in the tm struct)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
src/tools/systemd/tm_editor.rs

index 677e05207ccc428645777ad48c3368d64580ab93..770bb280803e42d2687e5534ee95a5048559349e 100644 (file)
@@ -19,6 +19,29 @@ impl TmEditor {
         Ok(epoch)
     }
 
+    /// increases the year by 'years' and resets all smaller fields to their minimum
+    pub fn add_years(&mut self, years: libc::c_int) -> Result<(), Error> {
+        if years == 0 { return Ok(()); }
+        self.t.tm_mon = 0;
+        self.t.tm_mday = 1;
+        self.t.tm_hour = 0;
+        self.t.tm_min = 0;
+        self.t.tm_sec = 0;
+        self.t.tm_year += years;
+        self.normalize_time()
+    }
+
+    /// increases the month by 'months' and resets all smaller fields to their minimum
+    pub fn add_months(&mut self, months: libc::c_int) -> Result<(), Error> {
+        if months == 0 { return Ok(()); }
+        self.t.tm_mday = 1;
+        self.t.tm_hour = 0;
+        self.t.tm_min = 0;
+        self.t.tm_sec = 0;
+        self.t.tm_mon += months;
+        self.normalize_time()
+    }
+
     /// increases the day by 'days' and resets all smaller fields to their minimum
     pub fn add_days(&mut self, days: libc::c_int) -> Result<(), Error> {
         if days == 0 { return Ok(()); }
@@ -30,6 +53,8 @@ impl TmEditor {
     }
 
     pub fn year(&self) -> libc::c_int { self.t.tm_year + 1900 } // see man mktime
+    pub fn month(&self) -> libc::c_int { self.t.tm_mon + 1 }
+    pub fn day(&self) -> libc::c_int { self.t.tm_mday }
     pub fn hour(&self) -> libc::c_int { self.t.tm_hour }
     pub fn min(&self) -> libc::c_int { self.t.tm_min }
     pub fn sec(&self) -> libc::c_int { self.t.tm_sec }
@@ -83,7 +108,7 @@ impl TmEditor {
     }
 
     pub fn set_mon(&mut self, v: libc::c_int) -> Result<(), Error> {
-        self.t.tm_mon = v;
+        self.t.tm_mon = v - 1;
         self.normalize_time()
     }