Added SILC Thread Queue API
[silc.git] / lib / silcutil / silctime.c
index 13575e57ee62132c5ebdb8f1e592a7216e4b5879..4dc7f2172ec5470d0a78ff4736aefa5e38d2ffba 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 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
 
 #include "silc.h"
 
+/* Fills the SilcTime structure with correct values */
+
+static SilcBool silc_time_fill(SilcTime time,
+                              unsigned int year,
+                              unsigned int month,
+                              unsigned int day,
+                              unsigned int hour,
+                              unsigned int minute,
+                              unsigned int second,
+                              unsigned int msec)
+{
+  if (year > (1 << 15))
+    goto err;
+  if (month < 1 || month > 12)
+    goto err;
+  if (day < 1 || day > 31)
+    goto err;
+  if (hour > 23)
+    goto err;
+  if (minute > 60)
+    goto err;
+  if (second > 61)
+    goto err;
+  if (msec > 1000)
+    goto err;
+
+  time->year = year;
+  time->month = month;
+  time->day = day;
+  time->hour = hour;
+  time->minute = minute;
+  time->second = second;
+  time->msecond = msec;
+
+  return TRUE;
+
+ err:
+  silc_set_errno(SILC_ERR_BAD_TIME);
+  return FALSE;
+}
+
 /* Return time since Epoch */
 
 SilcInt64 silc_time(void)
@@ -26,20 +67,42 @@ SilcInt64 silc_time(void)
   return (SilcInt64)time(NULL);
 }
 
+/* Return time since Epoch in milliseconds */
+
+SilcInt64 silc_time_msec(void)
+{
+  struct timeval curtime;
+  silc_gettimeofday(&curtime);
+  return (curtime.tv_sec * (SilcUInt64)1000) +
+    (curtime.tv_usec / (SilcUInt64)1000);
+}
+
+/* Return time since Epoch in microseconds */
+
+SilcInt64 silc_time_usec(void)
+{
+  struct timeval curtime;
+  if (silc_gettimeofday(&curtime))
+    silc_set_errno_posix(errno);
+  return (curtime.tv_sec * (SilcUInt64)1000000) + curtime.tv_usec;
+}
+
 /* Returns time as string */
 
-const char *silc_time_string(SilcInt64 timeval)
+const char *silc_time_string(SilcInt64 time_val)
 {
   time_t curtime;
   char *return_time;
 
-  if (!timeval)
-    curtime = time(NULL);
+  if (!time_val)
+    curtime = silc_time();
   else
-    curtime = (time_t)timeval;
+    curtime = (time_t)time_val;
   return_time = ctime(&curtime);
-  if (!return_time)
+  if (!return_time) {
+    silc_set_errno(SILC_ERR_BAD_TIME);
     return NULL;
+  }
   return_time[strlen(return_time) - 1] = '\0';
 
   return (const char *)return_time;
@@ -47,28 +110,35 @@ const char *silc_time_string(SilcInt64 timeval)
 
 /* Returns time as SilcTime structure */
 
-SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time)
+SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time)
 {
-  struct tm *time;
+  struct tm *t;
+  unsigned int msec = 0;
+  time_t timeval;
+  SilcInt32 ctz = 0;
 
   if (!ret_time)
     return TRUE;
 
-  if (!timeval)
-    timeval = silc_time();
+  if (!time_val)
+    time_val = silc_time_msec();
 
-  time = localtime((time_t *)&timeval);
-  if (!time)
+  msec = (SilcUInt64)time_val % (SilcUInt64)1000;
+  timeval = (time_t)((SilcUInt64)time_val / (SilcUInt64)1000);
+
+  t = localtime(&timeval);
+  if (!t) {
+    silc_set_errno(SILC_ERR_BAD_TIME);
     return FALSE;
+  }
 
   memset(ret_time, 0, sizeof(*ret_time));
-  ret_time->year    = time->tm_year + 1900;
-  ret_time->month   = time->tm_mon + 1;
-  ret_time->day     = time->tm_mday;
-  ret_time->hour    = time->tm_hour;
-  ret_time->minute  = time->tm_min;
-  ret_time->second  = time->tm_sec;
-  ret_time->dst     = time->tm_isdst ? 1 : 0;
+  if (!silc_time_fill(ret_time, t->tm_year + 1900, t->tm_mon + 1,
+                     t->tm_mday, t->tm_hour, t->tm_min,
+                     t->tm_sec, msec))
+    return FALSE;
+
+  ret_time->dst        = t->tm_isdst ? 1 : 0;
 
 #ifdef SILC_WIN32
   ret_time->utc_east   = _timezone < 0 ? 1 : 0;
@@ -77,47 +147,55 @@ SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time)
   ret_time->utc_minute = (ret_time->utc_east ? (-(_timezone)) % 3600 :
                          _timezone % 3600);
 #else
