diff options
author | Andrew Bartlett <abartlet@samba.org> | 2011-02-22 17:59:51 +1100 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2011-03-01 02:13:22 +0100 |
commit | 608c8e787253f01b24f95c1523187a0a37737691 (patch) | |
tree | e2af06e52df5d4aee47a8c0efb55f650b6725efe /lib | |
parent | db11e6505e3205700fe323c550a72df825830f4b (diff) | |
download | samba-608c8e787253f01b24f95c1523187a0a37737691.tar.gz samba-608c8e787253f01b24f95c1523187a0a37737691.tar.bz2 samba-608c8e787253f01b24f95c1523187a0a37737691.zip |
lib/util/time: Merge time functions from source3/lib/time.c
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/time.c | 59 | ||||
-rw-r--r-- | lib/util/time.h | 17 |
2 files changed, 76 insertions, 0 deletions
diff --git a/lib/util/time.c b/lib/util/time.c index 770ebc4374..4843fc9697 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -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 **/ diff --git a/lib/util/time.h b/lib/util/time.h index 345382b80a..3a406340f4 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -119,12 +119,29 @@ _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 **/ _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); |