8595bb06137d5b4aeab8f57cac47de62e5387a93
[sfrench/samba-autobuild/.git] / lib / util / time.h
1 /* 
2    Unix SMB/CIFS implementation.
3    time utility functions
4
5    Copyright (C) Andrew Tridgell                1992-2004
6    Copyright (C) Stefan (metze) Metzmacher      2002
7    Copyright (C) Jeremy Allison                 2007
8    Copyright (C) Andrew Bartlett                2011
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifndef _SAMBA_TIME_H_
25 #define _SAMBA_TIME_H_
26
27 #ifndef TIME_T_MIN
28 /* we use 0 here, because (time_t)-1 means error */
29 #define TIME_T_MIN 0
30 #endif
31
32 /*
33  * we use the INT32_MAX here as on 64 bit systems,
34  * gmtime() fails with INT64_MAX
35  */
36 #ifndef TIME_T_MAX
37 #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
38 #endif
39
40
41 /* 64 bit time (100 nanosec) 1601 - cifs6.txt, section 3.5, page 30, 4 byte aligned */
42 typedef uint64_t NTTIME;
43
44 /**
45  External access to time_t_min and time_t_max.
46 **/
47 time_t get_time_t_max(void);
48
49 /**
50 a gettimeofday wrapper
51 **/
52 void GetTimeOfDay(struct timeval *tval);
53
54 /**
55 a wrapper to preferably get the monotonic time
56 **/
57 void clock_gettime_mono(struct timespec *tp);
58
59 /**
60 a wrapper to preferably get the monotonic time in s
61 **/
62 time_t time_mono(time_t *t);
63
64 /**
65 interpret an 8 byte "filetime" structure to a time_t
66 It's originally in "100ns units since jan 1st 1601"
67 **/
68 time_t nt_time_to_unix(NTTIME nt);
69
70 /**
71 put a 8 byte filetime from a time_t
72 This takes GMT as input
73 **/
74 void unix_to_nt_time(NTTIME *nt, time_t t);
75
76 /**
77 check if it's a null unix time
78 **/
79 bool null_time(time_t t);
80
81 /**
82 check if it's a null NTTIME
83 **/
84 bool null_nttime(NTTIME t);
85
86 /**
87 put a dos date into a buffer (time/date format)
88 This takes GMT time and puts local time in the buffer
89 **/
90 void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset);
91
92 /**
93 put a dos date into a buffer (date/time format)
94 This takes GMT time and puts local time in the buffer
95 **/
96 void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset);
97
98 /**
99 put a dos 32 bit "unix like" date into a buffer. This routine takes
100 GMT and converts it to LOCAL time before putting it (most SMBs assume
101 localtime for this sort of date)
102 **/
103 void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset);
104
105 /**
106   create a unix date (int GMT) from a dos date (which is actually in
107   localtime)
108 **/
109 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset);
110
111 /**
112 like make_unix_date() but the words are reversed
113 **/
114 time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset);
115
116 /**
117   create a unix GMT date from a dos date in 32 bit "unix like" format
118   these generally arrive as localtimes, with corresponding DST
119 **/
120 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset);
121
122 /**
123  Return a date and time as a string (optionally with microseconds)
124
125  format is %Y/%m/%d %H:%M:%S if strftime is available
126 **/
127
128 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires);
129
130 /**
131  Return the current date and time as a string (optionally with microseconds)
132
133  format is %Y/%m/%d %H:%M:%S if strftime is available
134 **/
135 char *current_timestring(TALLOC_CTX *ctx, bool hires);
136
137 /**
138 return a HTTP/1.0 time string
139 **/
140 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t);
141
142 /**
143  Return the date and time as a string
144
145  format is %a %b %e %X %Y %Z
146 **/
147 char *timestring(TALLOC_CTX *mem_ctx, time_t t);
148
149 /**
150   return a talloced string representing a NTTIME for human consumption
151 */
152 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt);
153
154 /**
155   put a NTTIME into a packet
156 */
157 void push_nttime(uint8_t *base, uint16_t offset, NTTIME t);
158
159 /**
160   pull a NTTIME from a packet
161 */
162 NTTIME pull_nttime(uint8_t *base, uint16_t offset);
163
164 /**
165   parse a nttime as a large integer in a string and return a NTTIME
166 */
167 NTTIME nttime_from_string(const char *s);
168
169 /**
170   return (tv1 - tv2) in microseconds
171 */
172 int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2);
173
174 /**
175   return (tp1 - tp2) in nanoseconds
176 */
177 int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2);
178
179 /**
180   return a zero timeval
181 */
182 struct timeval timeval_zero(void);
183
184 /**
185   return true if a timeval is zero
186 */
187 bool timeval_is_zero(const struct timeval *tv);
188
189 /**
190   return a timeval for the current time
191 */
192 struct timeval timeval_current(void);
193
194 /**
195   return a timeval struct with the given elements
196 */
197 struct timeval timeval_set(uint32_t secs, uint32_t usecs);
198
199 /**
200   return a timeval ofs microseconds after tv
201 */
202 struct timeval timeval_add(const struct timeval *tv,
203                            uint32_t secs, uint32_t usecs);
204
205 /**
206   return the sum of two timeval structures
207 */
208 struct timeval timeval_sum(const struct timeval *tv1,
209                            const struct timeval *tv2);
210
211 /**
212   return a timeval secs/usecs into the future
213 */
214 struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs);
215
216 /**
217   return a timeval milliseconds into the future
218 */
219 struct timeval timeval_current_ofs_msec(uint32_t msecs);
220
221 /**
222   return a timeval microseconds into the future
223 */
224 struct timeval timeval_current_ofs_usec(uint32_t usecs);
225
226 /**
227   compare two timeval structures. 
228   Return -1 if tv1 < tv2
229   Return 0 if tv1 == tv2
230   Return 1 if tv1 > tv2
231 */
232 int timeval_compare(const struct timeval *tv1, const struct timeval *tv2);
233
234 /**
235   return true if a timer is in the past
236 */
237 bool timeval_expired(const struct timeval *tv);
238
239 /**
240   return the number of seconds elapsed between two times
241 */
242 double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2);
243
244 /**
245   return the number of seconds elapsed since a given time
246 */
247 double timeval_elapsed(const struct timeval *tv);
248
249 /**
250   return the number of seconds elapsed between two times
251 */
252 double timespec_elapsed2(const struct timespec *ts1,
253                          const struct timespec *ts2);
254 /**
255   return the number of seconds elapsed since a given time
256 */
257 double timespec_elapsed(const struct timespec *ts);
258
259 /**
260   return the lesser of two timevals
261 */
262 struct timeval timeval_min(const struct timeval *tv1,
263                            const struct timeval *tv2);
264
265 /**
266   return the greater of two timevals
267 */
268 struct timeval timeval_max(const struct timeval *tv1,
269                            const struct timeval *tv2);
270
271 /**
272   return the difference between two timevals as a timeval
273   if tv1 comes after tv2, then return a zero timeval
274   (this is *tv2 - *tv1)
275 */
276 struct timeval timeval_until(const struct timeval *tv1,
277                              const struct timeval *tv2);
278
279 /**
280   convert a timeval to a NTTIME
281 */
282 NTTIME timeval_to_nttime(const struct timeval *tv);
283
284 /**
285   convert a NTTIME to a timeval
286 */
287 void nttime_to_timeval(struct timeval *tv, NTTIME t);
288
289 /**
290   return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
291  */
292 int get_time_zone(time_t t);
293
294 /**
295   check if 2 NTTIMEs are equal.
296 */
297 bool nt_time_equal(NTTIME *t1, NTTIME *t2);
298
299 void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second);
300
301 struct timespec nt_time_to_unix_timespec(NTTIME nt);
302
303 time_t convert_timespec_to_time_t(struct timespec ts);
304
305 struct timespec convert_time_t_to_timespec(time_t t);
306
307 bool null_timespec(struct timespec ts);
308
309 struct timespec convert_timeval_to_timespec(const struct timeval tv);
310 struct timeval convert_timespec_to_timeval(const struct timespec ts);
311 struct timespec timespec_current(void);
312 struct timespec timespec_min(const struct timespec *ts1,
313                              const struct timespec *ts2);
314 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2);
315 void round_timespec_to_sec(struct timespec *ts);
316 void round_timespec_to_usec(struct timespec *ts);
317 NTTIME unix_timespec_to_nt_time(struct timespec ts);
318
319 #endif /* _SAMBA_TIME_H_ */