r3323: more warning reductions
[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 #define TIME_FIXUP_CONSTANT 11644473600LL
54
55 /****************************************************************************
56 interpret an 8 byte "filetime" structure to a time_t
57 It's originally in "100ns units since jan 1st 1601"
58 ****************************************************************************/
59 time_t nt_time_to_unix(NTTIME nt)
60 {
61         if (nt == 0) {
62                 return 0;
63         }
64         if (nt == -1LL) {
65                 return (time_t)-1;
66         }
67         nt += 1000*1000*10/2;
68         nt /= 1000*1000*10;
69         nt -= TIME_FIXUP_CONSTANT;
70
71         if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
72                 return 0;
73         }
74
75         return (time_t)nt;
76 }
77
78
79 /****************************************************************************
80 put a 8 byte filetime from a time_t
81 This takes GMT as input
82 ****************************************************************************/
83 void unix_to_nt_time(NTTIME *nt, time_t t)
84 {
85         uint64_t t2; 
86
87         if (t == (time_t)-1) {
88                 *nt = (NTTIME)-1LL;
89                 return;
90         }               
91         if (t == 0) {
92                 *nt = 0;
93                 return;
94         }               
95
96         t2 = t;
97         t2 += TIME_FIXUP_CONSTANT;
98         t2 *= 1000*1000*10;
99
100         *nt = t2;
101 }
102
103
104 /****************************************************************************
105 check if it's a null unix time
106 ****************************************************************************/
107 BOOL null_time(time_t t)
108 {
109         return t == 0 || 
110                 t == (time_t)0xFFFFFFFF || 
111                 t == (time_t)-1;
112 }
113
114 /*******************************************************************
115   create a 16 bit dos packed date
116 ********************************************************************/
117 static uint16_t make_dos_date1(struct tm *t)
118 {
119         uint16_t ret=0;
120         ret = (((uint_t)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
121         ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
122         return ret;
123 }
124
125 /*******************************************************************
126   create a 16 bit dos packed time
127 ********************************************************************/
128 static uint16_t make_dos_time1(struct tm *t)
129 {
130         uint16_t ret=0;
131         ret = ((((uint_t)t->tm_min >> 3)&0x7) | (((uint_t)t->tm_hour) << 3));
132         ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
133         return ret;
134 }
135
136 /*******************************************************************
137   create a 32 bit dos packed date/time from some parameters
138   This takes a GMT time and returns a packed localtime structure
139 ********************************************************************/
140 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
141 {
142         struct tm *t;
143         uint32_t ret=0;
144
145         if (unixdate == 0) {
146                 return 0;
147         }
148
149         unixdate -= zone_offset;
150
151         t = gmtime(&unixdate);
152         if (!t) {
153                 return 0xFFFFFFFF;
154         }
155
156         ret = make_dos_date1(t);
157         ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
158
159         return ret;
160 }
161
162 /*******************************************************************
163 put a dos date into a buffer (time/date format)
164 This takes GMT time and puts local time in the buffer
165 ********************************************************************/
166 void push_dos_date(char *buf, int offset, time_t unixdate, int zone_offset)
167 {
168         uint32_t x = make_dos_date(unixdate, zone_offset);
169         SIVAL(buf,offset,x);
170 }
171
172 /*******************************************************************
173 put a dos date into a buffer (date/time format)
174 This takes GMT time and puts local time in the buffer
175 ********************************************************************/
176 void push_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
177 {
178         uint32_t x;
179         x = make_dos_date(unixdate, zone_offset);
180         x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
181         SIVAL(buf,offset,x);
182 }
183
184 /*******************************************************************
185 put a dos 32 bit "unix like" date into a buffer. This routine takes
186 GMT and converts it to LOCAL time before putting it (most SMBs assume
187 localtime for this sort of date)
188 ********************************************************************/
189 void push_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
190 {
191         if (!null_time(unixdate)) {
192                 unixdate -= zone_offset;
193         }
194         SIVAL(buf,offset,unixdate);
195 }
196
197 /*******************************************************************
198   interpret a 32 bit dos packed date/time to some parameters
199 ********************************************************************/
200 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
201 {
202         uint32_t p0,p1,p2,p3;
203
204         p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
205         p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
206
207         *second = 2*(p0 & 0x1F);
208         *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
209         *hour = (p1>>3)&0xFF;
210         *day = (p2&0x1F);
211         *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
212         *year = ((p3>>1)&0xFF) + 80;
213 }
214
215 /*******************************************************************
216   create a unix date (int GMT) from a dos date (which is actually in
217   localtime)
218 ********************************************************************/
219 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
220 {
221         uint32_t dos_date=0;
222         struct tm t;
223         time_t ret;
224
225         dos_date = IVAL(date_ptr,0);
226
227         if (dos_date == 0) return (time_t)0;
228   
229         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
230                            &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
231         t.tm_isdst = -1;
232   
233         ret = timegm(&t);
234
235         ret += zone_offset;
236
237         return ret;
238 }
239
240 /*******************************************************************
241 like make_unix_date() but the words are reversed
242 ********************************************************************/
243 time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
244 {
245         uint32_t x,x2;
246
247         x = IVAL(date_ptr,0);
248         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
249         SIVAL(&x,0,x2);
250
251         return pull_dos_date((void *)&x, zone_offset);
252 }
253
254 /*******************************************************************
255   create a unix GMT date from a dos date in 32 bit "unix like" format
256   these generally arrive as localtimes, with corresponding DST
257   ******************************************************************/
258 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
259 {
260         time_t t = (time_t)IVAL(date_ptr,0);
261         if (!null_time(t)) {
262                 t += zone_offset;
263         }
264         return t;
265 }
266
267
268 /***************************************************************************
269 return a HTTP/1.0 time string
270   ***************************************************************************/
271 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
272 {
273         char *buf;
274         char tempTime[60];
275         struct tm *tm = localtime(&t);
276
277         if (!tm) {
278                 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
279         }
280
281 #ifndef HAVE_STRFTIME
282         buf = talloc_strdup(mem_ctx, asctime(tm));
283         if (buf[strlen(buf)-1] == '\n') {
284                 buf[strlen(buf)-1] = 0;
285         }
286 #else
287         strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
288         buf = talloc_strdup(mem_ctx, tempTime);
289 #endif /* !HAVE_STRFTIME */
290
291         return buf;
292 }
293
294 /***************************************************************************
295 return a LDAP time string
296   ***************************************************************************/
297 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
298 {
299         struct tm *tm = gmtime(&t);
300
301         if (!tm) {
302                 return NULL;
303         }
304
305         /* formatted like: 20040408072012.0Z */
306         return talloc_asprintf(mem_ctx, 
307                                "%04u%02u%02u%02u%02u%02u.0Z",
308                                tm->tm_year+1900, tm->tm_mon+1,
309                                tm->tm_mday, tm->tm_hour, tm->tm_min,
310                                tm->tm_sec);
311 }
312
313 /*
314   a hack to move the stupid gcc strftime warning to one place - see manual page
315 */
316 #ifdef HAVE_STRFTIME
317 size_t sys_strftime(char *s, size_t max, const char *fmt, const struct tm *tm) 
318 {
319         return strftime(s, max, fmt, tm);
320 }
321 #endif
322
323 /****************************************************************************
324  Return the date and time as a string
325 ****************************************************************************/
326 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
327 {
328         char *TimeBuf;
329         char tempTime[80];
330         struct tm *tm;
331
332         tm = localtime(&t);
333         if (!tm) {
334                 return talloc_asprintf(mem_ctx,
335                                        "%ld seconds since the Epoch",
336                                        (long)t);
337         }
338
339 #ifdef HAVE_STRFTIME
340         /* some versions of gcc complain about using %c. This is a bug
341            in the gcc warning, not a bug in this code. See a recent
342            strftime() manual page for details.
343          */
344         sys_strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
345         TimeBuf = talloc_strdup(mem_ctx, tempTime);
346 #else
347         TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
348 #endif
349
350         return TimeBuf;
351 }
352
353 /*
354   return a talloced string representing a NTTIME for human consumption
355 */
356 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
357 {
358         time_t t = nt_time_to_unix(nt);
359         return talloc_strdup(mem_ctx, timestring(mem_ctx, t));
360 }
361
362
363 /*
364   put a NTTIME into a packet
365 */
366 void push_nttime(void *base, uint16_t offset, NTTIME t)
367 {
368         SBVAL(base, offset,   t);
369 }
370
371 /*
372   pull a NTTIME from a packet
373 */
374 NTTIME pull_nttime(void *base, uint16_t offset)
375 {
376         NTTIME ret = BVAL(base, offset);
377         return ret;
378 }
379
380 /*
381   parse a nttime as a large integer in a string and return a NTTIME
382 */
383 NTTIME nttime_from_string(const char *s)
384 {
385         return strtoull(s, NULL, 0);
386 }
387
388 int64_t usec_time_diff(struct timeval *larget, struct timeval *smallt)
389 {
390         int64_t sec_diff = larget->tv_sec - smallt->tv_sec;
391         return (sec_diff * 1000000) + (int64_t)(larget->tv_usec - smallt->tv_usec);
392 }
393