From patchwork Wed Mar 26 22:30:46 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Willmann X-Patchwork-Id: 334124 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ganesha.gnumonks.org (ganesha.gnumonks.org [IPv6:2001:780:45:1d:225:90ff:fe52:c662]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 08E5C14008A for ; Thu, 27 Mar 2014 09:33:02 +1100 (EST) Received: from localhost ([127.0.0.1] helo=ganesha.gnumonks.org) by ganesha.gnumonks.org with esmtp (Exim 4.72) (envelope-from ) id 1WSwNQ-00027V-P2; Wed, 26 Mar 2014 23:32:53 +0100 Received: from isonoe.totalueberwachung.de ([2a01:198:210:100::1]) by ganesha.gnumonks.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1WSwLr-00020G-DX for openbsc@lists.osmocom.org; Wed, 26 Mar 2014 23:31:17 +0100 Received: from adrastea.totalueberwachung.de (p5099b351.dip0.t-ipconnect.de [80.153.179.81]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by isonoe.totalueberwachung.de (Postfix) with ESMTPSA id E483660058; Wed, 26 Mar 2014 23:31:09 +0100 (CET) Received: by adrastea.totalueberwachung.de (Postfix, from userid 1000) id 66AA7220C1; Wed, 26 Mar 2014 23:31:07 +0100 (CET) From: Daniel Willmann To: OpenBSC Mailing List Subject: [PATCH 3/4] gsm0411_utils: Fix timezone offs calculation in gsm340_gen_scts() Date: Wed, 26 Mar 2014 23:30:46 +0100 Message-Id: <5e1c4810acada891fc3bdd36b5d49b03293ad7db.1395872056.git.daniel@totalueberwachung.de> X-Mailer: git-send-email 1.8.4.2 In-Reply-To: <1395873047-28229-1-git-send-email-dwillmann@sysmocom.de> References: <1395873047-28229-1-git-send-email-dwillmann@sysmocom.de> In-Reply-To: <64c11ccd40489136537a1d3e804c18e0d66440f4.1395872056.git.daniel@totalueberwachung.de> References: <64c11ccd40489136537a1d3e804c18e0d66440f4.1395872056.git.daniel@totalueberwachung.de> X-Spam-Score: -0.0 (/) Cc: Daniel Willmann X-BeenThere: openbsc@lists.osmocom.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Development of the OpenBSC GSM base station controller List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: openbsc-bounces@lists.osmocom.org Errors-To: openbsc-bounces@lists.osmocom.org The current code uses gmtime which returns the time in UTC. This is not the intended behaviour (otherwise the offset could always be set to zero). Iinstead of relying on tm_gmtoff this patch uses the (portable) gmtoffset_from_ts() function to determine the current time offset. Update the test with the expected result --- src/gsm/gsm0411_utils.c | 33 +++++++++++++++++---------------- tests/sms/sms_test.ok | 12 ------------ 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/gsm/gsm0411_utils.c b/src/gsm/gsm0411_utils.c index d857e41..c6d68bd 100644 --- a/src/gsm/gsm0411_utils.c +++ b/src/gsm/gsm0411_utils.c @@ -93,23 +93,24 @@ static time_t gmtoffset_from_ts(time_t time) /* Generate 03.40 TP-SCTS */ void gsm340_gen_scts(uint8_t *scts, time_t time) { - struct tm *tm = gmtime(&time); - - *scts++ = gsm411_bcdify(tm->tm_year % 100); - *scts++ = gsm411_bcdify(tm->tm_mon + 1); - *scts++ = gsm411_bcdify(tm->tm_mday); - *scts++ = gsm411_bcdify(tm->tm_hour); - *scts++ = gsm411_bcdify(tm->tm_min); - *scts++ = gsm411_bcdify(tm->tm_sec); -#ifdef HAVE_TM_GMTOFF_IN_TM - if (tm->tm_gmtoff >= 0) - *scts++ = gsm411_bcdify(tm->tm_gmtoff/(60*15)); + struct tm tm; + time_t gmtoffset; + + localtime_r(&time, &tm); + + *scts++ = gsm411_bcdify(tm.tm_year % 100); + *scts++ = gsm411_bcdify(tm.tm_mon + 1); + *scts++ = gsm411_bcdify(tm.tm_mday); + *scts++ = gsm411_bcdify(tm.tm_hour); + *scts++ = gsm411_bcdify(tm.tm_min); + *scts++ = gsm411_bcdify(tm.tm_sec); + + gmtoffset = gmtoffset_from_ts(time); + + if (gmtoffset >= 0) + *scts++ = gsm411_bcdify(gmtoffset/(60*15)); else - *scts++ = gsm411_bcdify(-tm->tm_gmtoff/(60*15)) | 0x08; -#else -#warning find a portable way to obtain timezone offset - *scts++ = 0; -#endif + *scts++ = gsm411_bcdify(-gmtoffset/(60*15)) | 0x08; } /* Decode 03.40 TP-SCTS (into utc/gmt timestamp) */ diff --git a/tests/sms/sms_test.ok b/tests/sms/sms_test.ok index a3d816c..a779f12 100644 --- a/tests/sms/sms_test.ok +++ b/tests/sms/sms_test.ok @@ -28,22 +28,10 @@ Result: len(9) data(0e d0 4f 78 d9 2d 9c 0e 01 ) Result: len(12) data(14 d0 4f 78 d9 2d 9c 0e c3 e2 31 19 ) Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ UTC Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ Europe/London -1364691600 -> 31301310000000 -> 1364695200 -Timezone Europe/London failed at ts 1364691600 Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ Europe/Berlin -1364695200 -> 31301320000000 -> 1364698800 -Timezone Europe/Berlin failed at ts 1364695200 Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ Europe/Athens -1364698800 -> 31301330000000 -> 1364702400 -Timezone Europe/Athens failed at ts 1364698800 Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ Europe/Moscow Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ Canada/Central -1362880800 -> 31300120000000 -> 1362884400 -Timezone Canada/Central failed at ts 1362880800 Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ America/New_York -1362880800 -> 31300120000000 -> 1362884400 -Timezone America/New_York failed at ts 1362880800 Testing gsm340_scts(gsm340_gen_scts(ts)) == ts for TZ America/Los_Angeles -1362880800 -> 31300120000000 -> 1362884400 -Timezone America/Los_Angeles failed at ts 1362880800 OK