Added silc_time_[m|u]sec.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 22 Oct 2006 10:31:05 +0000 (10:31 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 22 Oct 2006 10:31:05 +0000 (10:31 +0000)
lib/silcutil/silctime.c
lib/silcutil/silctime.h

index da49445ac8a079a8a3130b1c9f80db3eda120364..30dec3c60a7856a5af20b8da8c0be6f66a1b2e15 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 Pekka Riikonen
+  Copyright (C) 2003 - 2006 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
@@ -27,7 +27,8 @@ static SilcBool silc_time_fill(SilcTime time,
                               unsigned int day,
                               unsigned int hour,
                               unsigned int minute,
-                              unsigned int second)
+                              unsigned int second,
+                              unsigned int msec)
 {
   if (year > (1 << 15))
     return FALSE;
@@ -41,6 +42,8 @@ static SilcBool silc_time_fill(SilcTime time,
     return FALSE;
   if (second > 61)
     return FALSE;
+  if (msec > 1000)
+    return FALSE;
 
   time->year = year;
   time->month = month;
@@ -48,6 +51,7 @@ static SilcBool silc_time_fill(SilcTime time,
   time->hour = hour;
   time->minute = minute;
   time->second = second;
+  time->msecond = msec;
 
   return TRUE;
 }
@@ -59,17 +63,35 @@ 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 * 1000) + (curtime.tv_usec / 1000);
+}
+
+/* Return time since Epoch in microseconds */
+
+SilcInt64 silc_time_usec(void)
+{
+  struct timeval curtime;
+  silc_gettimeofday(&curtime);
+  return (curtime.tv_sec * 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)
     return NULL;
@@ -80,24 +102,28 @@ 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;
+  unsigned int msec = 0;
 
   if (!ret_time)
     return TRUE;
 
-  if (!timeval)
-    timeval = silc_time();
+  if (!time_val)
+    time_val = silc_time_msec();
+
+  msec = time_val % 1000;
+  time_val /= 1000;
 
-  time = localtime((time_t *)&timeval);
+  time = localtime((time_t *)&time_val);
   if (!time)
     return FALSE;
 
   memset(ret_time, 0, sizeof(*ret_time));
   if (!silc_time_fill(ret_time, time->tm_year + 1900, time->tm_mon + 1,
                      time->tm_mday, time->tm_hour, time->tm_min,
-                     time->tm_sec))
+                     time->tm_sec, msec))
     return FALSE;
 
   ret_time->dst        = time->tm_isdst ? 1 : 0;
@@ -141,7 +167,7 @@ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time)
   }
 
   /* 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;
@@ -178,28 +204,28 @@ 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);
+                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) {
+  if (!time_val->utc_hour && !time_val->utc_minute) {
     ret = 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);
+                  "%c%02u%02u", time_val->utc_east ? '+' : '-',
+                  time_val->utc_hour, time_val->utc_minute);
     if (ret < 0)
       return FALSE;
     len += ret;
@@ -230,7 +256,7 @@ SilcBool silc_time_generalized(const char *generalized_time, SilcTime ret_time)
   }
 
   /* 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;
@@ -288,36 +314,36 @@ 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);
+                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) {
+  if (time_val->msecond) {
     ret = snprintf(ret_string + len, ret_string_size - 1 - len,
-                  ".%lu", (unsigned long)timeval->msecond);
+                  ".%lu", (unsigned long)time_val->msecond);
     if (ret < 0)
       return FALSE;
     len += ret;
   }
 
-  if (!timeval->utc_hour && !timeval->utc_minute) {
+  if (!time_val->utc_hour && !time_val->utc_minute) {
     ret = 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);
+                  "%c%02u%02u", time_val->utc_east ? '+' : '-',
+                  time_val->utc_hour, time_val->utc_minute);
     if (ret < 0)
       return FALSE;
     len += ret;
@@ -325,3 +351,16 @@ SilcBool silc_time_generalized_string(SilcTime timeval, char *ret_string,
 
   return TRUE;
 }
+
+/* Return TRUE if `smaller' is smaller than `bigger'. */
+
+SilcBool silc_compare_time_val(struct timeval *smaller,
+                              struct timeval *bigger)
+{
+  if ((smaller->tv_sec < bigger->tv_sec) ||
+      ((smaller->tv_sec == bigger->tv_sec) &&
+       (smaller->tv_usec < bigger->tv_usec)))
+    return TRUE;
+
+  return FALSE;
+}
index c5cfc8be366fc77073dc0144ec612a3142924beb..c7494bafa9dc98d9cd124ee73f5d28941059dc3d 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 Pekka Riikonen
+  Copyright (C) 2003 - 2006 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
@@ -39,7 +39,7 @@
  * DESCRIPTION
  *
  *    This context represents time value.  It includes date and time