-#if defined(HAVE_TZSET)
+#if defined(HAVE_TIMEZONE)
   ret_time->utc_east   = timezone < 0 ? 1 : 0;
-  ret_time->utc_hour   = (ret_time->utc_east ? (-(timezone)) / 3600 :
-                         timezone / 3600);
-  ret_time->utc_minute = (ret_time->utc_east ? (-(timezone)) % 3600 :
-                         timezone % 3600);
-#endif /* HAVE_TZSET */
+  ctz = timezone;
+  if (ret_time->dst)
+    ctz -= 3600;
+#elif defined(HAVE_TM_GMTOFF)
+  ret_time->utc_east   = t->tm_gmtoff > 0 ? 1 : 0;
+  ctz = -t->tm_gmtoff;
+#elif defined(HAVE___TM_GMTOFF)
+  ret_time->utc_east   = t->__tm_gmtoff > 0 ? 1 : 0;
+  ctz = -t->__tm_gmtoff;
+#elif defined(HAVE___TM_GMTOFF__)
+  ret_time->utc_east   = t->__tm_gmtoff__ > 0 ? 1 : 0;
+  ctz = -t->__tm_gmtoff__;
+#endif /* HAVE_TIMEZONE */
+
+  ret_time->utc_hour   = (ret_time->utc_east ? (-(ctz)) / 3600 : ctz / 3600);
+  ret_time->utc_minute = (ret_time->utc_east ? (-(ctz)) % 3600 : ctz % 3600);
 #endif /* SILC_WIN32 */
 
+  if (ret_time->utc_minute)
+    ret_time->utc_minute /= 60;
+
   return TRUE;
 }
 
-/* Fills the SilcTime structure with correct values */
+/* Returns timezone */
 
-static SilcBool silc_time_fill(SilcTime time,
-                              unsigned int year,
-                              unsigned int month,
-                              unsigned int day,
-                              unsigned int hour,
-                              unsigned int minute,
-                              unsigned int second)
+SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size)
 {
-  if (year > 8191)
-    return FALSE;
-  if (month < 1 || month > 12)
-    return FALSE;
-  if (day < 1 || day > 31)
-    return FALSE;
-  if (hour > 23)
-    return FALSE;
-  if (minute > 60)
+  SilcTimeStruct curtime;
+
+  if (timezone_size < 6) {
+    silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
     return FALSE;
-  if (second > 61)
+  }
+
+  if (!silc_time_value(0, &curtime))
     return FALSE;
 
-  time->year = year;
-  time->month = month;
-  time->day = day;
-  time->hour = hour;
-  time->minute = minute;
-  time->second = second;
+  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;
 }
@@ -139,11 +217,12 @@ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
               &day, &hour, &minute, &second, &z);
   if (ret < 3) {
     SILC_LOG_DEBUG(("Invalid UTC time string"));
+    silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid UTC time string");
     return FALSE;
   }
 
   /* Fill the SilcTime structure */
-  ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
+  ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
   if (!ret) {
     SILC_LOG_DEBUG(("Incorrect values in UTC time string"));
     return FALSE;
@@ -154,19 +233,21 @@ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
     ret = sscanf(universal_time + (ret * 2) + 1, "%02u%02u", &hour, &minute);
     if (ret != 2) {
       SILC_LOG_DEBUG(("Malformed UTC time string"));
+      silc_set_errno_reason(SILC_ERR_BAD_TIME, "Malformed UTC time string");
       return FALSE;
     }
 
-    if (hour < 0 || hour > 23)
-      return FALSE;
-    if (minute < 0 || minute > 60)
+    if (hour > 23 || minute > 60) {
+      silc_set_errno(SILC_ERR_BAD_TIME);
       return FALSE;
+    }
 
     ret_time->utc_hour   = hour;
     ret_time->utc_minute = minute;
     ret_time->utc_east   = (z == '-') ? 0 : 1;
   } else if (z != 'Z') {
     SILC_LOG_DEBUG(("Invalid timezone"));
+    silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid timezone");
     return FALSE;
   }
 
@@ -180,28 +261,29 @@ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
 
 /* Encode universal time string. */
 
-SilcBool silc_time_universal_string(SilcTime timeval, char *ret_string,
+SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
                                    SilcUInt32 ret_string_size)
 {
   int ret, len = 0;
+
   memset(ret_string, 0, ret_string_size);
-  ret = snprintf(ret_string, ret_string_size - 1,
-                "%02u%02u%02u%02u%02u%02u",
-                timeval->year % 100, timeval->month, timeval->day,
-                timeval->hour, timeval->minute, timeval->second);
+  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);
   if (ret < 0)
     return FALSE;
   len += ret;
 
