From b5f273c1ecacbf2582bcb6e40103e6000ca48fb7 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Thu, 23 Jun 2005 08:30:21 +0000
Subject: [PATCH] Introduce the concept of decimal-coded dates: can represent
 any date in an easy to read/parse integer format. Used mainly for dates <
 Jan-1-1970 (e.g. birth-dates) and quick-and-easy month/day comparisons
 without using tm structs and time_t conversions (and all their
 peculiarities). Obviously these integers do not contain any *time*
 information. Created time_t to/from decimal-coded date functions (obviously
 only useful for dates in the 1970-2038 range).

---
 src/xpdev/datewrap.c | 41 +++++++++++++++++++++++++++++++++++++++--
 src/xpdev/datewrap.h |  6 ++++++
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/src/xpdev/datewrap.c b/src/xpdev/datewrap.c
index 34eb53e25d..82c07e338c 100644
--- a/src/xpdev/datewrap.c
+++ b/src/xpdev/datewrap.c
@@ -35,9 +35,46 @@
  * Note: If this box doesn't appear square, then you need to fix your tabs.	*
  ****************************************************************************/
 
-#if !defined(__BORLANDC__)
+#include "genwrap.h"
+
+/* Decimal-coded date functions */
+long time_to_date(time_t time)
+{
+	struct tm tm;
+
+	if(time==0)
+		return(0);
+
+	ZERO_VAR(tm);
+	if(gmtime_r(&time,&tm)==NULL)
+		return(0);
+	return(((tm.tm_year+1900)*10000)+((tm.tm_mon+1)*100)+tm.tm_mday);
+}
+
+time_t date_to_time(long date)
+{
+	struct tm tm;
 
-#include <time.h>	/* time(), time_t, struct tm, localtime() */
+	ZERO_VAR(tm);
+
+	if(date==0)
+		return(0);
+
+	tm.tm_year=date/10000;
+	tm.tm_mon=(date/100)%100;
+	tm.tm_mday=date%100;
+
+	/* correct for tm-wierdness */
+	if(tm.tm_year>=1900)
+		tm.tm_year-=1900;
+	if(tm.tm_mon)
+		tm.tm_mon--;
+	tm.tm_isdst=-1;	/* Auto-adjust for DST */
+
+	return(mktime(&tm));
+}
+
+#if !defined(__BORLANDC__)
 
 #if defined(_WIN32)
 	#include <windows.h>	/* SYSTEMTIME and GetLocalTime() */
diff --git a/src/xpdev/datewrap.h b/src/xpdev/datewrap.h
index ccb9b046f6..5b68a865dc 100644
--- a/src/xpdev/datewrap.h
+++ b/src/xpdev/datewrap.h
@@ -38,6 +38,12 @@
 #ifndef _DATEWRAP_H_
 #define _DATEWRAP_H_
 
+#include "genwrap.h"	/* time_t */
+
+/* Decimal-coded date functions */
+long	time_to_date(time_t time);
+time_t	date_to_time(long date);
+
 #if defined(__BORLANDC__)
 
 #include <dos.h>
-- 
GitLab