]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/systemd/tm_editor.rs
770bb280803e42d2687e5534ee95a5048559349e
[proxmox-backup.git] / src / tools / systemd / tm_editor.rs
1 use anyhow::Error;
2
3 use proxmox::tools::time::*;
4
5 pub struct TmEditor {
6 utc: bool,
7 t: libc::tm,
8 }
9
10 impl TmEditor {
11
12 pub fn new(epoch: i64, utc: bool) -> Result<Self, Error> {
13 let t = if utc { gmtime(epoch)? } else { localtime(epoch)? };
14 Ok(Self { utc, t })
15 }
16
17 pub fn into_epoch(mut self) -> Result<i64, Error> {
18 let epoch = if self.utc { timegm(&mut self.t)? } else { timelocal(&mut self.t)? };
19 Ok(epoch)
20 }
21
22 /// increases the year by 'years' and resets all smaller fields to their minimum
23 pub fn add_years(&mut self, years: libc::c_int) -> Result<(), Error> {
24 if years == 0 { return Ok(()); }
25 self.t.tm_mon = 0;
26 self.t.tm_mday = 1;
27 self.t.tm_hour = 0;
28 self.t.tm_min = 0;
29 self.t.tm_sec = 0;
30 self.t.tm_year += years;
31 self.normalize_time()
32 }
33
34 /// increases the month by 'months' and resets all smaller fields to their minimum
35 pub fn add_months(&mut self, months: libc::c_int) -> Result<(), Error> {
36 if months == 0 { return Ok(()); }
37 self.t.tm_mday = 1;
38 self.t.tm_hour = 0;
39 self.t.tm_min = 0;
40 self.t.tm_sec = 0;
41 self.t.tm_mon += months;
42 self.normalize_time()
43 }
44
45 /// increases the day by 'days' and resets all smaller fields to their minimum
46 pub fn add_days(&mut self, days: libc::c_int) -> Result<(), Error> {
47 if days == 0 { return Ok(()); }
48 self.t.tm_hour = 0;
49 self.t.tm_min = 0;
50 self.t.tm_sec = 0;
51 self.t.tm_mday += days;
52 self.normalize_time()
53 }
54
55 pub fn year(&self) -> libc::c_int { self.t.tm_year + 1900 } // see man mktime
56 pub fn month(&self) -> libc::c_int { self.t.tm_mon + 1 }
57 pub fn day(&self) -> libc::c_int { self.t.tm_mday }
58 pub fn hour(&self) -> libc::c_int { self.t.tm_hour }
59 pub fn min(&self) -> libc::c_int { self.t.tm_min }
60 pub fn sec(&self) -> libc::c_int { self.t.tm_sec }
61
62 // Note: tm_wday (0-6, Sunday = 0) => convert to Sunday = 6
63 pub fn day_num(&self) -> libc::c_int {
64 (self.t.tm_wday + 6) % 7
65 }
66
67 pub fn set_time(&mut self, hour: libc::c_int, min: libc::c_int, sec: libc::c_int) -> Result<(), Error> {
68 self.t.tm_hour = hour;
69 self.t.tm_min = min;
70 self.t.tm_sec = sec;
71 self.normalize_time()
72 }
73
74 pub fn set_min_sec(&mut self, min: libc::c_int, sec: libc::c_int) -> Result<(), Error> {
75 self.t.tm_min = min;
76 self.t.tm_sec = sec;
77 self.normalize_time()
78 }
79
80 fn normalize_time(&mut self) -> Result<(), Error> {
81 // libc normalizes it for us
82 if self.utc {
83 timegm(&mut self.t)?;
84 } else {
85 timelocal(&mut self.t)?;
86 }
87 Ok(())
88 }
89
90 pub fn set_sec(&mut self, v: libc::c_int) -> Result<(), Error> {
91 self.t.tm_sec = v;
92 self.normalize_time()
93 }
94
95 pub fn set_min(&mut self, v: libc::c_int) -> Result<(), Error> {
96 self.t.tm_min = v;
97 self.normalize_time()
98 }
99
100 pub fn set_hour(&mut self, v: libc::c_int) -> Result<(), Error> {
101 self.t.tm_hour = v;
102 self.normalize_time()
103 }
104
105 pub fn set_mday(&mut self, v: libc::c_int) -> Result<(), Error> {
106 self.t.tm_mday = v;
107 self.normalize_time()
108 }
109
110 pub fn set_mon(&mut self, v: libc::c_int) -> Result<(), Error> {
111 self.t.tm_mon = v - 1;
112 self.normalize_time()
113 }
114
115 pub fn set_year(&mut self, v: libc::c_int) -> Result<(), Error> {
116 self.t.tm_year = v - 1900;
117 self.normalize_time()
118 }
119 }