Patchwork [2/4] gsm0411_utils: Add helper function to get the gmt offset of a time_t

login
register
mail settings
Submitter Daniel Willmann
Date March 26, 2014, 10:30 p.m.
Message ID <96a6779baa1e1cc8bc1c05ce824928bbe7d240ed.1395872056.git.daniel@totalueberwachung.de>
Download mbox | patch
Permalink /patch/334123/
State New
Headers show

Comments

Daniel Willmann - March 26, 2014, 10:30 p.m.
The function uses localtime as well as gmtime to calculate the timezone
offset at a specific time. This is useful for the *_scts functions so
we're not dependent on non-portable features (tm_gmtoff).
---
 src/gsm/gsm0411_utils.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Patch

diff --git a/src/gsm/gsm0411_utils.c b/src/gsm/gsm0411_utils.c
index ad9753e..d857e41 100644
--- a/src/gsm/gsm0411_utils.c
+++ b/src/gsm/gsm0411_utils.c
@@ -72,6 +72,24 @@  uint8_t gsm411_unbcdify(uint8_t value)
 	return ret;
 }
 
+/* Figure out the timezone offset in a portable way.
+ * The idea is to convert the time_t into local and UTC struct tm
+ * representations and then calculate the difference of both. */
+static time_t gmtoffset_from_ts(time_t time)
+{
+	struct tm tm_local, tm_utc;
+	time_t ts_local, ts_utc;
+
+	localtime_r(&time, &tm_local);
+	gmtime_r(&time, &tm_utc);
+	tm_utc.tm_isdst = 0;
+	tm_local.tm_isdst = 0;
+	ts_utc = mktime(&tm_utc);
+	ts_local = mktime(&tm_local);
+
+	return ts_local - ts_utc;
+}
+
 /* Generate 03.40 TP-SCTS */
 void gsm340_gen_scts(uint8_t *scts, time_t time)
 {