9d906af2fae8d5ae24999776ba6555f8db4f5e20
[bbaumbach/samba-autobuild/.git] / source4 / lib / time.c
1 /* 
2    Unix SMB/CIFS implementation.
3    time handling functions
4
5    Copyright (C) Andrew Tridgell                1992-2004
6    Copyright (C) Stefan (metze) Metzmacher      2002   
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #ifndef TIME_T_MIN
26 #define TIME_T_MIN 0
27 #endif
28 #ifndef TIME_T_MAX
29 #define TIME_T_MAX (~(time_t)0)
30 #endif
31
32 /*******************************************************************
33  External access to time_t_min and time_t_max.
34 ********************************************************************/
35 time_t get_time_t_max(void)
36 {
37         return TIME_T_MAX;
38 }
39
40 /*******************************************************************
41 a gettimeofday wrapper
42 ********************************************************************/
43 void GetTimeOfDay(struct timeval *tval)
44 {
45 #ifdef HAVE_GETTIMEOFDAY_TZ
46         gettimeofday(tval,NULL);
47 #else
48         gettimeofday(tval);
49 #endif
50 }
51
52 /*******************************************************************
53 yield the difference between *A and *B, in seconds, ignoring leap seconds
54 ********************************************************************/
55 static int tm_diff(struct tm *a, struct tm *b)
56 {
57         int ay = a->tm_year + (1900 - 1);
58         int by = b->tm_year + (1900 - 1);
59         int intervening_leap_days =
60                 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
61         int years = ay - by;
62         int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
63         int hours = 24*days + (a->tm_hour - b->tm_hour);
64         int minutes = 60*hours + (a->tm_min - b->tm_min);
65         int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
66
67         return seconds;
68 }
69
70 /*******************************************************************
71   return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
72   ******************************************************************/
73 int get_time_zone(time_t t)
74 {
75         struct tm *tm = gmtime(&t);
76         struct tm tm_utc;
77         if (!tm)
78                 return 0;
79         tm_utc = *tm;
80         tm = localtime(&t);
81         if (!tm)
82                 return 0;
83         return tm_diff(&tm_utc,tm);
84 }
85
86 #define TIME_FIXUP_CONSTANT 11644473600LL
87
88 /****************************************************************************
89 interpret an 8 byte "filetime" structure to a time_t
90 It's originally in "100ns units since jan 1st 1601"
91 ****************************************************************************/
92 time_t nt_time_to_unix(NTTIME nt)
93 {
94         if (nt == 0) {
95                 return 0;
96         }
97         if (nt == -1LL) {
98                 return (time_t)-1;
99         }
100         nt += 1000*1000*10/2;
101         nt /= 1000*1000*10;
102         nt -= TIME_FIXUP_CONSTANT;
103
104         if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
105                 return 0;
106         }
107
108         return (time_t)nt;
109 }
110
111
112 /****************************************************************************
113 put a 8 byte filetime from a time_t
114 This takes GMT as input
115 ****************************************************************************/
116 void unix_to_nt_time(NTTIME *nt, time_t t)
117 {
118         uint64_t t2; 
119
120         if (t == (time_t)-1) {
121                 *nt = (NTTIME)-1LL;
122                 return;
123         }               
124         if (t == 0) {
125                 *nt = 0;
126                 return;
127         }               
128
129         t2 = t;
130         t2 += TIME_FIXUP_CONSTANT;
131         t2 *= 1000*1000*10;
132
133         *nt = t2;
134 }
135
136
137 /****************************************************************************
138 check if it's a null mtime
139 ****************************************************************************/
140 BOOL null_mtime(time_t mtime)
141 {
142         return mtime == 0 || 
143                 mtime == (time_t)0xFFFFFFFF || 
144                 mtime == (time_t)-1;
145 }
146
147 /*******************************************************************
148   create a 16 bit dos packed date
149 ********************************************************************/
150 static uint16_t make_dos_date1(struct tm *t)
151 {
152         uint16_t ret=0;
153         ret = (((uint_t)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
154         ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
155         return ret;
156 }
157
158 /*******************************************************************
159   create a 16 bit dos packed time
160 ********************************************************************/
161 static uint16_t make_dos_time1(struct tm *t)
162 {
163         uint16_t ret=0;
164         ret = ((((uint_t)t->tm_min >> 3)&0x7) | (((uint_t)t->tm_hour) << 3));
165         ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
166         return ret;
167 }
168
169 /*******************************************************************
170   create a 32 bit dos packed date/time from some parameters
171   This takes a GMT time and returns a packed localtime structure
172 ********************************************************************/
173 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
174 {
175         struct tm *t;
176         uint32_t ret=0;
177
178         if (unixdate == 0) {
179                 return 0;
180         }
181
182         unixdate -= zone_offset;
183
184         t = gmtime(&unixdate);
185         if (!t) {
186                 return 0xFFFFFFFF;
187         }
188
189         ret = make_dos_date1(t);
190         ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
191
192         return ret;
193 }
194
195 /*******************************************************************
196 put a dos date into a buffer (time/date format)
197 This takes GMT time and puts local time in the buffer
198 ********************************************************************/
199 void push_dos_date(char *buf, int offset, time_t unixdate, int zone_offset)
200 {
201         uint32_t x = make_dos_date(unixdate, zone_offset);
202         SIVAL(buf,offset,x);
203 }
204
205 /*******************************************************************
206 put a dos date into a buffer (date/time format)
207 This takes GMT time and puts local time in the buffer
208 ********************************************************************/
209 void push_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
210 {
211         uint32_t x;
212         x = make_dos_date(unixdate, zone_offset);
213         x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
214         SIVAL(buf,offset,x);
215 }
216
217 /*******************************************************************
218 put a dos 32 bit "unix like" date into a buffer. This routine takes
219 GMT and converts it to LOCAL time before putting it (most SMBs assume
220 localtime for this sort of date)
221 ********************************************************************/
222 void push_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
223 {
224         if (!null_mtime(unixdate)) {
225                 unixdate -= zone_offset;
226         }
227         SIVAL(buf,offset,unixdate);
228 }
229
230 /*******************************************************************
231   interpret a 32 bit dos packed date/time to some parameters
232 ********************************************************************/
233 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
234 {
235         uint32_t p0,p1,p2,p3;
236
237         p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
238         p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
239
240         *second = 2*(p0 & 0x1F);
241         *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
242         *hour = (p1>>3)&0xFF;
243         *day = (p2&0x1F);
244         *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
245         *year = ((p3>>1)&0xFF) + 80;
246 }
247
248 /*******************************************************************
249   create a unix date (int GMT) from a dos date (which is actually in
250   localtime)
251 ********************************************************************/
252 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
253 {
254         uint32_t dos_date=0;
255         struct tm t;
256         time_t ret;
257
258         dos_date = IVAL(date_ptr,0);
259
260         if (dos_date == 0) return (time_t)0;
261   
262         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
263                            &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
264         t.tm_isdst = -1;
265   
266         ret = timegm(&t);
267
268         ret += zone_offset;
269
270         return ret;
271 }
272
273 /*******************************************************************
274 like make_unix_date() but the words are reversed
275 ********************************************************************/
276 time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
277 {
278         uint32_t x,x2;
279
280         x = IVAL(date_ptr,0);
281         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
282         SIVAL(&x,0,x2);
283
284         return pull_dos_date((void *)&x, zone_offset);
285 }
286
287 /*******************************************************************
288   create a unix GMT date from a dos date in 32 bit "unix like" format
289   these generally arrive as localtimes, with corresponding DST
290   ******************************************************************/
291 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
292 {
293         time_t t = (time_t)IVAL(date_ptr,0);
294         if (!null_mtime(t)) {
295                 t += zone_offset;
296         }
297         return t;
298 }
299
300
301 /***************************************************************************
302 return a HTTP/1.0 time string
303   ***************************************************************************/
304 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
305 {
306         char *buf;
307         char tempTime[60];
308         struct tm *tm = localtime(&t);
309
310         if (!tm) {
311                 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
312         }
313
314 #ifndef HAVE_STRFTIME
315         buf = talloc_strdup(mem_ctx, asctime(tm));
316         if (buf[strlen(buf)-1] == '\n') {
317                 buf[strlen(buf)-1] = 0;
318         }
319 #else
320         strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
321         buf = talloc_strdup(mem_ctx, tempTime);
322 #endif /* !HAVE_STRFTIME */
323
324         return buf;
325 }
326
327 /***************************************************************************
328 return a LDAP time string
329   ***************************************************************************/
330 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
331 {
332         struct tm *tm = gmtime(&t);
333
334         if (!tm) {
335                 return NULL;
336         }
337
338         /* formatted like: 20040408072012.0Z */
339         return talloc_asprintf(mem_ctx, 
340                                "%04u%02u%02u%02u%02u%02u.0Z",
341                                tm->tm_year+1900, tm->tm_mon+1,
342                                tm->tm_mday, tm->tm_hour, tm->tm_min,
343                                tm->tm_sec);
344 }
345
346
347
348 /****************************************************************************
349  Return the date and time as a string
350 ****************************************************************************/
351 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
352 {
353         char *TimeBuf;
354         char tempTime[80];
355         struct tm *tm;
356
357         tm = localtime(&t);
358         if (!tm) {
359                 return talloc_asprintf(mem_ctx,
360                                        "%ld seconds since the Epoch",
361                                        (long)t);
362         }
363
364 #ifdef HAVE_STRFTIME
365         /* some versions of gcc complain about using %c. This is a bug
366            in the gcc warning, not a bug in this code. See a recent
367            strftime() manual page for details.
368          */
369         strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
370         TimeBuf = talloc_strdup(mem_ctx, tempTime);
371 #else
372         TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
373 #endif
374
375         return TimeBuf;
376 }
377
378 /*
379   return a talloced string representing a NTTIME for human consumption
380 */
381 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
382 {
383         time_t t = nt_time_to_unix(nt);
384         return talloc_strdup(mem_ctx, timestring(mem_ctx, t));
385 }
386
387
388 /*
389   put a NTTIME into a packet
390 */
391 void push_nttime(void *base, uint16_t offset, NTTIME t)
392 {
393         SBVAL(base, offset,   t);
394 }
395
396 /*
397   pull a NTTIME from a packet
398 */
399 NTTIME pull_nttime(void *base, uint16_t offset)
400 {
401         NTTIME ret = BVAL(base, offset);
402         return ret;
403 }
404
405 /*
406   parse a nttime as a large integer in a string and return a NTTIME
407 */
408 NTTIME nttime_from_string(const char *s)
409 {
410         return strtoull(s, NULL, 0);
411 }
412
413 long long usec_time_diff(struct timeval *larget, struct timeval *smallt)
414 {
415         long long sec_diff = larget->tv_sec - smallt->tv_sec;
416         return (sec_diff * 1000000) + (long long)(larget->tv_usec - smallt->tv_usec);
417 }
418