From 608c8e787253f01b24f95c1523187a0a37737691 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Tue, 22 Feb 2011 17:59:51 +1100 Subject: lib/util/time: Merge time functions from source3/lib/time.c --- lib/util/time.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/time.h | 17 +++++++++++++++++ 2 files changed, 76 insertions(+) (limited to 'lib') 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 @@ -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); -- cgit