From f4e5872398f5d2e86ae1ef6c4a866145e0c79bb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Tue, 24 Nov 2009 13:43:52 +0100 Subject: [PATCH] Replace struct tm with struct timeval in isds_isds_get_list_of_sent_messages() This is time zone agnostic and it supports subsecond precision. The broken time format is job for application now. --- client/isdsclient.c | 8 +++++++- src/isds.c | 34 ++++++++++++++++------------------ src/isds.h | 3 ++- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/client/isdsclient.c b/client/isdsclient.c index 9cb7d1b..037357f 100644 --- a/client/isdsclient.c +++ b/client/isdsclient.c @@ -438,13 +438,19 @@ int main(int argc, char **argv) { /* Get list of sent messages */ { - struct tm from_time = { + struct tm from_time_tm = { .tm_year = 2000 - 1900, .tm_mon = 1 - 1, .tm_mday = 1, .tm_hour = 1, .tm_min = 2, .tm_sec = 3, + .tm_isdst = -1 + }; + time_t from_time_t = mktime(&from_time_tm); + struct timeval from_time = { + .tv_sec = from_time_t, + .tv_usec = 4000 }; /* TODO: Try different criteria */ diff --git a/src/isds.c b/src/isds.c index 17ac5eb..c82fe14 100644 --- a/src/isds.c +++ b/src/isds.c @@ -3,7 +3,6 @@ #include #include #include -#include #include "isds_priv.h" #include "utils.h" #include "soap.h" @@ -191,9 +190,6 @@ isds_error isds_init(void) { if (!(isds_ns = xmlNewNs(NULL, BAD_CAST ISDS_NS, BAD_CAST "isds"))) return IE_ERROR; - /* Time arithmetics (extern timezone) */ - tzset(); - return IE_SUCCESS; } @@ -812,29 +808,31 @@ static isds_error tm2datestring(const struct tm *time, xmlChar **string) { } -/* Convert struct tm *@time to UTF-8 ISO 8601 date-time @string. */ -static isds_error tm2timestring(const struct tm *time, xmlChar **string) { +/* Convert struct timeval * @time to UTF-8 ISO 8601 date-time @string. It + * respects the @time microseconds too. */ +static isds_error timeval2timestring(const struct timeval *time, + xmlChar **string) { + struct tm broken; + if (!time || !string) return IE_INVAL; - /* timezone is number of seconds west of GMT */ - long int zone_hours = - timezone / (60 * 60); - long int zone_minutes = ( ((timezone < 0) ? (-1 * timezone) : timezone) - % (60 * 60)) / 60; + if (!gmtime_r(&time->tv_sec, &broken)) return IE_DATE; /* TODO: small negative year should be formated as "-0012". This is not * true for glibc "%04d". We should implement it. + * TODO: What's type of time->tv_usec exactly? Unsigned? Absolute? * See */ if (-1 == isds_asprintf((char **) string, - "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:%02ld", - time->tm_year + 1900, time->tm_mon + 1, time->tm_mday, - time->tm_hour, time->tm_min, time->tm_sec, - zone_hours, zone_minutes)) + "%04d-%02d-%02dT%02d:%02d:%02d.%06ld", + broken.tm_year + 1900, broken.tm_mon + 1, broken.tm_mday, + broken.tm_hour, broken.tm_min, broken.tm_sec, + time->tv_usec)) return IE_ERROR; - return IE_SUCCESS; } + /* Following EXTRACT_* macros expects @result, @xpath_ctx, @err, @context * and leave lable */ #define EXTRACT_STRING(element, string) \ @@ -2130,7 +2128,7 @@ serialization_failed: * in case of error the list will be NULLed. * @return IE_SUCCESS or appropriate error code. */ isds_error isds_get_list_of_sent_messages(struct isds_ctx *context, - const struct tm *from_time, const struct tm *to_time, + const struct timeval *from_time, const struct timeval *to_time, const long int *dmSenderOrgUnitNum, const unsigned int status_filter, const unsigned long int offset, unsigned long int *number, struct isds_list **messages) { @@ -2168,14 +2166,14 @@ isds_error isds_get_list_of_sent_messages(struct isds_ctx *context, if (from_time) { - err = tm2timestring(from_time, &string); + err = timeval2timestring(from_time, &string); if (err) goto leave; } INSERT_STRING(request, "dmFromTime", string); free(string); string = NULL; if (to_time) { - err = tm2timestring(to_time, &string); + err = timeval2timestring(to_time, &string); if (err) goto leave; } INSERT_STRING(request, "dmToTime", string); diff --git a/src/isds.h b/src/isds.h index 9fd7b59..35dbb01 100644 --- a/src/isds.h +++ b/src/isds.h @@ -5,6 +5,7 @@ * Private declarations in isds_priv.h. */ #include /* For size_t */ +#include /* For struct timeval */ struct isds_ctx; /* Context for specific ISDS box */ @@ -442,7 +443,7 @@ isds_error isds_send_message(struct isds_ctx *context, * in case of error the list will be NULLed. * @return IE_SUCCESS or appropriate error code. */ isds_error isds_get_list_of_sent_messages(struct isds_ctx *context, - const struct tm *from_time, const struct tm *to_time, + const struct timeval *from_time, const struct timeval *to_time, const long int *dmSenderOrgUnitNum, const unsigned int status_filter, const unsigned long int offset, unsigned long int *number, struct isds_list **messages); -- 2.11.4.GIT