2 Unix SMB/CIFS implementation.
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
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.
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.
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/>.
24 #ifndef _SAMBA_TIME_H_
25 #define _SAMBA_TIME_H_
32 /* we use 0 here, because (time_t)-1 means error */
37 * we use the INT32_MAX here as on 64 bit systems,
38 * gmtime() fails with INT64_MAX
41 #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
45 /* 64 bit time (100 nanosec) 1601 - cifs6.txt, section 3.5, page 30, 4 byte aligned */
46 typedef uint64_t NTTIME
;
49 External access to time_t_min and time_t_max.
51 time_t get_time_t_max(void);
54 a gettimeofday wrapper
56 void GetTimeOfDay(struct timeval
*tval
);
59 a wrapper to preferably get the monotonic time
61 void clock_gettime_mono(struct timespec
*tp
);
64 a wrapper to preferably get the monotonic time in s
66 time_t time_mono(time_t *t
);
69 interpret an 8 byte "filetime" structure to a time_t
70 It's originally in "100ns units since jan 1st 1601"
72 time_t nt_time_to_unix(NTTIME nt
);
75 put a 8 byte filetime from a time_t
76 This takes GMT as input
78 void unix_to_nt_time(NTTIME
*nt
, time_t t
);
81 check if it's a null unix time
83 bool null_time(time_t t
);
86 check if it's a null NTTIME
88 bool null_nttime(NTTIME t
);
91 put a dos date into a buffer (time/date format)
92 This takes GMT time and puts local time in the buffer
94 void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
);
97 put a dos date into a buffer (date/time format)
98 This takes GMT time and puts local time in the buffer
100 void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
);
103 put a dos 32 bit "unix like" date into a buffer. This routine takes
104 GMT and converts it to LOCAL time before putting it (most SMBs assume
105 localtime for this sort of date)
107 void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
);
110 create a unix date (int GMT) from a dos date (which is actually in
113 time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
);
116 like make_unix_date() but the words are reversed
118 time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
);
121 create a unix GMT date from a dos date in 32 bit "unix like" format
122 these generally arrive as localtimes, with corresponding DST
124 time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
);
127 Return a date and time as a string (optionally with microseconds)
129 format is %Y/%m/%d %H:%M:%S if strftime is available
132 char *timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
);
135 Return the current date and time as a string (optionally with microseconds)
137 format is %Y/%m/%d %H:%M:%S if strftime is available
139 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
);
142 Return a date and time as a string (optionally with microseconds)
144 format is %Y%m%d_%H%M%S or %Y%m%d_%H%M%S_%us
147 char *minimal_timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
);
150 Return the current date and time as a string (optionally with microseconds)
152 format is %Y%m%d_%H%M%S or %Y%m%d_%H%M%S_%us
154 char *current_minimal_timestring(TALLOC_CTX
*ctx
, bool hires
);
157 return a HTTP/1.0 time string
159 char *http_timestring(TALLOC_CTX
*mem_ctx
, time_t t
);
162 Return the date and time as a string
164 format is %a %b %e %X %Y %Z
166 char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
);
169 return a talloced string representing a NTTIME for human consumption
171 const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
);
174 put a NTTIME into a packet
176 void push_nttime(uint8_t *base
, uint16_t offset
, NTTIME t
);
179 pull a NTTIME from a packet
181 NTTIME
pull_nttime(uint8_t *base
, uint16_t offset
);
184 parse a nttime as a large integer in a string and return a NTTIME
186 NTTIME
nttime_from_string(const char *s
);
189 return (tv1 - tv2) in microseconds
191 int64_t usec_time_diff(const struct timeval
*tv1
, const struct timeval
*tv2
);
194 return (tp1 - tp2) in nanoseconds
196 int64_t nsec_time_diff(const struct timespec
*tp1
, const struct timespec
*tp2
);
199 return a zero timeval
201 struct timeval
timeval_zero(void);
204 return true if a timeval is zero
206 bool timeval_is_zero(const struct timeval
*tv
);
209 return a timeval for the current time
211 struct timeval
timeval_current(void);
214 return a timeval struct with the given elements
216 struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
);
219 return a timeval ofs microseconds after tv
221 struct timeval
timeval_add(const struct timeval
*tv
,
222 uint32_t secs
, uint32_t usecs
);
225 return the sum of two timeval structures
227 struct timeval
timeval_sum(const struct timeval
*tv1
,
228 const struct timeval
*tv2
);
231 return a timeval secs/usecs into the future
233 struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
);
236 return a timeval milliseconds into the future
238 struct timeval
timeval_current_ofs_msec(uint32_t msecs
);
241 return a timeval microseconds into the future
243 struct timeval
timeval_current_ofs_usec(uint32_t usecs
);
246 compare two timeval structures.
247 Return -1 if tv1 < tv2
248 Return 0 if tv1 == tv2
249 Return 1 if tv1 > tv2
251 int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
);
254 return true if a timer is in the past
256 bool timeval_expired(const struct timeval
*tv
);
259 return the number of seconds elapsed between two times
261 double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
);
264 return the number of seconds elapsed since a given time
266 double timeval_elapsed(const struct timeval
*tv
);
269 return the number of seconds elapsed between two times
271 double timespec_elapsed2(const struct timespec
*ts1
,
272 const struct timespec
*ts2
);
274 return the number of seconds elapsed since a given time
276 double timespec_elapsed(const struct timespec
*ts
);
279 return the lesser of two timevals
281 struct timeval
timeval_min(const struct timeval
*tv1
,
282 const struct timeval
*tv2
);
285 return the greater of two timevals
287 struct timeval
timeval_max(const struct timeval
*tv1
,
288 const struct timeval
*tv2
);
291 return the difference between two timevals as a timeval
292 if tv1 comes after tv2, then return a zero timeval
293 (this is *tv2 - *tv1)
295 struct timeval
timeval_until(const struct timeval
*tv1
,
296 const struct timeval
*tv2
);
299 convert a timeval to a NTTIME
301 NTTIME
timeval_to_nttime(const struct timeval
*tv
);
304 convert a NTTIME to a timeval
306 void nttime_to_timeval(struct timeval
*tv
, NTTIME t
);
309 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
311 int get_time_zone(time_t t
);
314 check if 2 NTTIMEs are equal.
316 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
);
318 void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
);
320 struct timespec
nt_time_to_unix_timespec(NTTIME nt
);
322 time_t convert_timespec_to_time_t(struct timespec ts
);
324 struct timespec
convert_time_t_to_timespec(time_t t
);
326 bool null_timespec(struct timespec ts
);
328 struct timespec
convert_timeval_to_timespec(const struct timeval tv
);
329 struct timeval
convert_timespec_to_timeval(const struct timespec ts
);
330 struct timespec
timespec_current(void);
331 struct timespec
timespec_min(const struct timespec
*ts1
,
332 const struct timespec
*ts2
);
333 int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
);
334 void round_timespec_to_sec(struct timespec
*ts
);
335 void round_timespec_to_usec(struct timespec
*ts
);
336 NTTIME
unix_timespec_to_nt_time(struct timespec ts
);
338 #endif /* _SAMBA_TIME_H_ */