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);
53 #define TIME_FIXUP_CONSTANT 11644473600LL
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)
69 nt -= TIME_FIXUP_CONSTANT;
71 if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
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)
87 if (t == (time_t)-1) {
97 t2 += TIME_FIXUP_CONSTANT;
104 /****************************************************************************
105 check if it's a null unix time
106 ****************************************************************************/
107 BOOL null_time(time_t t)
110 t == (time_t)0xFFFFFFFF ||
114 /*******************************************************************
115 create a 16 bit dos packed date
116 ********************************************************************/
117 static uint16_t make_dos_date1(struct tm *t)
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));
125 /*******************************************************************
126 create a 16 bit dos packed time
127 ********************************************************************/
128 static uint16_t make_dos_time1(struct tm *t)
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));
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)
149 unixdate -= zone_offset;
151 t = gmtime(&unixdate);
156 ret = make_dos_date1(t);
157 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
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)
168 uint32_t x = make_dos_date(unixdate, zone_offset);
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)
179 x = make_dos_date(unixdate, zone_offset);
180 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
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)
191 if (!null_time(unixdate)) {
192 unixdate -= zone_offset;
194 SIVAL(buf,offset,unixdate);
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)
202 uint32_t p0,p1,p2,p3;
204 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
205 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
207 *second = 2*(p0 & 0x1F);
208 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
209 *hour = (p1>>3)&0xFF;
211 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
212 *year = ((p3>>1)&0xFF) + 80;
215 /*******************************************************************
216 create a unix date (int GMT) from a dos date (which is actually in
218 ********************************************************************/
219 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
225 dos_date = IVAL(date_ptr,0);
227 if (dos_date == 0) return (time_t)0;
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);
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)
247 x = IVAL(date_ptr,0);
248 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
251 return pull_dos_date((void *)&x, zone_offset);
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)
260 time_t t = (time_t)IVAL(date_ptr,0);
268 /***************************************************************************
269 return a HTTP/1.0 time string
270 ***************************************************************************/
271 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
275 struct tm *tm = localtime(&t);
278 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
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;
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 */
294 /***************************************************************************
295 return a LDAP time string
296 ***************************************************************************/
297 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
299 struct tm *tm = gmtime(&t);
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,
314 a hack to move the stupid gcc strftime warning to one place - see manual page
317 size_t sys_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
319 return strftime(s, max, fmt, tm);
323 /****************************************************************************
324 Return the date and time as a string
325 ****************************************************************************/
326 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
334 return talloc_asprintf(mem_ctx,
335 "%ld seconds since the Epoch",
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.
344 sys_strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
345 TimeBuf = talloc_strdup(mem_ctx, tempTime);
347 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
354 return a talloced string representing a NTTIME for human consumption
356 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
358 time_t t = nt_time_to_unix(nt);
359 return talloc_strdup(mem_ctx, timestring(mem_ctx, t));
364 put a NTTIME into a packet
366 void push_nttime(void *base, uint16_t offset, NTTIME t)
368 SBVAL(base, offset, t);
372 pull a NTTIME from a packet
374 NTTIME pull_nttime(void *base, uint16_t offset)
376 NTTIME ret = BVAL(base, offset);
381 parse a nttime as a large integer in a string and return a NTTIME
383 NTTIME nttime_from_string(const char *s)
385 return strtoull(s, NULL, 0);
388 int64_t usec_time_diff(struct timeval *larget, struct timeval *smallt)
390 int64_t sec_diff = larget->tv_sec - smallt->tv_sec;
391 return (sec_diff * 1000000) + (int64_t)(larget->tv_usec - smallt->tv_usec);