- *    down to millisecond precision.
+ *    down to millisecond precision.  The structure size is 64 bits.
  *
  * SOURCE
  *
@@ -73,36 +73,66 @@ typedef struct {
  ***/
 SilcInt64 silc_time(void);
 
+/****f* silcutil/SilcTimeAPI/silc_time_msec
+ *
+ * SYNOPSIS
+ *
+ *    SilcInt64 silc_time_msec(void);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the current time of the system since Epoch in millisecond
+ *    resolution.  Returns - 1 on error.
+ *
+ ***/
+SilcInt64 silc_time_msec(void);
+
+/****f* silcutil/SilcTimeAPI/silc_time_usec
+ *
+ * SYNOPSIS
+ *
+ *    SilcInt64 silc_time_usec(void);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the current time of the system since Epoch in microsecond
+ *    resolution.  Returns - 1 on error.
+ *
+ ***/
+SilcInt64 silc_time_usec(void);
+
 /****f* silcutil/SilcTimeAPI/silc_time_string
  *
  * SYNOPSIS
  *
- *    const char *silc_time_string(SilcInt64 timeval);
+ *    const char *silc_time_string(SilcInt64 time_val);
  *
  * DESCRIPTION
  *
  *    Returns time and date as string.  The caller must not free the string
  *    and next call to this function will delete the old string.  If the
- *    `timeval' is zero (0) returns current time as string, otherwise the
- *    `timeval' as string.  Returns NULL on error.
+ *    `time_val' is zero (0) returns current time as string, otherwise the
+ *    `time_val' as string.  The `time_val' is in seconds since Epoch.
+ *    Returns NULL on error.
  *
  ***/
-const char *silc_time_string(SilcInt64 timeval);
+const char *silc_time_string(SilcInt64 time_val);
 
 /****f* silcutil/SilcTimeAPI/silc_time_value
  *
  * SYNOPSIS
  *
- *   SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
+ *   SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time);
  *
  * DESCRIPTION
  *
- *    Returns time and date as SilcTime.  If the `timeval' is zero (0)
- *    returns current time as SilcTime, otherwise the `timeval' as SilcTime.
- *    Returns FALSE on error, TRUE otherwise.
+ *    Returns time and date as SilcTime.  If the `time_val' is zero (0)
+ *    returns current time as SilcTime, otherwise the `time_val' as SilcTime.
+ *    The `time_val' is in milliseconds since Epoch.  Returns FALSE on error,
+ *    TRUE otherwise.
  *
  ***/
-SilcBool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
+SilcBool silc_time_value(SilcInt64 time_val, SilcTime ret_time);
 
 /****f* silcutil/SilcTimeAPI/silc_time_universal
  *
@@ -134,7 +164,7 @@ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
  *
  * SYNOPSIS
  *
- *    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);
  *
  * DESCRIPTION
@@ -143,7 +173,7 @@ SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
  *
  ***/
-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);
 
 /****f* silcutil/SilcTimeAPI/silc_time_generalized
@@ -181,7 +211,8 @@ silc_time_generalized(const char *generalized_time, SilcTime ret_time);
  *
  * SYNOPSIS
  *
- *    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);
  *
  * DESCRIPTION
@@ -190,7 +221,37 @@ silc_time_generalized(const char *generalized_time, SilcTime ret_time);
  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
  *
  ***/
-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);
 
+/****f* silcutil/SilcTimeAPI/silc_compare_timeval
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_compare_timeval(struct time_val *smaller,
+ *                                  struct time_val *bigger)
+ *
+ * DESCRIPTION
+ *
+ *    Compare two timeval structures and return TRUE if the first
+ *    time value is smaller than the second time value.
+ *
+ ***/
+SilcBool silc_compare_timeval(struct timeval *smaller,
+                             struct timeval *bigger);
+
+/****f* silcutil/SilcTimeAPI/silc_gettimeofday
+ *
+ * SYNOPSIS
+ *
+ *    int silc_gettimeofday(struct timeval *p);
+ *
+ * DESCRIPTION
+ *
+ *    Return current time to struct timeval.  This function is system
+ *    dependant.  Returns 0 on success and -1 on error.
+ *
+ ***/
+int silc_gettimeofday(struct timeval *p);
+
 #endif /* SILCTIME_H */