source: trunk/server/lib/util/time.h@ 751

Last change on this file since 751 was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 7.7 KB
Line 
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 _PUBLIC_
28#define _PUBLIC_
29#endif
30
31#ifndef TIME_T_MIN
32/* we use 0 here, because (time_t)-1 means error */
33#define TIME_T_MIN 0
34#endif
35
36/*
37 * we use the INT32_MAX here as on 64 bit systems,
38 * gmtime() fails with INT64_MAX
39 */
40#ifndef TIME_T_MAX
41#define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
42#endif
43
44
45/* 64 bit time (100 nanosec) 1601 - cifs6.txt, section 3.5, page 30, 4 byte aligned */
46typedef uint64_t NTTIME;
47
48/**
49 External access to time_t_min and time_t_max.
50**/
51_PUBLIC_ time_t get_time_t_max(void);
52
53/**
54a gettimeofday wrapper
55**/
56_PUBLIC_ void GetTimeOfDay(struct timeval *tval);
57
58/**
59a wrapper to preferably get the monotonic time
60**/
61_PUBLIC_ void clock_gettime_mono(struct timespec *tp);
62
63/**
64a wrapper to preferably get the monotonic time in s
65**/
66_PUBLIC_ time_t time_mono(time_t *t);
67
68/**
69interpret an 8 byte "filetime" structure to a time_t
70It's originally in "100ns units since jan 1st 1601"
71**/
72_PUBLIC_ time_t nt_time_to_unix(NTTIME nt);
73
74/**
75put a 8 byte filetime from a time_t
76This takes GMT as input
77**/
78_PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t);
79
80/**
81check if it's a null unix time
82**/
83_PUBLIC_ bool null_time(time_t t);
84
85/**
86check if it's a null NTTIME
87**/
88_PUBLIC_ bool null_nttime(NTTIME t);
89
90/**
91put a dos date into a buffer (time/date format)
92This takes GMT time and puts local time in the buffer
93**/
94_PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset);
95
96/**
97put a dos date into a buffer (date/time format)
98This takes GMT time and puts local time in the buffer
99**/
100_PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset);
101
102/**
103put a dos 32 bit "unix like" date into a buffer. This routine takes
104GMT and converts it to LOCAL time before putting it (most SMBs assume
105localtime for this sort of date)
106**/
107_PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset);
108
109/**
110 create a unix date (int GMT) from a dos date (which is actually in
111 localtime)
112**/
113_PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset);
114
115/**
116like make_unix_date() but the words are reversed
117**/
118_PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset);
119
120/**
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
123**/
124_PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset);
125
126/**
127 Return a date and time as a string (optionally with microseconds)
128
129 format is %Y/%m/%d %H:%M:%S if strftime is available
130**/
131
132char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires);
133
134/**
135 Return the current date and time as a string (optionally with microseconds)
136
137 format is %Y/%m/%d %H:%M:%S if strftime is available
138**/
139char *current_timestring(TALLOC_CTX *ctx, bool hires);
140
141/**
142return a HTTP/1.0 time string
143**/
144_PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t);
145
146/**
147 Return the date and time as a string
148
149 format is %a %b %e %X %Y %Z
150**/
151_PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t);
152
153/**
154 return a talloced string representing a NTTIME for human consumption
155*/
156_PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt);
157
158/**
159 put a NTTIME into a packet
160*/
161_PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t);
162
163/**
164 pull a NTTIME from a packet
165*/
166_PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset);
167
168/**
169 parse a nttime as a large integer in a string and return a NTTIME
170*/
171_PUBLIC_ NTTIME nttime_from_string(const char *s);
172
173/**
174 return (tv1 - tv2) in microseconds
175*/
176_PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2);
177
178/**
179 return (tp1 - tp2) in nanoseconds
180*/
181_PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2);
182
183/**
184 return a zero timeval
185*/
186_PUBLIC_ struct timeval timeval_zero(void);
187
188/**
189 return true if a timeval is zero
190*/
191_PUBLIC_ bool timeval_is_zero(const struct timeval *tv);
192
193/**
194 return a timeval for the current time
195*/
196_PUBLIC_ struct timeval timeval_current(void);
197
198/**
199 return a timeval struct with the given elements
200*/
201_PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs);
202
203/**
204 return a timeval ofs microseconds after tv
205*/
206_PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
207 uint32_t secs, uint32_t usecs);
208
209/**
210 return the sum of two timeval structures
211*/
212struct timeval timeval_sum(const struct timeval *tv1,
213 const struct timeval *tv2);
214
215/**
216 return a timeval secs/usecs into the future
217*/
218_PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs);
219
220/**
221 compare two timeval structures.
222 Return -1 if tv1 < tv2
223 Return 0 if tv1 == tv2
224 Return 1 if tv1 > tv2
225*/
226_PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2);
227
228/**
229 return true if a timer is in the past
230*/
231_PUBLIC_ bool timeval_expired(const struct timeval *tv);
232
233/**
234 return the number of seconds elapsed between two times
235*/
236_PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2);
237
238/**
239 return the number of seconds elapsed since a given time
240*/
241_PUBLIC_ double timeval_elapsed(const struct timeval *tv);
242
243/**
244 return the lesser of two timevals
245*/
246_PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
247 const struct timeval *tv2);
248
249/**
250 return the greater of two timevals
251*/
252_PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
253 const struct timeval *tv2);
254
255/**
256 return the difference between two timevals as a timeval
257 if tv1 comes after tv2, then return a zero timeval
258 (this is *tv2 - *tv1)
259*/
260_PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
261 const struct timeval *tv2);
262
263/**
264 convert a timeval to a NTTIME
265*/
266_PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv);
267
268/**
269 convert a NTTIME to a timeval
270*/
271_PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t);
272
273/**
274 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
275 */
276_PUBLIC_ int get_time_zone(time_t t);
277
278/**
279 check if 2 NTTIMEs are equal.
280*/
281bool nt_time_equal(NTTIME *t1, NTTIME *t2);
282
283void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second);
284
285struct timespec nt_time_to_unix_timespec(NTTIME *nt);
286
287time_t convert_timespec_to_time_t(struct timespec ts);
288
289struct timespec convert_time_t_to_timespec(time_t t);
290
291bool null_timespec(struct timespec ts);
292
293/** Extra minutes to add to the normal GMT to local time conversion. */
294extern int extra_time_offset;
295
296#endif /* _SAMBA_TIME_H_ */
Note: See TracBrowser for help on using the repository browser.