Fixed silc_time_value, added silc_timezone.
authorPekka Riikonen <priikone@silcnet.org>
Thu, 22 Feb 2007 14:33:20 +0000 (14:33 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Thu, 22 Feb 2007 14:33:20 +0000 (14:33 +0000)
lib/silcutil/silctime.c
lib/silcutil/silctime.h
lib/silcutil/tests/Makefile.am
lib/silcutil/tests/test_silctime.c [new file with mode: 0644]

index 7619f99e63ed3042006e0a3177a6cf33c47e2c58..4444e4e8a5cb2e7074d1a8c02b19b7c35a97d3fa 100644 (file)
@@ -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)
index 17912fe3b365aed4c257458ebb09c129400e4497..60963736cfcae14c3ad068fbc97649ff3e6e5d95 100644 (file)
@@ -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
index 1bf2f09bc7ecae530b5bdfe7f12c302eac19e5d0..f08893d7250f48d83332f6ade8b81ce11f9fe098 100644 (file)
@@ -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 (file)
index 0000000..0bbe281
--- /dev/null
@@ -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;
+}