r4670: abartlet was worried about floating point precision with my first
[gd/samba-autobuild/.git] / source4 / lib / time.c
index 9e6da2cfa696b0e0c92a122ecc0ab5c8581cffe0..fd005215a118c363a59fbce34442c8a086fa3875 100644 (file)
@@ -164,7 +164,7 @@ static uint32_t make_dos_date(time_t unixdate, int zone_offset)
 put a dos date into a buffer (time/date format)
 This takes GMT time and puts local time in the buffer
 ********************************************************************/
-void push_dos_date(char *buf, int offset, time_t unixdate, int zone_offset)
+void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
 {
        uint32_t x = make_dos_date(unixdate, zone_offset);
        SIVAL(buf,offset,x);
@@ -174,7 +174,7 @@ void push_dos_date(char *buf, int offset, time_t unixdate, int zone_offset)
 put a dos date into a buffer (date/time format)
 This takes GMT time and puts local time in the buffer
 ********************************************************************/
-void push_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
+void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
 {
        uint32_t x;
        x = make_dos_date(unixdate, zone_offset);
@@ -187,7 +187,7 @@ put a dos 32 bit "unix like" date into a buffer. This routine takes
 GMT and converts it to LOCAL time before putting it (most SMBs assume
 localtime for this sort of date)
 ********************************************************************/
-void push_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
+void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
 {
        if (!null_time(unixdate)) {
                unixdate -= zone_offset;
@@ -311,16 +311,6 @@ char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
                               tm->tm_sec);
 }
 
-/*
-  a hack to move the stupid gcc strftime warning to one place - see manual page
-*/
-#ifdef HAVE_STRFTIME
-size_t sys_strftime(char *s, size_t max, const char *fmt, const struct tm *tm) 
-{
-       return strftime(s, max, fmt, tm);
-}
-#endif
-
 /****************************************************************************
  Return the date and time as a string
 ****************************************************************************/
@@ -342,7 +332,7 @@ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
           in the gcc warning, not a bug in this code. See a recent
           strftime() manual page for details.
         */
-       sys_strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
+       strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
        TimeBuf = talloc_strdup(mem_ctx, tempTime);
 #else
        TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
@@ -357,14 +347,14 @@ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
 {
        time_t t = nt_time_to_unix(nt);
-       return talloc_strdup(mem_ctx, timestring(mem_ctx, t));
+       return timestring(mem_ctx, t);
 }
 
 
 /*
   put a NTTIME into a packet
 */
-void push_nttime(void *base, uint16_t offset, NTTIME t)
+void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
 {
        SBVAL(base, offset,   t);
 }
@@ -372,7 +362,7 @@ void push_nttime(void *base, uint16_t offset, NTTIME t)
 /*
   pull a NTTIME from a packet
 */
-NTTIME pull_nttime(void *base, uint16_t offset)
+NTTIME pull_nttime(uint8_t *base, uint16_t offset)
 {
        NTTIME ret = BVAL(base, offset);
        return ret;
@@ -486,14 +476,22 @@ BOOL timeval_expired(struct timeval *tv)
        return (tv2.tv_usec >= tv->tv_usec);
 }
 
+/*
+  return the number of seconds elapsed between two times
+*/
+double timeval_elapsed2(struct timeval *tv1, struct timeval *tv2)
+{
+       return (tv2->tv_sec - tv1->tv_sec) + 
+              (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
+}
+
 /*
   return the number of seconds elapsed since a given time
 */
 double timeval_elapsed(struct timeval *tv)
 {
        struct timeval tv2 = timeval_current();
-       return (tv2.tv_sec - tv->tv_sec) + 
-              (tv2.tv_usec - tv->tv_usec)*1.0e-6;
+       return timeval_elapsed2(tv, &tv2);
 }
 
 /*
@@ -507,6 +505,17 @@ struct timeval timeval_min(struct timeval *tv1, struct timeval *tv2)
        return *tv2;
 }
 
+/*
+  return the greater of two timevals
+*/
+struct timeval timeval_max(struct timeval *tv1, struct timeval *tv2)
+{
+       if (tv1->tv_sec > tv2->tv_sec) return *tv1;
+       if (tv1->tv_sec < tv2->tv_sec) return *tv2;
+       if (tv1->tv_usec > tv2->tv_usec) return *tv1;
+       return *tv2;
+}
+
 /*
   return the difference between two timevals as a timeval
   if tv2 comes after tv1, then return a zero timeval
@@ -527,3 +536,13 @@ struct timeval timeval_diff(struct timeval *tv1, struct timeval *tv2)
        }
        return t;
 }
+
+
+/*
+  convert a timeval to a NTTIME
+*/
+NTTIME timeval_to_nttime(struct timeval *tv)
+{
+       return 10*(tv->tv_usec + 
+                 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
+}