2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
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.
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.
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.
29 #define TIME_T_MAX (~(time_t)0)
32 /*******************************************************************
33 External access to time_t_min and time_t_max.
34 ********************************************************************/
35 time_t get_time_t_max(void)
40 /*******************************************************************
41 a gettimeofday wrapper
42 ********************************************************************/
43 void GetTimeOfDay(struct timeval *tval)
45 #ifdef HAVE_GETTIMEOFDAY_TZ
46 gettimeofday(tval,NULL);
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)
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);
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);
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)
75 struct tm *tm = gmtime(&t);
83 return tm_diff(&tm_utc,tm);
86 #define TIME_FIXUP_CONSTANT 11644473600LL
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)
100 nt += 1000*1000*10/2;
102 nt -= TIME_FIXUP_CONSTANT;
104 if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
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)
120 if (t == (time_t)-1) {
130 t2 += TIME_FIXUP_CONSTANT;
137 /****************************************************************************
138 check if it's a null mtime
139 ****************************************************************************/
140 BOOL null_mtime(time_t mtime)
143 mtime == (time_t)0xFFFFFFFF ||
147 /*******************************************************************
148 create a 16 bit dos packed date
149 ********************************************************************/
150 static uint16_t make_dos_date1(struct tm *t)
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));
158 /*******************************************************************
159 create a 16 bit dos packed time
160 ********************************************************************/
161 static uint16_t make_dos_time1(struct tm *t)
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));
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)
182 unixdate -= zone_offset;
184 t = gmtime(&unixdate);
189 ret = make_dos_date1(t);
190 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
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)
201 uint32_t x = make_dos_date(unixdate, zone_offset);
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)
212 x = make_dos_date(unixdate, zone_offset);
213 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
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)
224 if (!null_mtime(unixdate)) {
225 unixdate -= zone_offset;
227 SIVAL(buf,offset,unixdate);
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)
235 uint32_t p0,p1,p2,p3;
237 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
238 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
240 *second = 2*(p0 & 0x1F);
241 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
242 *hour = (p1>>3)&0xFF;
244 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
245 *year = ((p3>>1)&0xFF) + 80;
248 /*******************************************************************
249 create a unix date (int GMT) from a dos date (which is actually in
251 ********************************************************************/
252 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
258 dos_date = IVAL(date_ptr,0);
260 if (dos_date == 0) return (time_t)0;
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);
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)
280 x = IVAL(date_ptr,0);
281 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
284 return pull_dos_date((void *)&x, zone_offset);
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)
293 time_t t = (time_t)IVAL(date_ptr,0);
294 if (!null_mtime(t)) {
301 /***************************************************************************
302 return a HTTP/1.0 time string
303 ***************************************************************************/
304 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
308 struct tm *tm = localtime(&t);
311 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
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;
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 */
327 /***************************************************************************
328 return a LDAP time string
329 ***************************************************************************/
330 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
332 struct tm *tm = gmtime(&t);
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,
348 /****************************************************************************
349 Return the date and time as a string
350 ****************************************************************************/
351 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
359 return talloc_asprintf(mem_ctx,
360 "%ld seconds since the Epoch",
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.
369 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
370 TimeBuf = talloc_strdup(mem_ctx, tempTime);
372 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
379 return a talloced string representing a NTTIME for human consumption
381 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
383 time_t t = nt_time_to_unix(nt);
384 return talloc_strdup(mem_ctx, timestring(mem_ctx, t));
389 put a NTTIME into a packet
391 void push_nttime(void *base, uint16_t offset, NTTIME t)
393 SBVAL(base, offset, t);
397 pull a NTTIME from a packet
399 NTTIME pull_nttime(void *base, uint16_t offset)
401 NTTIME ret = BVAL(base, offset);
406 parse a nttime as a large integer in a string and return a NTTIME
408 NTTIME nttime_from_string(const char *s)
410 return strtoull(s, NULL, 0);