Fixed silc_time_value, added silc_timezone.
[silc.git] / lib / silcutil / silctime.c
index 30dec3c60a7856a5af20b8da8c0be6f66a1b2e15..4444e4e8a5cb2e7074d1a8c02b19b7c35a97d3fa 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2006 Pekka Riikonen
+  Copyright (C) 2003 - 2007 Pekka Riikonen
 
   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
@@ -106,6 +106,7 @@ SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time)
 {
   struct tm *time;
   unsigned int msec = 0;
+  time_t timeval;
 
   if (!ret_time)
     return TRUE;
@@ -113,10 +114,10 @@ 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((time_t *)&time_val);
+  time = localtime(&timeval);
   if (!time)
     return FALSE;
 
@@ -146,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)
@@ -209,7 +235,7 @@ SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
 {
   int ret, len = 0;
   memset(ret_string, 0, ret_string_size);
-  ret = snprintf(ret_string, ret_string_size - 1,
+  ret = silc_snprintf(ret_string, ret_string_size - 1,
                 "%02u%02u%02u%02u%02u%02u",
                 time_val->year % 100, time_val->month, time_val->day,
                 time_val->hour, time_val->minute, time_val->second);
@@ -218,12 +244,12 @@ SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
   len += ret;
 
   if (!time_val->utc_hour && !time_val->utc_minute) {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
+    ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
     if (ret < 0)
       return FALSE;
     len += ret;
   } else {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len,
+    ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
                   "%c%02u%02u", time_val->utc_east ? '+' : '-',
                   time_val->utc_hour, time_val->utc_minute);
     if (ret < 0)
@@ -319,7 +345,7 @@ SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
 {
   int len = 0, ret;
   memset(ret_string, 0, ret_string_size);
-  ret = snprintf(ret_string, ret_string_size - 1,
+  ret = silc_snprintf(ret_string, ret_string_size - 1,
                 "%04u%02u%02u%02u%02u%02u",
                 time_val->year, time_val->month, time_val->day, time_val->hour,
                 time_val->minute, time_val->second);
@@ -328,7 +354,7 @@ SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
   len += ret;
 
   if (time_val->msecond) {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len,
+    ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
                   ".%lu", (unsigned long)time_val->msecond);
     if (ret < 0)
       return FALSE;
@@ -336,12 +362,12 @@ SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
   }
 
   if (!time_val->utc_hour && !time_val->utc_minute) {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
+    ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
     if (ret < 0)
       return FALSE;
     len += ret;
   } else {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len,
+    ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
                   "%c%02u%02u", time_val->utc_east ? '+' : '-',
                   time_val->utc_hour, time_val->utc_minute);
     if (ret < 0)
@@ -354,8 +380,8 @@ SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
 
 /* Return TRUE if `smaller' is smaller than `bigger'. */
 
-SilcBool silc_compare_time_val(struct timeval *smaller,
-                              struct timeval *bigger)
+SilcBool silc_compare_timeval(struct timeval *smaller,
+                             struct timeval *bigger)
 {
   if ((smaller->tv_sec < bigger->tv_sec) ||
       ((smaller->tv_sec == bigger->tv_sec) &&