lib/util/time: Merge time functions from source3/lib/time.c
authorAndrew Bartlett <abartlet@samba.org>
Tue, 22 Feb 2011 06:59:51 +0000 (17:59 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 1 Mar 2011 01:13:22 +0000 (02:13 +0100)
lib/util/time.c
lib/util/time.h
source3/lib/time.c

index 770ebc43745101a181d4a6464c309242f8dd6969..4843fc9697200de423317bbcd632c948d5c5b3ca 100644 (file)
@@ -4,6 +4,8 @@
 
    Copyright (C) Andrew Tridgell               1992-2004
    Copyright (C) Stefan (metze) Metzmacher     2002   
+   Copyright (C) Jeremy Allison                        2007
+   Copyright (C) Andrew Bartlett                2011
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -337,6 +339,63 @@ _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
 }
 
 
+/****************************************************************************
+ Return the date and time as a string
+****************************************************************************/
+
+char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
+{
+       time_t t;
+       struct tm *tm;
+
+       t = (time_t)tp->tv_sec;
+       tm = localtime(&t);
+       if (!tm) {
+               if (hires) {
+                       return talloc_asprintf(ctx,
+                                              "%ld.%06ld seconds since the Epoch",
+                                              (long)tp->tv_sec,
+                                              (long)tp->tv_usec);
+               } else {
+                       return talloc_asprintf(ctx,
+                                              "%ld seconds since the Epoch",
+                                              (long)t);
+               }
+       } else {
+#ifdef HAVE_STRFTIME
+               char TimeBuf[60];
+               if (hires) {
+                       strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
+                       return talloc_asprintf(ctx,
+                                              "%s.%06ld", TimeBuf,
+                                              (long)tp->tv_usec);
+               } else {
+                       strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
+                       return talloc_strdup(ctx, TimeBuf);
+               }
+#else
+               if (hires) {
+                       const char *asct = asctime(tm);
+                       return talloc_asprintf(ctx, "%s.%06ld",
+                                       asct ? asct : "unknown",
+                                       (long)tp->tv_usec);
+               } else {
+                       const char *asct = asctime(tm);
+                       return talloc_asprintf(ctx, asct ? asct : "unknown");
+               }
+#endif
+       }
+}
+
+char *current_timestring(TALLOC_CTX *ctx, bool hires)
+{
+       struct timeval tv;
+
+       GetTimeOfDay(&tv);
+       return timeval_string(ctx, &tv, hires);
+}
+
+
 /**
 return a HTTP/1.0 time string
 **/
index 345382b80a0893b30128e346f9c26c13fe7f744f..3a406340f45287e9f5333b16cc7da1c0a9aa7224 100644 (file)
@@ -118,6 +118,21 @@ _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset);
 **/
 _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset);
 
+/**
+ Return a date and time as a string (optionally with microseconds)
+
+ format is %Y/%m/%d %H:%M:%S if strftime is available
+**/
+
+char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires);
+
+/**
+ Return the current date and time as a string (optionally with microseconds)
+
+ format is %Y/%m/%d %H:%M:%S if strftime is available
+**/
+char *current_timestring(TALLOC_CTX *ctx, bool hires);
+
 /**
 return a HTTP/1.0 time string
 **/
@@ -125,6 +140,8 @@ _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t);
 
 /**
  Return the date and time as a string
+
+ format is %a %b %e %X %Y %Z
 **/
 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t);
 
index eba358f11fdcd11c9d5b21ee5a24575faf552730..db9ec0a34fb7bd6a5234336f28d6e1aa073fac03 100644 (file)
@@ -129,69 +129,6 @@ int set_server_zone_offset(time_t t)
        return server_zone_offset;
 }
 
-/****************************************************************************
- Return the date and time as a string
-****************************************************************************/
-
-char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
-{
-       fstring TimeBuf;
-       time_t t;
-       struct tm *tm;
-
-       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);
-               } else {
-                       slprintf(TimeBuf,
-                                sizeof(TimeBuf)-1,
-                                "%ld seconds since the Epoch",
-                                (long)t);
-               }
-       } else {
-#ifdef HAVE_STRFTIME
-               if (hires) {
-                       strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
-                       slprintf(TimeBuf+strlen(TimeBuf),
-                                sizeof(TimeBuf)-1 - strlen(TimeBuf), 
-                                ".%06ld", 
-                                (long)tp->tv_usec);
-               } else {
-                       strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
-               }
-#else
-               if (hires) {
-                       const char *asct = asctime(tm);
-                       slprintf(TimeBuf, 
-                                sizeof(TimeBuf)-1, 
-                                "%s.%06ld", 
-                                asct ? asct : "unknown", 
-                                (long)tp->tv_usec);
-               } else {
-                       const char *asct = asctime(tm);
-                       fstrcpy(TimeBuf, asct ? asct : "unknown");
-               }
-#endif
-       }
-       return talloc_strdup(ctx, TimeBuf);
-}
-
-char *current_timestring(TALLOC_CTX *ctx, bool hires)
-{
-       struct timeval tv;
-
-       GetTimeOfDay(&tv);
-       return timeval_string(ctx, &tv, hires);
-}
-
-
-
 /***************************************************************************
  Server versions of the above functions.
 ***************************************************************************/