From 88148e0d0ba517f8de23e9113e929025b87024f9 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 29 Aug 2015 10:46:05 +0000
Subject: [PATCH] Fix: xpDateTime_to_time() would convert an xpDateTime_t which
 was in UTC to the local timezone equivalent (not adjusting for offset). Now
 using timegm() (aka mkgmtime()) to fix this scenario. Times in zones other
 than the local time zone or UTC are explicitly not supported by this function
 now (returns INVALID_TIME in that scenario).

---
 src/xpdev/xpdatetime.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/xpdev/xpdatetime.c b/src/xpdev/xpdatetime.c
index 2ebb074c1c..ed50017bfd 100644
--- a/src/xpdev/xpdatetime.c
+++ b/src/xpdev/xpdatetime.c
@@ -8,7 +8,7 @@
  * @format.tab-size 4		(Plain Text/Source Code File Header)			*
  * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
  *																			*
- * Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html		*
+ * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
  *																			*
  * This library is free software; you can redistribute it and/or			*
  * modify it under the terms of the GNU Lesser General Public License		*
@@ -123,6 +123,7 @@ xpTimeZone_t DLLCALL xpTimeZone_local(void)
 #endif
 }
 
+/* TODO: Supports local timezone and UTC only, currently */
 time_t DLLCALL xpDateTime_to_time(xpDateTime_t xpDateTime)
 {
 	struct tm tm;
@@ -140,7 +141,11 @@ time_t DLLCALL xpDateTime_to_time(xpDateTime_t xpDateTime)
 	tm.tm_min	= xpDateTime.time.minute;
 	tm.tm_sec	= (int)xpDateTime.time.second;
 
-	return sane_mktime(&tm);
+	if(xpDateTime.zone == xpTimeZone_UTC)
+		return sane_timegm(&tm);
+	if(xpDateTime.zone == xpTimeZone_LOCAL || xpDateTime.zone == xpTimeZone_local())
+		return sane_mktime(&tm);
+	return INVALID_TIME;
 }
 
 xpDateTime_t DLLCALL time_to_xpDateTime(time_t ti, xpTimeZone_t zone)
-- 
GitLab