s3-time: fix build warnings after we moved to shared time functions.
[ira/wip.git] / source3 / lib / time.c
index f6ff6d3407480020389dd1911c5e7157e51e63ec..dffc03b1cf7b0b31dc40703bb213aae9306aeb64 100644 (file)
  */
 
 
-#ifndef TIME_T_MIN
-#define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
-                   : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
-#endif
-#ifndef TIME_T_MAX
-#define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
-#endif
-
 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
 
 #if (SIZEOF_LONG == 8)
 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
 #endif
 
-/*******************************************************************
-  create a 16 bit dos packed date
-********************************************************************/
-static uint16_t make_dos_date1(struct tm *t)
-{
-       uint16_t ret=0;
-       ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
-       ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
-       return ret;
-}
-
-/*******************************************************************
-  create a 16 bit dos packed time
-********************************************************************/
-static uint16_t make_dos_time1(struct tm *t)
-{
-       uint16_t ret=0;
-       ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
-       ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
-       return ret;
-}
-
-/*******************************************************************
-  create a 32 bit dos packed date/time from some parameters
-  This takes a GMT time and returns a packed localtime structure
-********************************************************************/
-static uint32_t make_dos_date(time_t unixdate, int zone_offset)
-{
-       struct tm *t;
-       uint32_t ret=0;
-
-       if (unixdate == 0) {
-               return 0;
-       }
-
-       unixdate -= zone_offset;
-
-       t = gmtime(&unixdate);
-       if (!t) {
-               return 0xFFFFFFFF;
-       }
-
-       ret = make_dos_date1(t);
-       ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
-
-       return ret;
-}
 
 /**
   parse a nttime as a large integer in a string and return a NTTIME
@@ -188,27 +133,21 @@ int set_server_zone_offset(time_t t)
  Return the date and time as a string
 ****************************************************************************/
 
-char *current_timestring(TALLOC_CTX *ctx, bool hires)
+char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
 {
        fstring TimeBuf;
-       struct timeval tp;
        time_t t;
        struct tm *tm;
 
-       if (hires) {
-               GetTimeOfDay(&tp);
-               t = (time_t)tp.tv_sec;
-       } else {
-               t = time(NULL);
-       }
+       t = (time_t)tp->tv_sec;
        tm = localtime(&t);
        if (!tm) {
                if (hires) {
                        slprintf(TimeBuf,
                                 sizeof(TimeBuf)-1,
                                 "%ld.%06ld seconds since the Epoch",
-                                (long)tp.tv_sec, 
-                                (long)tp.tv_usec);
+                                (long)tp->tv_sec,
+                                (long)tp->tv_usec);
                } else {
                        slprintf(TimeBuf,
                                 sizeof(TimeBuf)-1,
@@ -222,7 +161,7 @@ char *current_timestring(TALLOC_CTX *ctx, bool hires)
                        slprintf(TimeBuf+strlen(TimeBuf),
                                 sizeof(TimeBuf)-1 - strlen(TimeBuf), 
                                 ".%06ld", 
-                                (long)tp.tv_usec);
+                                (long)tp->tv_usec);
                } else {
                        strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
                }
@@ -233,7 +172,7 @@ char *current_timestring(TALLOC_CTX *ctx, bool hires)
                                 sizeof(TimeBuf)-1, 
                                 "%s.%06ld", 
                                 asct ? asct : "unknown", 
-                                (long)tp.tv_usec);
+                                (long)tp->tv_usec);
                } else {
                        const char *asct = asctime(tm);
                        fstrcpy(TimeBuf, asct ? asct : "unknown");
@@ -243,43 +182,14 @@ char *current_timestring(TALLOC_CTX *ctx, bool hires)
        return talloc_strdup(ctx, TimeBuf);
 }
 
-
-/*******************************************************************
- Put a dos date into a buffer (time/date format).
- This takes GMT time and puts local time in the buffer.
-********************************************************************/
-
-static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
+char *current_timestring(TALLOC_CTX *ctx, bool hires)
 {
-       uint32_t x = make_dos_date(unixdate, zone_offset);
-       SIVAL(buf,offset,x);
-}
-
-/*******************************************************************
- Put a dos date into a buffer (date/time format).
- This takes GMT time and puts local time in the buffer.
-********************************************************************/
+       struct timeval tv;
 
