source: trunk/samba/source/lib/time.c@ 22

Last change on this file since 22 was 22, checked in by Yuri Dario, 18 years ago

Source code upgrade to 3.0.25pre2.

File size: 36.2 KB
Line 
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 Copyright (C) Jeremy Allison 2007
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include "includes.h"
25
26/**
27 * @file
28 * @brief time handling functions
29 */
30
31
32#ifndef TIME_T_MIN
33#define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
34 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
35#endif
36#ifndef TIME_T_MAX
37#define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
38#endif
39
40#define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
41
42/***************************************************************************
43 External access to time_t_min and time_t_max.
44****************************************************************************/
45
46time_t get_time_t_max(void)
47{
48 return TIME_T_MAX;
49}
50
51/***************************************************************************
52 A gettimeofday wrapper.
53****************************************************************************/
54
55void GetTimeOfDay(struct timeval *tval)
56{
57#ifdef HAVE_GETTIMEOFDAY_TZ
58 gettimeofday(tval,NULL);
59#else
60 gettimeofday(tval);
61#endif
62}
63
64#if (SIZEOF_LONG == 8)
65#define TIME_FIXUP_CONSTANT_INT 11644473600L
66#elif (SIZEOF_LONG_LONG == 8)
67#define TIME_FIXUP_CONSTANT_INT 11644473600LL
68#endif
69
70/****************************************************************************
71 Interpret an 8 byte "filetime" structure to a time_t
72 It's originally in "100ns units since jan 1st 1601"
73
74 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
75
76 tv_sec = 0
77 tv_nsec = 0;
78
79 Returns GMT.
80****************************************************************************/
81
82time_t nt_time_to_unix(NTTIME nt)
83{
84 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt));
85}
86
87/****************************************************************************
88 Put a 8 byte filetime from a time_t. Uses GMT.
89****************************************************************************/
90
91void unix_to_nt_time(NTTIME *nt, time_t t)
92{
93 uint64_t t2;
94
95 if (t == (time_t)-1) {
96 *nt = (NTTIME)-1LL;
97 return;
98 }
99 if (t == 0) {
100 *nt = 0;
101 return;
102 }
103
104 t2 = t;
105 t2 += TIME_FIXUP_CONSTANT_INT;
106 t2 *= 1000*1000*10;
107
108 *nt = t2;
109}
110
111/****************************************************************************
112 Check if it's a null unix time.
113****************************************************************************/
114
115BOOL null_time(time_t t)
116{
117 return t == 0 ||
118 t == (time_t)0xFFFFFFFF ||
119 t == (time_t)-1;
120}
121
122/****************************************************************************
123 Check if it's a null NTTIME.
124****************************************************************************/
125
126BOOL null_nttime(NTTIME t)
127{
128 return t == 0 || t == (NTTIME)-1;
129}
130
131/****************************************************************************
132 Check if it's a null timespec.
133****************************************************************************/
134
135BOOL null_timespec(struct timespec ts)
136{
137 return ts.tv_sec == 0 ||
138 ts.tv_sec == (time_t)0xFFFFFFFF ||
139 ts.tv_sec == (time_t)-1;
140}
141
142/*******************************************************************
143 create a 16 bit dos packed date
144********************************************************************/
145static uint16_t make_dos_date1(struct tm *t)
146{
147 uint16_t ret=0;
148 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
149 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
150 return ret;
151}
152
153/*******************************************************************
154 create a 16 bit dos packed time
155********************************************************************/
156static uint16_t make_dos_time1(struct tm *t)
157{
158 uint16_t ret=0;
159 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
160 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
161 return ret;
162}
163
164/*******************************************************************
165 create a 32 bit dos packed date/time from some parameters
166 This takes a GMT time and returns a packed localtime structure
167********************************************************************/
168static uint32_t make_dos_date(time_t unixdate, int zone_offset)
169{
170 struct tm *t;
171 uint32_t ret=0;
172
173 if (unixdate == 0) {
174 return 0;
175 }
176
177 unixdate -= zone_offset;
178
179 t = gmtime(&unixdate);
180 if (!t) {
181 return 0xFFFFFFFF;
182 }
183
184 ret = make_dos_date1(t);
185 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
186
187 return ret;
188}
189
190/**
191put a dos date into a buffer (time/date format)
192This takes GMT time and puts local time in the buffer
193**/
194void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
195{
196 uint32_t x = make_dos_date(unixdate, zone_offset);
197 SIVAL(buf,offset,x);
198}
199
200/**
201put a dos date into a buffer (date/time format)
202This takes GMT time and puts local time in the buffer
203**/
204void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
205{
206 uint32_t x;
207 x = make_dos_date(unixdate, zone_offset);
208 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
209 SIVAL(buf,offset,x);
210}
211
212/**
213put a dos 32 bit "unix like" date into a buffer. This routine takes
214GMT and converts it to LOCAL time before putting it (most SMBs assume
215localtime for this sort of date)
216**/
217void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
218{
219 if (!null_time(unixdate)) {
220 unixdate -= zone_offset;
221 }
222 SIVAL(buf,offset,unixdate);
223}
224
225/*******************************************************************
226 interpret a 32 bit dos packed date/time to some parameters
227********************************************************************/
228static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
229{
230 uint32_t p0,p1,p2,p3;
231
232 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
233 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
234
235 *second = 2*(p0 & 0x1F);
236 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
237 *hour = (p1>>3)&0xFF;
238 *day = (p2&0x1F);
239 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
240 *year = ((p3>>1)&0xFF) + 80;
241}
242
243/**
244 create a unix date (int GMT) from a dos date (which is actually in
245 localtime)
246**/
247time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
248{
249 uint32_t dos_date=0;
250 struct tm t;
251 time_t ret;
252
253 dos_date = IVAL(date_ptr,0);
254
255 if (dos_date == 0) return (time_t)0;
256
257 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
258 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
259 t.tm_isdst = -1;
260
261 ret = timegm(&t);
262
263 ret += zone_offset;
264
265 return ret;
266}
267
268/**
269like make_unix_date() but the words are reversed
270**/
271time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
272{
273 uint32_t x,x2;
274
275 x = IVAL(date_ptr,0);
276 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
277 SIVAL(&x,0,x2);
278
279 return pull_dos_date((const uint8_t *)&x, zone_offset);
280}
281
282/**
283 create a unix GMT date from a dos date in 32 bit "unix like" format
284 these generally arrive as localtimes, with corresponding DST
285**/
286time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
287{
288 time_t t = (time_t)IVAL(date_ptr,0);
289 if (!null_time(t)) {
290 t += zone_offset;
291 }
292 return t;
293}
294
295/***************************************************************************
296 Return a HTTP/1.0 time string.
297***************************************************************************/
298
299char *http_timestring(time_t t)
300{
301 static fstring buf;
302 struct tm *tm = localtime(&t);
303
304 if (!tm) {
305 slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
306 } else {
307#ifndef HAVE_STRFTIME
308 const char *asct = asctime(tm);
309 fstrcpy(buf, asct ? asct : "unknown");
310 }
311 if(buf[strlen(buf)-1] == '\n') {
312 buf[strlen(buf)-1] = 0;
313#else /* !HAVE_STRFTIME */
314 strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
315#endif /* !HAVE_STRFTIME */
316 }
317 return buf;
318}
319
320
321/**
322 Return the date and time as a string
323**/
324char *timestring(TALLOC_CTX *mem_ctx, time_t t)
325{
326 char *TimeBuf;
327 char tempTime[80];
328 struct tm *tm;
329
330 tm = localtime(&t);
331 if (!tm) {
332 return talloc_asprintf(mem_ctx,
333 "%ld seconds since the Epoch",
334 (long)t);
335 }
336
337#ifdef HAVE_STRFTIME
338 /* some versions of gcc complain about using %c. This is a bug
339 in the gcc warning, not a bug in this code. See a recent
340 strftime() manual page for details.
341 */
342 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
343 TimeBuf = talloc_strdup(mem_ctx, tempTime);
344#else
345 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
346#endif
347
348 return TimeBuf;
349}
350
351/**
352 return a talloced string representing a NTTIME for human consumption
353*/
354const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
355{
356 time_t t;
357 if (nt == 0) {
358 return "NTTIME(0)";
359 }
360 t = nt_time_to_unix(nt);
361 return timestring(mem_ctx, t);
362}
363
364
365/**
366 parse a nttime as a large integer in a string and return a NTTIME
367*/
368NTTIME nttime_from_string(const char *s)
369{
370 return strtoull(s, NULL, 0);
371}
372
373/**
374 return (tv1 - tv2) in microseconds
375*/
376int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
377{
378 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
379 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
380}
381
382
383/**
384 return a zero timeval
385*/
386struct timeval timeval_zero(void)
387{
388 struct timeval tv;
389 tv.tv_sec = 0;
390 tv.tv_usec = 0;
391 return tv;
392}
393
394/**
395 return True if a timeval is zero
396*/
397BOOL timeval_is_zero(const struct timeval *tv)
398{
399 return tv->tv_sec == 0 && tv->tv_usec == 0;
400}
401
402/**
403 return a timeval for the current time
404*/
405struct timeval timeval_current(void)
406{
407 struct timeval tv;
408 GetTimeOfDay(&tv);
409 return tv;
410}
411
412/**
413 return a timeval struct with the given elements
414*/
415struct timeval timeval_set(uint32_t secs, uint32_t usecs)
416{
417 struct timeval tv;
418 tv.tv_sec = secs;
419 tv.tv_usec = usecs;
420 return tv;
421}
422
423
424/**
425 return a timeval ofs microseconds after tv
426*/
427struct timeval timeval_add(const struct timeval *tv,
428 uint32_t secs, uint32_t usecs)
429{
430 struct timeval tv2 = *tv;
431 const unsigned int million = 1000000;
432 tv2.tv_sec += secs;
433 tv2.tv_usec += usecs;
434 tv2.tv_sec += tv2.tv_usec / million;
435 tv2.tv_usec = tv2.tv_usec % million;
436 return tv2;
437}
438
439/**
440 return the sum of two timeval structures
441*/
442struct timeval timeval_sum(const struct timeval *tv1,
443 const struct timeval *tv2)
444{
445 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
446}
447
448/**
449 return a timeval secs/usecs into the future
450*/
451struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
452{
453 struct timeval tv = timeval_current();
454 return timeval_add(&tv, secs, usecs);
455}
456
457/**
458 compare two timeval structures.
459 Return -1 if tv1 < tv2
460 Return 0 if tv1 == tv2
461 Return 1 if tv1 > tv2
462*/
463int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
464{
465 if (tv1->tv_sec > tv2->tv_sec) return 1;
466 if (tv1->tv_sec < tv2->tv_sec) return -1;
467 if (tv1->tv_usec > tv2->tv_usec) return 1;
468 if (tv1->tv_usec < tv2->tv_usec) return -1;
469 return 0;
470}
471
472/**
473 return True if a timer is in the past
474*/
475BOOL timeval_expired(const struct timeval *tv)
476{
477 struct timeval tv2 = timeval_current();
478 if (tv2.tv_sec > tv->tv_sec) return True;
479 if (tv2.tv_sec < tv->tv_sec) return False;
480 return (tv2.tv_usec >= tv->tv_usec);
481}
482
483/**
484 return the number of seconds elapsed between two times
485*/
486double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
487{
488 return (tv2->tv_sec - tv1->tv_sec) +
489 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
490}
491
492/**
493 return the number of seconds elapsed since a given time
494*/
495double timeval_elapsed(const struct timeval *tv)
496{
497 struct timeval tv2 = timeval_current();
498 return timeval_elapsed2(tv, &tv2);
499}
500
501/**
502 return the lesser of two timevals
503*/
504struct timeval timeval_min(const struct timeval *tv1,
505 const struct timeval *tv2)
506{
507 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
508 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
509 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
510 return *tv2;
511}
512
513/**
514 return the greater of two timevals
515*/
516struct timeval timeval_max(const struct timeval *tv1,
517 const struct timeval *tv2)
518{
519 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
520 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
521 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
522 return *tv2;
523}
524
525/**
526 return the difference between two timevals as a timeval
527 if tv1 comes after tv2, then return a zero timeval
528 (this is *tv2 - *tv1)
529*/
530struct timeval timeval_until(const struct timeval *tv1,
531 const struct timeval *tv2)
532{
533 struct timeval t;
534 if (timeval_compare(tv1, tv2) >= 0) {
535 return timeval_zero();
536 }
537 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
538 if (tv1->tv_usec > tv2->tv_usec) {
539 t.tv_sec--;
540 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
541 } else {
542 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
543 }
544 return t;
545}
546
547
548/**
549 convert a timeval to a NTTIME
550*/
551NTTIME timeval_to_nttime(const struct timeval *tv)
552{
553 return 10*(tv->tv_usec +
554 ((TIME_FIXUP_CONSTANT_INT + (uint64_t)tv->tv_sec) * 1000000));
555}
556
557/*******************************************************************
558 Yield the difference between *A and *B, in seconds, ignoring leap seconds.
559********************************************************************/
560
561static int tm_diff(struct tm *a, struct tm *b)
562{
563 int ay = a->tm_year + (1900 - 1);
564 int by = b->tm_year + (1900 - 1);
565 int intervening_leap_days =
566 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
567 int years = ay - by;
568 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
569 int hours = 24*days + (a->tm_hour - b->tm_hour);
570 int minutes = 60*hours + (a->tm_min - b->tm_min);
571 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
572
573 return seconds;
574}
575
576int extra_time_offset=0;
577
578/*******************************************************************
579 Return the UTC offset in seconds west of UTC, or 0 if it cannot be determined.
580********************************************************************/
581
582int get_time_zone(time_t t)
583{
584 struct tm *tm = gmtime(&t);
585 struct tm tm_utc;
586 if (!tm)
587 return 0;
588 tm_utc = *tm;
589 tm = localtime(&t);
590 if (!tm)
591 return 0;
592 return tm_diff(&tm_utc,tm)+60*extra_time_offset;
593}
594
595/****************************************************************************
596 Check if NTTIME is 0.
597****************************************************************************/
598
599BOOL nt_time_is_zero(const NTTIME *nt)
600{
601 return (*nt == 0);
602}
603
604/****************************************************************************
605 Convert ASN.1 GeneralizedTime string to unix-time.
606 Returns 0 on failure; Currently ignores timezone.
607****************************************************************************/
608
609time_t generalized_to_unix_time(const char *str)
610{
611 struct tm tm;
612
613 ZERO_STRUCT(tm);
614
615 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
616 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
617 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
618 return 0;
619 }
620 tm.tm_year -= 1900;
621 tm.tm_mon -= 1;
622
623 return timegm(&tm);
624}
625
626/*******************************************************************
627 Accessor function for the server time zone offset.
628 set_server_zone_offset() must have been called first.
629******************************************************************/
630
631static int server_zone_offset;
632
633int get_server_zone_offset(void)
634{
635 return server_zone_offset;
636}
637
638/*******************************************************************
639 Initialize the server time zone offset. Called when a client connects.
640******************************************************************/
641
642int set_server_zone_offset(time_t t)
643{
644 server_zone_offset = get_time_zone(t);
645 return server_zone_offset;
646}
647
648/****************************************************************************
649 Return the date and time as a string
650****************************************************************************/
651
652char *current_timestring(BOOL hires)
653{
654 static fstring TimeBuf;
655 struct timeval tp;
656 time_t t;
657 struct tm *tm;
658
659 if (hires) {
660 GetTimeOfDay(&tp);
661 t = (time_t)tp.tv_sec;
662 } else {
663 t = time(NULL);
664 }
665 tm = localtime(&t);
666 if (!tm) {
667 if (hires) {
668 slprintf(TimeBuf,
669 sizeof(TimeBuf)-1,
670 "%ld.%06ld seconds since the Epoch",
671 (long)tp.tv_sec,
672 (long)tp.tv_usec);
673 } else {
674 slprintf(TimeBuf,
675 sizeof(TimeBuf)-1,
676 "%ld seconds since the Epoch",
677 (long)t);
678 }
679 } else {
680#ifdef HAVE_STRFTIME
681 if (hires) {
682 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
683 slprintf(TimeBuf+strlen(TimeBuf),
684 sizeof(TimeBuf)-1 - strlen(TimeBuf),
685 ".%06ld",
686 (long)tp.tv_usec);
687 } else {
688 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
689 }
690#else
691 if (hires) {
692 const char *asct = asctime(tm);
693 slprintf(TimeBuf,
694 sizeof(TimeBuf)-1,
695 "%s.%06ld",
696 asct ? asct : "unknown",
697 (long)tp.tv_usec);
698 } else {
699 const char *asct = asctime(tm);
700 fstrcpy(TimeBuf, asct ? asct : "unknown");
701 }
702#endif
703 }
704 return(TimeBuf);
705}
706
707
708/*******************************************************************
709 Put a dos date into a buffer (time/date format).
710 This takes GMT time and puts local time in the buffer.
711********************************************************************/
712
713static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
714{
715 uint32 x = make_dos_date(unixdate, zone_offset);
716 SIVAL(buf,offset,x);
717}
718
719/*******************************************************************
720 Put a dos date into a buffer (date/time format).
721 This takes GMT time and puts local time in the buffer.
722********************************************************************/
723
724static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
725{
726 uint32 x = make_dos_date(unixdate, zone_offset);
727 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
728 SIVAL(buf,offset,x);
729}
730
731/*******************************************************************
732 Put a dos 32 bit "unix like" date into a buffer. This routine takes
733 GMT and converts it to LOCAL time before putting it (most SMBs assume
734 localtime for this sort of date)
735********************************************************************/
736
737static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
738{
739 if (!null_mtime(unixdate)) {
740 unixdate -= zone_offset;
741 }
742 SIVAL(buf,offset,unixdate);
743}
744
745
746/***************************************************************************
747 Server versions of the above functions.
748***************************************************************************/
749
750void srv_put_dos_date(char *buf,int offset,time_t unixdate)
751{
752 put_dos_date(buf, offset, unixdate, server_zone_offset);
753}
754
755void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
756{
757 put_dos_date2(buf, offset, unixdate, server_zone_offset);
758}
759
760void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
761{
762 put_dos_date3(buf, offset, unixdate, server_zone_offset);
763}
764
765/****************************************************************************
766 Take a Unix time and convert to an NTTIME structure and place in buffer
767 pointed to by p.
768****************************************************************************/
769
770void put_long_date_timespec(char *p, struct timespec ts)
771{
772 NTTIME nt;
773 unix_timespec_to_nt_time(&nt, ts);
774 SIVAL(p, 0, nt & 0xFFFFFFFF);
775 SIVAL(p, 4, nt >> 32);
776}
777
778void put_long_date(char *p, time_t t)
779{
780 struct timespec ts;
781 ts.tv_sec = t;
782 ts.tv_nsec = 0;
783 put_long_date_timespec(p, ts);
784}
785
786/****************************************************************************
787 Return the best approximation to a 'create time' under UNIX from a stat
788 structure.
789****************************************************************************/
790
791time_t get_create_time(const SMB_STRUCT_STAT *st,BOOL fake_dirs)
792{
793 time_t ret, ret1;
794
795 if(S_ISDIR(st->st_mode) && fake_dirs) {
796 return (time_t)315493200L; /* 1/1/1980 */
797 }
798
799 ret = MIN(st->st_ctime, st->st_mtime);
800 ret1 = MIN(ret, st->st_atime);
801
802 if(ret1 != (time_t)0) {
803 return ret1;
804 }
805
806 /*
807 * One of ctime, mtime or atime was zero (probably atime).
808 * Just return MIN(ctime, mtime).
809 */
810 return ret;
811}
812
813struct timespec get_create_timespec(const SMB_STRUCT_STAT *st,BOOL fake_dirs)
814{
815 struct timespec ts;
816 ts.tv_sec = get_create_time(st, fake_dirs);
817 ts.tv_nsec = 0;
818 return ts;
819}
820
821/****************************************************************************
822 Get/Set all the possible time fields from a stat struct as a timespec.
823****************************************************************************/
824
825struct timespec get_atimespec(const SMB_STRUCT_STAT *pst)
826{
827#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
828 struct timespec ret;
829
830 /* Old system - no ns timestamp. */
831 ret.tv_sec = pst->st_atime;
832 ret.tv_nsec = 0;
833 return ret;
834#else
835#if defined(HAVE_STAT_ST_ATIM)
836 return pst->st_atim;
837#elif defined(HAVE_STAT_ST_ATIMENSEC)
838 struct timespec ret;
839 ret.tv_sec = pst->st_atime;
840 ret.tv_nsec = pst->st_atimensec;
841 return ret;
842#else
843#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
844#endif
845#endif
846}
847
848void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
849{
850#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
851 /* Old system - no ns timestamp. */
852 pst->st_atime = ts.tv_sec;
853#else
854#if defined(HAVE_STAT_ST_ATIM)
855 pst->st_atim = ts;
856#elif defined(HAVE_STAT_ST_ATIMENSEC)
857 pst->st_atime = ts.tv_sec;
858 pst->st_atimensec = ts.tv_nsec
859#else
860#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
861#endif
862#endif
863}
864
865struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
866{
867#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
868 struct timespec ret;
869
870 /* Old system - no ns timestamp. */
871 ret.tv_sec = pst->st_mtime;
872 ret.tv_nsec = 0;
873 return ret;
874#else
875#if defined(HAVE_STAT_ST_MTIM)
876 return pst->st_mtim;
877#elif defined(HAVE_STAT_ST_MTIMENSEC)
878 struct timespec ret;
879 ret.tv_sec = pst->st_mtime;
880 ret.tv_nsec = pst->st_mtimensec;
881 return ret;
882#else
883#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
884#endif
885#endif
886}
887
888void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
889{
890#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
891 /* Old system - no ns timestamp. */
892 pst->st_mtime = ts.tv_sec;
893#else
894#if defined(HAVE_STAT_ST_MTIM)
895 pst->st_mtim = ts;
896#elif defined(HAVE_STAT_ST_MTIMENSEC)
897 pst->st_mtime = ts.tv_sec;
898 pst->st_mtimensec = ts.tv_nsec
899#else
900#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
901#endif
902#endif
903}
904
905struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
906{
907#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
908 struct timespec ret;
909
910 /* Old system - no ns timestamp. */
911 ret.tv_sec = pst->st_ctime;
912 ret.tv_nsec = 0;
913 return ret;
914#else
915#if defined(HAVE_STAT_ST_CTIM)
916 return pst->st_ctim;
917#elif defined(HAVE_STAT_ST_CTIMENSEC)
918 struct timespec ret;
919 ret.tv_sec = pst->st_ctime;
920 ret.tv_nsec = pst->st_ctimensec;
921 return ret;
922#else
923#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
924#endif
925#endif
926}
927
928void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
929{
930#if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
931 /* Old system - no ns timestamp. */
932 pst->st_ctime = ts.tv_sec;
933#else
934#if defined(HAVE_STAT_ST_CTIM)
935 pst->st_ctim = ts;
936#elif defined(HAVE_STAT_ST_CTIMENSEC)
937 pst->st_ctime = ts.tv_sec;
938 pst->st_ctimensec = ts.tv_nsec
939#else
940#error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
941#endif
942#endif
943}
944
945void dos_filetime_timespec(struct timespec *tsp)
946{
947 tsp->tv_sec &= ~1;
948 tsp->tv_nsec = 0;
949}
950
951/*******************************************************************
952 Create a unix date (int GMT) from a dos date (which is actually in
953 localtime).
954********************************************************************/
955
956static time_t make_unix_date(const void *date_ptr, int zone_offset)
957{
958 uint32 dos_date=0;
959 struct tm t;
960 time_t ret;
961
962 dos_date = IVAL(date_ptr,0);
963
964 if (dos_date == 0) {
965 return 0;
966 }
967
968 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
969 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
970 t.tm_isdst = -1;
971
972 ret = timegm(&t);
973
974 ret += zone_offset;
975
976 return(ret);
977}
978
979/*******************************************************************
980 Like make_unix_date() but the words are reversed.
981********************************************************************/
982
983static time_t make_unix_date2(const void *date_ptr, int zone_offset)
984{
985 uint32 x,x2;
986
987 x = IVAL(date_ptr,0);
988 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
989 SIVAL(&x,0,x2);
990
991 return(make_unix_date((const void *)&x, zone_offset));
992}
993
994/*******************************************************************
995 Create a unix GMT date from a dos date in 32 bit "unix like" format
996 these generally arrive as localtimes, with corresponding DST.
997******************************************************************/
998
999static time_t make_unix_date3(const void *date_ptr, int zone_offset)
1000{
1001 time_t t = (time_t)IVAL(date_ptr,0);
1002 if (!null_mtime(t)) {
1003 t += zone_offset;
1004 }
1005 return(t);
1006}
1007
1008time_t srv_make_unix_date(const void *date_ptr)
1009{
1010 return make_unix_date(date_ptr, server_zone_offset);
1011}
1012
1013time_t srv_make_unix_date2(const void *date_ptr)
1014{
1015 return make_unix_date2(date_ptr, server_zone_offset);
1016}
1017
1018time_t srv_make_unix_date3(const void *date_ptr)
1019{
1020 return make_unix_date3(date_ptr, server_zone_offset);
1021}
1022
1023time_t convert_timespec_to_time_t(struct timespec ts)
1024{
1025 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1026 increment if it's greater than 500 millionth of a second. */
1027 if (ts.tv_nsec > 500000000) {
1028 return ts.tv_sec + 1;
1029 }
1030 return ts.tv_sec;
1031}
1032
1033struct timespec convert_time_t_to_timespec(time_t t)
1034{
1035 struct timespec ts;
1036 ts.tv_sec = t;
1037 ts.tv_nsec = 0;
1038 return ts;
1039}
1040
1041/****************************************************************************
1042 Convert a normalized timeval to a timespec.
1043****************************************************************************/
1044
1045struct timespec convert_timeval_to_timespec(const struct timeval tv)
1046{
1047 struct timespec ts;
1048 ts.tv_sec = tv.tv_sec;
1049 ts.tv_nsec = tv.tv_usec * 1000;
1050 return ts;
1051}
1052
1053/****************************************************************************
1054 Convert a normalized timespec to a timeval.
1055****************************************************************************/
1056
1057struct timeval convert_timespec_to_timeval(const struct timespec ts)
1058{
1059 struct timeval tv;
1060 tv.tv_sec = ts.tv_sec;
1061 tv.tv_usec = ts.tv_nsec / 1000;
1062 return tv;
1063}
1064
1065/****************************************************************************
1066 Return a timespec for the current time
1067****************************************************************************/
1068
1069struct timespec timespec_current(void)
1070{
1071 struct timeval tv;
1072 struct timespec ts;
1073 GetTimeOfDay(&tv);
1074 ts.tv_sec = tv.tv_sec;
1075 ts.tv_nsec = tv.tv_sec * 1000;
1076 return ts;
1077}
1078
1079/****************************************************************************
1080 Return the lesser of two timespecs.
1081****************************************************************************/
1082
1083struct timespec timespec_min(const struct timespec *ts1,
1084 const struct timespec *ts2)
1085{
1086 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
1087 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
1088 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
1089 return *ts2;
1090}
1091
1092/****************************************************************************
1093 compare two timespec structures.
1094 Return -1 if ts1 < ts2
1095 Return 0 if ts1 == ts2
1096 Return 1 if ts1 > ts2
1097****************************************************************************/
1098
1099int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
1100{
1101 if (ts1->tv_sec > ts2->tv_sec) return 1;
1102 if (ts1->tv_sec < ts2->tv_sec) return -1;
1103 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
1104 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
1105 return 0;
1106}
1107
1108/****************************************************************************
1109 Interprets an nt time into a unix struct timespec.
1110 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
1111 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
1112****************************************************************************/
1113
1114struct timespec interpret_long_date(const char *p)
1115{
1116 NTTIME nt;
1117 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
1118 if (nt == (uint64_t)-1) {
1119 struct timespec ret;
1120 ret.tv_sec = (time_t)-1;
1121 ret.tv_nsec = 0;
1122 return ret;
1123 }
1124 return nt_time_to_unix_timespec(&nt);
1125}
1126
1127/***************************************************************************
1128 Client versions of the above functions.
1129***************************************************************************/
1130
1131void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
1132{
1133 put_dos_date(buf, offset, unixdate, cli->serverzone);
1134}
1135
1136void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
1137{
1138 put_dos_date2(buf, offset, unixdate, cli->serverzone);
1139}
1140
1141void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
1142{
1143 put_dos_date3(buf, offset, unixdate, cli->serverzone);
1144}
1145
1146time_t cli_make_unix_date(struct cli_state *cli, void *date_ptr)
1147{
1148 return make_unix_date(date_ptr, cli->serverzone);
1149}
1150
1151time_t cli_make_unix_date2(struct cli_state *cli, void *date_ptr)
1152{
1153 return make_unix_date2(date_ptr, cli->serverzone);
1154}
1155
1156time_t cli_make_unix_date3(struct cli_state *cli, void *date_ptr)
1157{
1158 return make_unix_date3(date_ptr, cli->serverzone);
1159}
1160
1161/* Large integer version. */
1162struct timespec nt_time_to_unix_timespec(NTTIME *nt)
1163{
1164 int64 d;
1165 struct timespec ret;
1166
1167 if (*nt == 0 || *nt == (int64)-1) {
1168 ret.tv_sec = 0;
1169 ret.tv_nsec = 0;
1170 return ret;
1171 }
1172
1173 d = (int64)*nt;
1174 /* d is now in 100ns units, since jan 1st 1601".
1175 Save off the ns fraction. */
1176
1177 ret.tv_nsec = (long) ((d % 100) * 100);
1178
1179 /* Convert to seconds */
1180 d /= 1000*1000*10;
1181
1182 /* Now adjust by 369 years to make the secs since 1970 */
1183 d -= TIME_FIXUP_CONSTANT_INT;
1184
1185 if (d <= (int64)TIME_T_MIN) {
1186 ret.tv_sec = TIME_T_MIN;
1187 ret.tv_nsec = 0;
1188 return ret;
1189 }
1190
1191 if (d >= (int64)TIME_T_MAX) {
1192 ret.tv_sec = TIME_T_MAX;
1193 ret.tv_nsec = 0;
1194 return ret;
1195 }
1196
1197 ret.tv_sec = (time_t)d;
1198 return ret;
1199}
1200/****************************************************************************
1201 Check if two NTTIMEs are the same.
1202****************************************************************************/
1203
1204BOOL nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
1205{
1206 return (*nt1 == *nt2);
1207}
1208
1209/*******************************************************************
1210 Re-read the smb serverzone value.
1211******************************************************************/
1212
1213static struct timeval start_time_hires;
1214
1215void TimeInit(void)
1216{
1217 set_server_zone_offset(time(NULL));
1218
1219 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
1220
1221 /* Save the start time of this process. */
1222 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
1223 GetTimeOfDay(&start_time_hires);
1224 }
1225}
1226
1227/**********************************************************************
1228 Return a timeval struct of the uptime of this process. As TimeInit is
1229 done before a daemon fork then this is the start time from the parent
1230 daemon start. JRA.
1231***********************************************************************/
1232
1233void get_process_uptime(struct timeval *ret_time)
1234{
1235 struct timeval time_now_hires;
1236
1237 GetTimeOfDay(&time_now_hires);
1238 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
1239 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
1240 ret_time->tv_sec -= 1;
1241 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
1242 } else {
1243 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
1244 }
1245}
1246
1247/****************************************************************************
1248 Convert a NTTIME structure to a time_t.
1249 It's originally in "100ns units".
1250
1251 This is an absolute version of the one above.
1252 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
1253 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
1254****************************************************************************/
1255
1256time_t nt_time_to_unix_abs(const NTTIME *nt)
1257{
1258 uint64 d;
1259
1260 if (*nt == 0) {
1261 return (time_t)0;
1262 }
1263
1264 if (*nt == (uint64)-1) {
1265 return (time_t)-1;
1266 }
1267
1268 if (*nt == NTTIME_INFINITY) {
1269 return (time_t)-1;
1270 }
1271
1272 /* reverse the time */
1273 /* it's a negative value, turn it to positive */
1274 d=~*nt;
1275
1276 d += 1000*1000*10/2;
1277 d /= 1000*1000*10;
1278
1279 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
1280 return (time_t)0;
1281 }
1282
1283 return (time_t)d;
1284}
1285
1286/****************************************************************************
1287 Put a 8 byte filetime from a struct timespec. Uses GMT.
1288****************************************************************************/
1289
1290void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
1291{
1292 uint64 d;
1293
1294 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
1295 *nt = 0;
1296 return;
1297 }
1298 if (ts.tv_sec == TIME_T_MAX) {
1299 *nt = 0x7fffffffffffffffLL;
1300 return;
1301 }
1302 if (ts.tv_sec == (time_t)-1) {
1303 *nt = (uint64)-1;
1304 return;
1305 }
1306
1307 d = ts.tv_sec;
1308 d += TIME_FIXUP_CONSTANT_INT;
1309 d *= 1000*1000*10;
1310 /* d is now in 100ns units. */
1311 d += (ts.tv_nsec / 100);
1312
1313 *nt = d;
1314}
1315
1316/****************************************************************************
1317 Convert a time_t to a NTTIME structure
1318
1319 This is an absolute version of the one above.
1320 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
1321 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
1322****************************************************************************/
1323
1324void unix_to_nt_time_abs(NTTIME *nt, time_t t)
1325{
1326 double d;
1327
1328 if (t==0) {
1329 *nt = 0;
1330 return;
1331 }
1332
1333 if (t == TIME_T_MAX) {
1334 *nt = 0x7fffffffffffffffLL;
1335 return;
1336 }
1337
1338 if (t == (time_t)-1) {
1339 /* that's what NT uses for infinite */
1340 *nt = NTTIME_INFINITY;
1341 return;
1342 }
1343
1344 d = (double)(t);
1345 d *= 1.0e7;
1346
1347 *nt = d;
1348
1349 /* convert to a negative value */
1350 *nt=~*nt;
1351}
1352
1353
1354/****************************************************************************
1355 Check if it's a null mtime.
1356****************************************************************************/
1357
1358BOOL null_mtime(time_t mtime)
1359{
1360 if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
1361 return(True);
1362 return(False);
1363}
1364
1365/****************************************************************************
1366 Utility function that always returns a const string even if localtime
1367 and asctime fail.
1368****************************************************************************/
1369
1370const char *time_to_asc(const time_t t)
1371{
1372 const char *asct;
1373 struct tm *lt = localtime(&t);
1374
1375 if (!lt) {
1376 return "unknown time";
1377 }
1378
1379 asct = asctime(lt);
1380 if (!asct) {
1381 return "unknown time";
1382 }
1383 return asct;
1384}
1385
1386const char *display_time(NTTIME nttime)
1387{
1388 static fstring string;
1389
1390 float high;
1391 float low;
1392 int sec;
1393 int days, hours, mins, secs;
1394
1395 if (nttime==0)
1396 return "Now";
1397
1398 if (nttime==NTTIME_INFINITY)
1399 return "Never";
1400
1401 high = 65536;
1402 high = high/10000;
1403 high = high*65536;
1404 high = high/1000;
1405 high = high * (~(nttime >> 32));
1406
1407 low = ~(nttime & 0xFFFFFFFF);
1408 low = low/(1000*1000*10);
1409
1410 sec=high+low;
1411
1412 days=sec/(60*60*24);
1413 hours=(sec - (days*60*60*24)) / (60*60);
1414 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
1415 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
1416
1417 fstr_sprintf(string, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
1418 return (string);
1419}
1420
1421BOOL nt_time_is_set(const NTTIME *nt)
1422{
1423 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
1424 return False;
1425 }
1426
1427 if (*nt == NTTIME_INFINITY) {
1428 return False;
1429 }
1430
1431 return True;
1432}
Note: See TracBrowser for help on using the repository browser.