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
unsigned int day,
unsigned int hour,
unsigned int minute,
- unsigned int second)
+ unsigned int second,
+ unsigned int msec)
{
if (year > (1 << 15))
return FALSE;
return FALSE;
if (second > 61)
return FALSE;
+ if (msec > 1000)
+ return FALSE;
time->year = year;
time->month = month;
time->hour = hour;
time->minute = minute;
time->second = second;
+ time->msecond = msec;
return TRUE;
}
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;
/* 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;
}
/* 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;
/* 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;
}
/* 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;
/* 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;
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;
+}
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
* 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
*
***/
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
*
*
* 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
* `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
*
* 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
* `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 */