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

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/OpenBSC@lists.osmocom.org/.

Daniel Willmann dwillmann at sysmocom.de
Wed Mar 26 22:30:45 UTC 2014


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(+)

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)
 {
-- 
1.8.4.2





More information about the OpenBSC mailing list