From 41071737d02a92b64de0d83b3cd2c9d4cabecb37 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Fri, 5 Jun 2009 11:51:46 -0500 Subject: [PATCH] Add utility to convert SCTS to local/remote time --- src/smsutil.c | 37 +++++++++++++++++++++++++++++++++++++ src/smsutil.h | 2 ++ unit/test-sms.c | 16 ++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/smsutil.c b/src/smsutil.c index 6bbb57d..d44e73d 100644 --- a/src/smsutil.c +++ b/src/smsutil.c @@ -1484,3 +1484,40 @@ gboolean sms_udh_iter_next(struct sms_udh_iter *iter) return TRUE; } + +/* Returns both forms of time. The time_t value returns the time in local + * timezone. The struct tm is filled out with the remote time information + */ +time_t sms_scts_to_time(struct sms_scts *scts, struct tm *remote) +{ + struct tm t; + time_t ret; + + t.tm_sec = scts->second; + t.tm_min = scts->minute; + t.tm_hour = scts->hour; + t.tm_mday = scts->day; + t.tm_mon = scts->month - 1; + t.tm_isdst = -1; + + if (scts->year > 80) + t.tm_year = scts->year; + else + t.tm_year = scts->year + 100; + + ret = mktime(&t); + + /* Adjust local time by the local timezone information */ + ret += t.tm_gmtoff; + + /* Set the proper timezone on the remote side */ + t.tm_gmtoff = scts->timezone * 15 * 60; + + /* Now adjust by the remote timezone information */ + ret -= t.tm_gmtoff; + + if (remote) + memcpy(remote, &t, sizeof(struct tm)); + + return ret; +} diff --git a/src/smsutil.h b/src/smsutil.h index 940a185..566701c 100644 --- a/src/smsutil.h +++ b/src/smsutil.h @@ -295,4 +295,6 @@ guint8 sms_udh_iter_get_ie_length(struct sms_udh_iter *iter); void sms_udh_iter_get_ie_data(struct sms_udh_iter *iter, guint8 *data); gboolean sms_udh_iter_has_next(struct sms_udh_iter *iter); gboolean sms_udh_iter_next(struct sms_udh_iter *iter); + +time_t sms_scts_to_time(struct sms_scts *scts, struct tm *remote); #endif diff --git a/unit/test-sms.c b/unit/test-sms.c index d644cda..6057076 100644 --- a/unit/test-sms.c +++ b/unit/test-sms.c @@ -38,6 +38,10 @@ static const char *simple_submit = "0011000B916407281553F80000AA" static void print_scts(struct sms_scts *scts, const char *prefix) { + time_t ts; + struct tm remote; + char buf[128]; + g_print("%s: (YY-MM-DD) %02d-%02d-%02d\n", prefix, (int)scts->year, (int)scts->month, (int)scts->day); @@ -47,6 +51,18 @@ static void print_scts(struct sms_scts *scts, const char *prefix) g_print("%s: Timezone %d hours %d minutes\n", prefix, (int)scts->timezone / 4, (int)((abs(scts->timezone) % 4) * 15)); + + ts = sms_scts_to_time(scts, &remote); + + strftime(buf, 127, "%a, %d %b %Y %H:%M:%S %z", localtime(&ts)); + buf[127] = '\0'; + + g_print("local time: %s\n", buf); + + strftime(buf, 127, "%a, %d %b %Y %H:%M:%S %z", &remote); + buf[127] = '\0'; + + g_print("remote time: %s\n", buf); } static void print_vpf(enum sms_validity_period_format vpf, -- 2.7.4