-static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
-{
-       uint32_t x = make_dos_date(unixdate, zone_offset);
-       x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
-       SIVAL(buf,offset,x);
+       GetTimeOfDay(&tv);
+       return timeval_string(ctx, &tv, hires);
 }
 
-/*******************************************************************
- 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)
-********************************************************************/
-
-static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
-{
-       if (!null_mtime(unixdate)) {
-               unixdate -= zone_offset;
-       }
-       SIVAL(buf,offset,unixdate);
-}
 
 
 /***************************************************************************
@@ -288,17 +198,17 @@ static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
 
 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
 {
-       put_dos_date(buf, offset, unixdate, server_zone_offset);
+       push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
 }
 
 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
 {
-       put_dos_date2(buf, offset, unixdate, server_zone_offset);
+       push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
 }
 
 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
 {
-       put_dos_date3(buf, offset, unixdate, server_zone_offset);
+       push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
 }
 
 void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
@@ -395,7 +305,7 @@ time_t make_unix_date2(const void *date_ptr, int zone_offset)
 time_t make_unix_date3(const void *date_ptr, int zone_offset)
 {
        time_t t = (time_t)IVAL(date_ptr,0);
-       if (!null_mtime(t)) {
+       if (!null_time(t)) {
                t += zone_offset;
        }
        return(t);
@@ -529,17 +439,17 @@ struct timespec interpret_long_date(const char *p)
 
 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
 {
-       put_dos_date(buf, offset, unixdate, cli->serverzone);
+       push_dos_date((uint8_t *)buf, offset, unixdate, cli->serverzone);
 }
 
 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
 {
-       put_dos_date2(buf, offset, unixdate, cli->serverzone);
+       push_dos_date2((uint8_t *)buf, offset, unixdate, cli->serverzone);
 }
 
 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
 {
-       put_dos_date3(buf, offset, unixdate, cli->serverzone);
+       push_dos_date3((uint8_t *)buf, offset, unixdate, cli->serverzone);
 }
 
 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
@@ -557,14 +467,6 @@ time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
        return make_unix_date3(date_ptr, cli->serverzone);
 }
 
-/****************************************************************************
- Check if two NTTIMEs are the same.
-****************************************************************************/
-
-bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
-{
-       return (*nt1 == *nt2);
-}
 
 /*******************************************************************
  Re-read the smb serverzone value.
@@ -680,6 +582,33 @@ void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
        *nt = d;
 }
 
+#if 0
+void nt_time_to_unix_timespec(struct timespec *ts, NTTIME t)
+{
+       if (ts == NULL) {
+               return;
+       }
+
+       /* t starts in 100 nsec units since 1601-01-01. */
+
+       t *= 100;
+       /* t is now in nsec units since 1601-01-01. */
+
+       t -= TIME_FIXUP_CONSTANT*1000*1000*100;
+       /* t is now in nsec units since the UNIX epoch 1970-01-01. */
+
+       ts->tv_sec  = t / 1000000000LL;
+
+       if (TIME_T_MIN > ts->tv_sec || ts->tv_sec > TIME_T_MAX) {
+               ts->tv_sec  = 0;
+               ts->tv_nsec = 0;
+               return;
+       }
+
+       ts->tv_nsec = t - ts->tv_sec*1000000000LL;
+}
+#endif
+
 /****************************************************************************
  Convert a time_t to a NTTIME structure
 
@@ -718,17 +647,6 @@ void unix_to_nt_time_abs(NTTIME *nt, time_t t)
 }
 
 
-/****************************************************************************
- Check if it's a null mtime.
-****************************************************************************/
-
-bool null_mtime(time_t mtime)
-{
-       if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
-               return true;
-       return false;
-}
-
 /****************************************************************************
  Utility function that always returns a const string even if localtime
  and asctime fail.