From 6a77a7b3fbb66da2647d10a71678becbdc82a202 Mon Sep 17 00:00:00 2001 From: Vinicius Costa Gomes Date: Thu, 10 Jun 2010 11:51:37 -0300 Subject: [PATCH] obexd: Fix using UTC time on exported dates Some devices are not able to calculate the localtime from a UTC time, so the most portable way is to always provide the clients with the localtime. --- obexd/plugins/phonebook-tracker.c | 42 +++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/obexd/plugins/phonebook-tracker.c b/obexd/plugins/phonebook-tracker.c index e38370999..619d36162 100644 --- a/obexd/plugins/phonebook-tracker.c +++ b/obexd/plugins/phonebook-tracker.c @@ -22,6 +22,8 @@ #include #include +#include +#include #include #include #include @@ -483,6 +485,42 @@ static int query_tracker(const char *query, int num_fields, return 0; } +static char *iso8601_utc_to_localtime(const char *datetime) +{ + time_t time; + struct tm tm, *local; + char localdate[32]; + char tz; + int nr; + + memset(&tm, 0, sizeof(tm)); + + nr = sscanf(datetime, "%04u-%02u-%02uT%02u:%02u:%02u%c", + &tm.tm_year, &tm.tm_mon, &tm.tm_mday, + &tm.tm_hour, &tm.tm_min, &tm.tm_sec, + &tz); + if (nr < 6) { + /* Invalid time format */ + return g_strdup(""); + } + + /* Time already in localtime */ + if (nr == 6) + return g_strdup(datetime); + + tm.tm_year -= 1900; /* Year since 1900 */ + tm.tm_mon--; /* Months since January, values 0-11 */ + + time = mktime(&tm); + time -= timezone; + + local = localtime(&time); + + strftime(localdate, sizeof(localdate), "%Y-%m-%dT%H:%M:%S", local); + + return g_strdup(localdate); +} + static void set_call_type(struct phonebook_contact *contact, const char *datetime, const char *is_sent, const char *is_answered) @@ -505,8 +543,8 @@ static void set_call_type(struct phonebook_contact *contact, else contact->calltype = CALL_TYPE_OUTGOING; - /* Tracker already gives time in the ISO 8601 format */ - contact->datetime = g_strdup(datetime); + /* Tracker gives time in the ISO 8601 format, UTC time */ + contact->datetime = iso8601_utc_to_localtime(datetime); } static void pull_contacts(char **reply, int num_fields, void *user_data) -- 2.47.3