From 4dc3f89eed4c7e716873df2daf65fb8bd2bcc314 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Thu, 22 Feb 2007 14:33:20 +0000 Subject: [PATCH] Fixed silc_time_value, added silc_timezone. --- lib/silcutil/silctime.c | 30 ++++++++++++++++++++--- lib/silcutil/silctime.h | 18 ++++++++++++++ lib/silcutil/tests/Makefile.am | 3 ++- lib/silcutil/tests/test_silctime.c | 38 ++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 lib/silcutil/tests/test_silctime.c diff --git a/lib/silcutil/silctime.c b/lib/silcutil/silctime.c index 7619f99e..4444e4e8 100644 --- a/lib/silcutil/silctime.c +++ b/lib/silcutil/silctime.c @@ -114,13 +114,12 @@ SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time) if (!time_val) time_val = silc_time_msec(); - msec = time_val % 1000; - time_val /= 1000; + msec = (SilcUInt64)time_val % (SilcUInt64)1000; + timeval = (time_t)((SilcUInt64)time_val / (SilcUInt64)1000); time = localtime(&timeval); if (!time) return FALSE; - time_val = timeval; memset(ret_time, 0, sizeof(*ret_time)); if (!silc_time_fill(ret_time, time->tm_year + 1900, time->tm_mon + 1, @@ -148,6 +147,31 @@ SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time) return TRUE; } +/* Returns timezone */ + +SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size) +{ + SilcTimeStruct curtime; + + if (timezone_size < 6) + return FALSE; + + if (!silc_time_value(0, &curtime)) + return FALSE; + + if (!curtime.utc_hour && curtime.utc_minute) + silc_snprintf(timezone, timezone_size, "Z"); + else if (curtime.utc_minute) + silc_snprintf(timezone, timezone_size, "%c%02d:%02d", + curtime.utc_east ? '+' : '-', curtime.utc_hour, + curtime.utc_minute); + else + silc_snprintf(timezone, timezone_size, "%c%02d", + curtime.utc_east ? '+' : '-', curtime.utc_hour); + + return TRUE; +} + /* Returns time from universal time string into SilcTime */ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time) diff --git a/lib/silcutil/silctime.h b/lib/silcutil/silctime.h index 17912fe3..60963736 100644 --- a/lib/silcutil/silctime.h +++ b/lib/silcutil/silctime.h @@ -134,6 +134,24 @@ const char *silc_time_string(SilcInt64 time_val_sec); ***/ SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time); +/****f* silcutil/SilcTimeAPI/silc_timezone + * + * SYNOPSIS + * + * SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size); + * + * DESCRIPTION + * + * Returns current timezone in Universal time format into the `timezone' + * buffer of size of `timezone_size'. The possible values this function + * returns are: Z (For UTC timezone), +hh (UTC + hours) -hh (UTC - hours), + * +hh:mm (UTC + hours:minutes) or -hh:mm (UTC - hours:minutes). + * + * Returns FALSE on error, TRUE otherwise. + * + ***/ +SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size); + /****f* silcutil/SilcTimeAPI/silc_time_universal * * SYNOPSIS diff --git a/lib/silcutil/tests/Makefile.am b/lib/silcutil/tests/Makefile.am index 1bf2f09b..f08893d7 100644 --- a/lib/silcutil/tests/Makefile.am +++ b/lib/silcutil/tests/Makefile.am @@ -20,7 +20,7 @@ AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign bin_PROGRAMS = test_silcstrutil test_silcstringprep test_silchashtable \ test_silclist test_silcfsm test_silcasync test_silcschedule \ test_silcnet test_silcstack test_silcmime test_silcfdstream \ - test_silcatomic test_silcmutex + test_silcatomic test_silcmutex test_silctime test_silcstrutil_SOURCES = test_silcstrutil.c test_silcstringprep_SOURCES = test_silcstringprep.c @@ -35,6 +35,7 @@ test_silcstack_SOURCES = test_silcstack.c test_silcfdstream_SOURCES = test_silcfdstream.c test_silcatomic_SOURCES = test_silcatomic.c test_silcmutex_SOURCES = test_silcmutex.c +test_silctime_SOURCES = test_silctime.c LIBS = $(SILC_COMMON_LIBS) LDADD = -L.. -L../.. -lsilc diff --git a/lib/silcutil/tests/test_silctime.c b/lib/silcutil/tests/test_silctime.c new file mode 100644 index 00000000..0bbe2814 --- /dev/null +++ b/lib/silcutil/tests/test_silctime.c @@ -0,0 +1,38 @@ +/* SilcTime tests */ + +#include "silc.h" + +int main(int argc, char **argv) +{ + SilcBool success = FALSE; + SilcTimeStruct curtime; + + if (argc > 1 && !strcmp(argv[1], "-d")) { + silc_log_debug(TRUE); + silc_log_quick(TRUE); + silc_log_debug_hexdump(TRUE); + silc_log_set_debug_string("*time*"); + } + + SILC_LOG_DEBUG(("Get current time")); + if (!silc_time_value(0, &curtime)) + goto err; + SILC_LOG_DEBUG(("year : %d", curtime.year)); + SILC_LOG_DEBUG(("month : %d", curtime.month)); + SILC_LOG_DEBUG(("day : %d", curtime.day)); + SILC_LOG_DEBUG(("hour : %d", curtime.hour)); + SILC_LOG_DEBUG(("minute : %d", curtime.minute)); + SILC_LOG_DEBUG(("msecond : %d", curtime.msecond)); + SILC_LOG_DEBUG(("utc_hour : %d", curtime.utc_hour)); + SILC_LOG_DEBUG(("utc_min : %d", curtime.utc_minute)); + SILC_LOG_DEBUG(("utc_east : %d", curtime.utc_east)); + SILC_LOG_DEBUG(("dst : %d", curtime.dst)); + + success = TRUE; + + err: + SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE")); + fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE"); + + return success; +} -- 2.24.0