Added SILC Thread Queue API
[silc.git] / lib / silcutil / silctime.h
index c72f0a9854670341d21c7962203284c9d32e23da..e01e1ae701f92660debb212b10dae7a6195f7b86 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
  * 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
  *
  ***/
-typedef struct {
-  unsigned int year       : 13;           /* Year,     0 - 8191 */
+typedef struct SilcTimeObject {
+  unsigned int year       : 15;           /* Year,     0 - 32768 */
   unsigned int month      : 4;    /* Month,    1 - 12 */
   unsigned int day        : 5;    /* Day,      1 - 31 */
   unsigned int hour       : 5;    /* Hour,     0 - 23 */
@@ -56,7 +56,6 @@ typedef struct {
   unsigned int utc_minute : 6;    /* Offset to Zulu (UTC) minutes */
   unsigned int utc_east   : 1;    /* Offset, 1 east (+), 0 west (-) */
   unsigned int dst        : 1;    /* Set if daylight saving time */
-  /* 2 bits to spare */
 } *SilcTime, SilcTimeStruct;
 /***/
 
@@ -74,43 +73,92 @@ 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_sec);
  *
  * 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_sec' is zero (0) returns current time as string, otherwise the
+ *    `time_val_sec' as string.  The `time_val_sec' is in seconds since Epoch.
+ *    Returns NULL on error.
  *
  ***/
-const char *silc_time_string(SilcInt64 timeval);
+const char *silc_time_string(SilcInt64 time_val_sec);
 
 /****f* silcutil/SilcTimeAPI/silc_time_value
  *
  * SYNOPSIS
  *
- *   bool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
+ *   SilcBool silc_time_value(SilcInt64 time_val_msec, SilcTime ret_time);
+ *
+ * DESCRIPTION
+ *
+ *    Returns time and date as SilcTime.  If the `time_val_msec' is zero (0)
+ *    returns current time as SilcTime, otherwise the `time_val_msec' as
+ *    SilcTime.  The `time_val_msec' is in milliseconds since Epoch.  Returns
+ *    FALSE on error, TRUE otherwise.
+ *
+ ***/
+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 time and date as SilcTime.  If the `timeval' is zero (0)
- *    returns current time as SilcTime, otherwise the `timeval' as SilcTime.
+ *    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).  The
+ *    returned values are always offsets to UTC.
+ *
  *    Returns FALSE on error, TRUE otherwise.
  *
  ***/
-bool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
+SilcBool silc_timezone(char *timezone, SilcUInt32 timezone_size);
 
 /****f* silcutil/SilcTimeAPI/silc_time_universal
  *
  * SYNOPSIS
  *
- *    bool silc_time_universal(const char *universal_time,
- *                             SilcTime ret_time);
+ *    SilcBool silc_time_universal(const char *universal_time,
+ *                                 SilcTime ret_time);
  *
  * DESCRIPTION
  *
@@ -129,14 +177,14 @@ bool silc_time_value(SilcInt64 timeval, SilcTime ret_time);
  *    silc_time_universal("030219190403Z", &ret_time);
  *
  ***/
-bool silc_time_universal(const char *universal_time, SilcTime ret_time);
+SilcBool silc_time_universal(const char *universal_time, SilcTime ret_time);
 
 /****f* silcutil/SilcTimeAPI/silc_time_universal_string
  *
  * SYNOPSIS
  *
- *    bool silc_time_universal_string(SilcTime timeval, char *ret_string,
- *                                    SilcUInt32 ret_string_size);
+ *    SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
+ *                                        SilcUInt32 ret_string_size);
  *
  * DESCRIPTION
  *
@@ -144,15 +192,15 @@ bool silc_time_universal(const char *universal_time, SilcTime ret_time);
  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
  *
  ***/
-bool silc_time_universal_string(SilcTime timeval, char *ret_string,
-                               SilcUInt32 ret_string_size);
+SilcBool silc_time_universal_string(SilcTime time_val, char *ret_string,
+                                   SilcUInt32 ret_string_size);
 
 /****f* silcutil/SilcTimeAPI/silc_time_generalized
  *
  * SYNOPSIS
  *
- *    bool silc_time_generalized(const char *generalized_time,
- *                               SilcTime ret_time);
+ *    SilcBool silc_time_generalized(const char *generalized_time,
+ *                                   SilcTime ret_time);
  *
  * DESCRIPTION
  *
@@ -175,14 +223,16 @@ bool silc_time_universal_string(SilcTime timeval, char *ret_string,
  *    silc_time_generalized("20030219190510.212Z", &ret_time);
  *
  ***/
-bool silc_time_generalized(const char *generalized_time, SilcTime ret_time);
+SilcBool
+silc_time_generalized(const char *generalized_time, SilcTime ret_time);
 
 /****f* silcutil/SilcTimeAPI/silc_time_generalized_string
  *
  * SYNOPSIS
  *
- *    bool silc_time_generalized_string(SilcTime timeval, char *ret_string,
- *                                      SilcUInt32 ret_string_size);
+ *    SilcBool silc_time_generalized_string(SilcTime time_val,
+ *                                          char *ret_string,
+ *                                          SilcUInt32 ret_string_size);
  *
  * DESCRIPTION
  *
@@ -190,7 +240,79 @@ bool silc_time_generalized(const char *generalized_time, SilcTime ret_time);
  *    `ret_string' buffer.  Returns FALSE if the buffer is too small.
  *
  ***/
-bool silc_time_generalized_string(SilcTime timeval, char *ret_string,
-                                 SilcUInt32 ret_string_size);
+SilcBool silc_time_generalized_string(SilcTime time_val, char *ret_string,
+                                     SilcUInt32 ret_string_size);
+
+/****f* silcutil/SilcTimeAPI/silc_compare_timeval
+ *
+ * SYNOPSIS
+ *
+ *    int silc_compare_timeval(struct time_val *t1, struct time_val *t2);
+ *
+ * DESCRIPTION
+ *
+ *    Compares `t1' and `t2' time structures and returns less than zero,
+ *    zero or more than zero when `t1' is smaller, equal or bigger than
+ *    `t2', respectively.
+ *
+ ***/
+int silc_compare_timeval(struct timeval *t1, struct timeval *t2);
+
+/****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);
+
+/****f* silcutil/SilcTimeAPI/silc_usleep
+ *
+ * SYNOPSIS
+ *
+ *    void silc_usleep(unsigned long microseconds);
+ *
+ * DESCRIPTION
+ *
+ *    Delays the execution of process/thread for the specified amount of
+ *    time, which is in microseconds.
+ *
+ * NOTES
+ *
+ *    The delay is only approximate and on some platforms the resolution is
+ *    in fact milliseconds.
+ *
+ ***/
+static inline
+void silc_usleep(unsigned long microseconds)
+{
+#ifdef SILC_UNIX
+#ifdef HAVE_NANOSLEEP
+  struct timespec tv;
+  tv.tv_sec = 0;
+  tv.tv_nsec = microseconds * 1000;
+#endif /* HAVE_NANOSLEEP */
+#endif /* SILC_UNIX */
+
+#ifdef SILC_UNIX
+#ifdef HAVE_NANOSLEEP
+  nanosleep(&tv, NULL);
+#else
+  usleep(microseconds);
+#endif /* HAVE_NANOSLEEP */
+#endif /* SILC_UNIX */
+#ifdef SILC_WIN32
+  Sleep(microseconds / 1000);
+#endif /* SILC_WIN32 */
+#ifdef SILC_SYMBIAN
+  silc_symbian_usleep(microseconds);
+#endif /* SILC_SYMBIAN */
+}
 
 #endif /* SILCTIME_H */