-  if (!timeval->utc_hour && !timeval->utc_minute) {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
+  if (!time_val->utc_hour && !time_val->utc_minute) {
+    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,
-                  "%c%02u%02u", timeval->utc_east ? '+' : '-',
-                  timeval->utc_hour, timeval->utc_minute);
+    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)
       return FALSE;
     len += ret;
@@ -228,11 +310,12 @@ SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
               &day, &hour, &minute, &second);
   if (ret < 3) {
     SILC_LOG_DEBUG(("Invalid generalized time string"));
+    silc_set_errno_reason(SILC_ERR_BAD_TIME, "Invalid generalized time string");
     return FALSE;
   }
 
   /* Fill the SilcTime structure */
-  ret = silc_time_fill(ret_time, year, month, day, hour, minute, second);
+  ret = silc_time_fill(ret_time, year, month, day, hour, minute, second, 0);
   if (!ret) {
     SILC_LOG_DEBUG(("Incorrect values in generalized time string"));
     return FALSE;
@@ -243,6 +326,8 @@ SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
   ret = sscanf(generalized_time + i, "%c", &z);
   if (ret != 1) {
     SILC_LOG_DEBUG(("Malformed generalized time string"));
+    silc_set_errno_reason(SILC_ERR_BAD_TIME,
+                         "Malformed generalized time string");
     return FALSE;
   }
 
@@ -253,6 +338,8 @@ SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
     ret = sscanf(generalized_time + i, "%u%n", &msecond, &l);
     if (ret != 1) {
       SILC_LOG_DEBUG(("Malformed generalized time string"));
+      silc_set_errno_reason(SILC_ERR_BAD_TIME,
+                           "Malformed generalized time string");
       return FALSE;
     }
     while (l > 4) {
@@ -271,14 +358,16 @@ SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
   if (z == '-' || z == '+') {
     ret = sscanf(generalized_time + i + 1, "%02u%02u", &hour, &minute);
     if (ret != 2) {
-      SILC_LOG_DEBUG(("Malformed UTC time string"));
+      SILC_LOG_DEBUG(("Malformed generalized time string"));
+      silc_set_errno_reason(SILC_ERR_BAD_TIME,
+                           "Malformed generalized time string");
       return FALSE;
     }
 
-    if (hour < 0 || hour > 23)
-      return FALSE;
-    if (minute < 0 || minute > 60)
+    if (hour > 23 || minute > 60) {
+      silc_set_errno(SILC_ERR_BAD_TIME);
       return FALSE;
+    }
 
     ret_time->utc_hour   = hour;
     ret_time->utc_minute = minute;
@@ -290,36 +379,37 @@ SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
 
 /* Encode generalized time string */
 
-SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
+SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
                                      SilcUInt32 ret_string_size)
 {
   int len = 0, ret;
   memset(ret_string, 0, ret_string_size);
-  ret = snprintf(ret_string, ret_string_size - 1,
-                "%04u%02u%02u%02u%02u%02u",
-                timeval->year, timeval->month, timeval->day, timeval->hour,
-                timeval->minute, timeval->second);
+  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);
   if (ret < 0)
     return FALSE;
   len += ret;
 
-  if (timeval->msecond) {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len,
-                  ".%lu", (unsigned long)timeval->msecond);
+  if (time_val->msecond) {
+    ret = silc_snprintf(ret_string + len, ret_string_size - 1 - len,
+                       ".%lu", (unsigned long)time_val->msecond);
     if (ret < 0)
       return FALSE;
     len += ret;
   }
 
-  if (!timeval->utc_hour && !timeval->utc_minute) {
-    ret = snprintf(ret_string + len, ret_string_size - 1 - len, "Z");
+  if (!time_val->utc_hour && !time_val->utc_minute) {
+    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,
-                  "%c%02u%02u", timeval->utc_east ? '+' : '-',
-                  timeval->utc_hour, timeval->utc_minute);
+    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)
       return FALSE;
     len += ret;
@@ -327,3 +417,13 @@ SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
 
   return TRUE;
 }
+
+/* Return TRUE if `smaller' is smaller than `bigger'. */
+
+int silc_compare_timeval(struct timeval *t1, struct timeval *t2)
+{
+  SilcInt32 s = t1->tv_sec - t2->tv_sec;
+  if (!s)
+    return t1->tv_usec - t2->tv_usec;
+  return s;
+}