public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2 08/11] tools/systemd/tm_editor: move conversion of the year into getter and setter
Date: Fri,  4 Sep 2020 14:33:31 +0200	[thread overview]
Message-ID: <20200904123334.3731-9-d.csapak@proxmox.com> (raw)
In-Reply-To: <20200904123334.3731-1-d.csapak@proxmox.com>

the tm struct contains the year - 1900 but we added that

if we want to use the libc normalization correctly, the tm struct
must have the correct year in it, else the computations for timezones,
etc. fail

instead add a getter that adds the years and a setter that subtracts it again

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/tools/systemd/tm_editor.rs | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/tools/systemd/tm_editor.rs b/src/tools/systemd/tm_editor.rs
index b098f1cb..677e0520 100644
--- a/src/tools/systemd/tm_editor.rs
+++ b/src/tools/systemd/tm_editor.rs
@@ -10,13 +10,11 @@ pub struct TmEditor {
 impl TmEditor {
 
     pub fn new(epoch: i64, utc: bool) -> Result<Self, Error> {
-        let mut t = if utc { gmtime(epoch)? } else { localtime(epoch)? };
-        t.tm_year += 1900; // real years for clarity
+        let t = if utc { gmtime(epoch)? } else { localtime(epoch)? };
         Ok(Self { utc, t })
     }
 
     pub fn into_epoch(mut self) -> Result<i64, Error> {
-        self.t.tm_year -= 1900;
         let epoch = if self.utc { timegm(&mut self.t)? } else { timelocal(&mut self.t)? };
         Ok(epoch)
     }
@@ -31,6 +29,7 @@ impl TmEditor {
         self.normalize_time()
     }
 
+    pub fn year(&self) -> libc::c_int { self.t.tm_year + 1900 } // see man mktime
     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 }
@@ -89,7 +88,7 @@ impl TmEditor {
     }
 
     pub fn set_year(&mut self, v: libc::c_int) -> Result<(), Error> {
-        self.t.tm_year = v;
+        self.t.tm_year = v - 1900;
         self.normalize_time()
     }
 }
-- 
2.20.1





  parent reply	other threads:[~2020-09-04 12:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-04 12:33 [pbs-devel] [PATCH proxmox-backup v2 00/11] implement dates for calendarevents Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 01/11] tools/systemd/tm_editor: remove TMChanges optimization Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 02/11] tools/systemd/time: let libc normalize time for us Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 03/11] tools/systemd/time: move continue out of the if/else Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 04/11] tools/systemd/time: convert the resulting timestamp into an option Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 05/11] tools/systemd/tm_editor: remove reset_time from add_days and document it Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 06/11] tools/systemd/parse_time: error out on invalid ranges Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 07/11] tools/systemd/time: fix selection for multiple options Dominik Csapak
2020-09-04 13:38   ` Dietmar Maurer
2020-09-07  6:27     ` Dominik Csapak
2020-09-04 12:33 ` Dominik Csapak [this message]
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 09/11] tools/systemd/tm_editor: add setter/getter for months/years/days Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 10/11] tools/systemd/time: fix signed conversion Dominik Csapak
2020-09-04 12:33 ` [pbs-devel] [PATCH proxmox-backup v2 11/11] tools/systemd/time: enable dates for calendarevents Dominik Csapak
2020-09-04 13:36 ` [pbs-devel] applied: [PATCH proxmox-backup v2 00/11] implement " Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200904123334.3731-9-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal