From c43eef30dfedda5b907a23ee74d71bffe7195231 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 29 Aug 2015 10:40:15 +0000
Subject: [PATCH] Define a MSFT macro for the GLIBC/BSD timegm() function and a
 sane_timegm() equivalent to sane_mktime() for broken-down date/time's in UTC.

---
 src/xpdev/datewrap.c | 14 +++++++++++++-
 src/xpdev/datewrap.h | 10 ++++++++--
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/xpdev/datewrap.c b/src/xpdev/datewrap.c
index 3c055e2fc1..ac9b0d3e08 100644
--- a/src/xpdev/datewrap.c
+++ b/src/xpdev/datewrap.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 2011 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		*
@@ -64,6 +64,18 @@ time_t DLLCALL sane_mktime(struct tm* tm)
 	return mktime(tm);
 }
 
+/* Compensates for struct tm "weirdness" */
+time_t DLLCALL sane_timegm(struct tm* tm)
+{
+	if(tm->tm_year>=1900)
+		tm->tm_year-=1900;
+	if(tm->tm_mon)		/* Month is zero-based */
+		tm->tm_mon--;
+	tm->tm_isdst=0;		/* Don't adjust for DST */
+
+	return timegm(tm);
+}
+
 time32_t DLLCALL time32(time32_t* tp)
 {
 	time_t t;
diff --git a/src/xpdev/datewrap.h b/src/xpdev/datewrap.h
index 34b967c298..7ffd3cbff5 100644
--- a/src/xpdev/datewrap.h
+++ b/src/xpdev/datewrap.h
@@ -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 2011 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		*
@@ -47,8 +47,9 @@ extern "C" {
 /* Return difference (in seconds) in time() result from standard (0 on success) */
 DLLEXPORT time_t		DLLCALL		checktime(void);
 
-/* Implementation of mktime() that handles common tm element conversions for you */
+/* Implementation of mktime()/timegm() that handles common tm element conversions for you */
 DLLEXPORT time_t		DLLCALL		sane_mktime(struct tm*);
+DLLEXPORT time_t		DLLCALL		sane_timegm(struct tm*);
 
 /* Legacy (32-bit time_t) versions of time() and mktime() */
 DLLEXPORT time32_t		DLLCALL		time32(time32_t* tp);
@@ -71,6 +72,11 @@ DLLEXPORT struct tm*	DLLCALL		localtime32(const time32_t* t, struct tm* tm);
 
 #endif
 
+/* Microsoft's equivalent of GLIBC/BSD timegm() */
+#ifdef _MSC_VER
+	#define timegm _mkgmtime
+#endif
+
 /***********************************/
 /* Borland DOS date/time functions */
 /***********************************/
-- 
GitLab