Skip to content
Snippets Groups Projects
Commit b5f273c1 authored by rswindell's avatar rswindell
Browse files

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).
parent 93189589
Branches
Tags
No related merge requests found
......@@ -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() */
......
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment