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)
96 nt -= TIME_FIXUP_CONSTANT;
98 if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
106 /****************************************************************************
107 put a 8 byte filetime from a time_t
108 This takes GMT as input
109 ****************************************************************************/
110 void unix_to_nt_time(NTTIME *nt, time_t t)
114 if (t == (time_t)-1) {
124 t2 += TIME_FIXUP_CONSTANT;
131 /****************************************************************************
132 check if it's a null mtime
133 ****************************************************************************/
134 BOOL null_mtime(time_t mtime)
137 mtime == (time_t)0xFFFFFFFF ||
141 /*******************************************************************
142 create a 16 bit dos packed date
143 ********************************************************************/
144 static uint16_t make_dos_date1(struct tm *t)
147 ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
148 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
152 /*******************************************************************
153 create a 16 bit dos packed time
154 ********************************************************************/
155 static uint16_t make_dos_time1(struct tm *t)
158 ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
159 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
163 /*******************************************************************
164 create a 32 bit dos packed date/time from some parameters
165 This takes a GMT time and returns a packed localtime structure
166 ********************************************************************/
167 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
176 unixdate -= zone_offset;
178 t = gmtime(&unixdate);
183 ret = make_dos_date1(t);
184 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
189 /*******************************************************************
190 put a dos date into a buffer (time/date format)
191 This takes GMT time and puts local time in the buffer
192 ********************************************************************/
193 void push_dos_date(char *buf, int offset, time_t unixdate, int zone_offset)
195 uint32_t x = make_dos_date(unixdate, zone_offset);
199 /*******************************************************************
200 put a dos date into a buffer (date/time format)
201 This takes GMT time and puts local time in the buffer
202 ********************************************************************/
203 void push_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
206 x = make_dos_date(unixdate, zone_offset);
207 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
211 /*******************************************************************
212 put a dos 32 bit "unix like" date into a buffer. This routine takes
213 GMT and converts it to LOCAL time before putting it (most SMBs assume
214 localtime for this sort of date)
215 ********************************************************************/
216 void push_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
218 if (!null_mtime(unixdate)) {
219 unixdate -= zone_offset;
221 SIVAL(buf,offset,unixdate);
224 /*******************************************************************
225 interpret a 32 bit dos packed date/time to some parameters
226 ********************************************************************/
227 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
229 uint32_t p0,p1,p2,p3;
231 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
232 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
234 *second = 2*(p0 & 0x1F);
235 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
236 *hour = (p1>>3)&0xFF;
238 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
239 *year = ((p3>>1)&0xFF) + 80;
242 /*******************************************************************
243 create a unix date (int GMT) from a dos date (which is actually in
245 ********************************************************************/
246 time_t pull_dos_date(const uint8 *date_ptr, int zone_offset)
252 dos_date = IVAL(date_ptr,0);
254 if (dos_date == 0) return (time_t)0;
256 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
257 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
267 /*******************************************************************
268 like make_unix_date() but the words are reversed
269 ********************************************************************/
270 time_t pull_dos_date2(const uint8 *date_ptr, int zone_offset)
274 x = IVAL(date_ptr,0);
275 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
278 return pull_dos_date((void *)&x, zone_offset);
281 /*******************************************************************
282 create a unix GMT date from a dos date in 32 bit "unix like" format
283 these generally arrive as localtimes, with corresponding DST
284 ******************************************************************/
285 time_t pull_dos_date3(const uint8 *date_ptr, int zone_offset)
287 time_t t = (time_t)IVAL(date_ptr,0);
288 if (!null_mtime(t)) {
295 /***************************************************************************
296 return a HTTP/1.0 time string
297 ***************************************************************************/
298 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
302 struct tm *tm = localtime(&t);
305 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
308 #ifndef HAVE_STRFTIME
309 buf = talloc_strdup(mem_ctx, asctime(tm));
310 if (buf[strlen(buf)-1] == '\n') {
311 buf[strlen(buf)-1] = 0;
314 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
315 buf = talloc_strdup(mem_ctx, tempTime);
316 #endif /* !HAVE_STRFTIME */
321 /***************************************************************************
322 return a LDAP time string
323 ***************************************************************************/
324 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
326 struct tm *tm = gmtime(&t);
332 /* formatted like: 20040408072012.0Z */
333 return talloc_asprintf(mem_ctx,
334 "%04u%02u%02u%02u%02u%02u.0Z",
335 tm->tm_year+1900, tm->tm_mon+1,
336 tm->tm_mday, tm->tm_hour, tm->tm_min,
342 /****************************************************************************
343 Return the date and time as a string
344 ****************************************************************************/
345 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
353 return talloc_asprintf(mem_ctx,
354 "%ld seconds since the Epoch",
359 /* some versions of gcc complain about using %c. This is a bug
360 in the gcc warning, not a bug in this code. See a recent
361 strftime() manual page for details.
363 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
364 TimeBuf = talloc_strdup(mem_ctx, tempTime);
366 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
373 return a talloced string representing a NTTIME for human consumption
375 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
377 time_t t = nt_time_to_unix(nt);
378 return talloc_strdup(mem_ctx, timestring(mem_ctx, t));
383 put a NTTIME into a packet
385 void push_nttime(void *base, uint16_t offset, NTTIME t)
387 SBVAL(base, offset, t);
391 pull a NTTIME from a packet
393 NTTIME pull_nttime(void *base, uint16_t offset)
395 NTTIME ret = BVAL(base, offset);
400 parse a nttime as a large integer in a string and return a NTTIME
402 NTTIME nttime_from_string(const char *s)
404 return strtoull(s, NULL, 0);