--- sbbs/src/xpdev/datewrap.c 2018/04/24 16:41:24 1.1.1.1 +++ sbbs/src/xpdev/datewrap.c 2018/04/24 16:45:27 1.1.1.2 @@ -1,14 +1,14 @@ /* datewrap.c */ -/* Wrappers for Borland getdate() and gettime() functions */ +/* Wrappers for non-standard date and time functions */ -/* $Id: datewrap.c,v 1.1.1.1 2018/04/24 16:41:24 root Exp $ */ +/* $Id: datewrap.c,v 1.1.1.2 2018/04/24 16:45:27 root Exp $ */ /**************************************************************************** * @format.tab-size 4 (Plain Text/Source Code File Header) * * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) * * * - * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html * + * Copyright 2008 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 * @@ -37,7 +37,20 @@ #include "string.h" /* memset */ #include "genwrap.h" -#include "datewrap.h" /* xpDateTime_t */ +#include "datewrap.h" + +/* Return difference (in seconds) in time() result from standard */ +time_t checktime(void) +{ + time_t t=0x2D24BD00L; /* Correct time_t value on Jan-1-1994 */ + struct tm gmt; + struct tm tm; + + memset(&tm,0,sizeof(tm)); + tm.tm_year=94; + tm.tm_mday=1; + return mktime(&tm) - mktime(gmtime_r(&t,&gmt)); +} /* Compensates for struct tm "weirdness" */ time_t sane_mktime(struct tm* tm) @@ -51,256 +64,18 @@ time_t sane_mktime(struct tm* tm) return mktime(tm); } -/**************************************/ -/* Cross-platform date/time functions */ -/**************************************/ - -xpDateTime_t xpDateTime_create(unsigned year, unsigned month, unsigned day - ,unsigned hour, unsigned minute, float second - ,xpTimeZone_t zone) -{ - xpDateTime_t xpDateTime; - - xpDateTime.date.year = year; - xpDateTime.date.month = month; - xpDateTime.date.day = day; - xpDateTime.time.hour = hour; - xpDateTime.time.minute = minute; - xpDateTime.time.second = second; - xpDateTime.zone = zone; - - return xpDateTime; -} - -xpDateTime_t xpDateTime_now(void) -{ -#if defined(_WIN32) - SYSTEMTIME systime; - - GetLocalTime(&systime); - return(xpDateTime_create(systime.wYear,systime.wMonth,systime.wDay - ,systime.wHour,systime.wMinute,(float)systime.wSecond+(systime.wMilliseconds*0.001F) - ,xpTimeZone_local())); -#else /* !Win32 (e.g. Unix) */ - struct tm tm; - struct timeval tv; - time_t t; - - gettimeofday(&tv, NULL); - t=tv.tv_sec; - localtime_r(&t,&tm); - - return xpDateTime_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday - ,tm.tm_hour,tm.tm_min,(float)tm.tm_sec+(tv.tv_usec*0.00001) - ,xpTimeZone_local()); -#endif -} - -xpTimeZone_t xpTimeZone_local(void) -{ -#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DARWIN__) - struct tm tm; - time_t t; - - localtime_r(&t, &tm); - return(tm.tm_gmtoff/60); -#else -#if defined(__BORLANDC__) || defined(__CYGWIN__) - #define timezone _timezone -#endif - - /* Converts (_)timezone from seconds west of UTC to minutes east of UTC */ - return -timezone/60; -#endif -} - -time_t xpDateTime_to_time(xpDateTime_t xpDateTime) -{ - struct tm tm; - - ZERO_VAR(tm); - - if(xpDateTime.date.year==0) - return(0); - - tm.tm_year = xpDateTime.date.year; - tm.tm_mon = xpDateTime.date.month; - tm.tm_mday = xpDateTime.date.day; - - tm.tm_hour = xpDateTime.time.hour; - tm.tm_min = xpDateTime.time.minute; - tm.tm_sec = (int)xpDateTime.time.second; - - return sane_mktime(&tm); - -} - -xpDateTime_t time_to_xpDateTime(time_t ti) -{ - xpDateTime_t never; - struct tm tm; - - memset(&never,0,sizeof(never)); - if(ti==0) - return(never); - - ZERO_VAR(tm); - if(gmtime_r(&ti,&tm)==NULL) - return(never); - - return xpDateTime_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday - ,tm.tm_hour,tm.tm_min,(float)tm.tm_sec,xpTimeZone_local()); -} - -/**********************************************/ -/* Decimal-coded ISO-8601 date/time functions */ -/**********************************************/ - -isoDate_t xpDateTime_to_isoDateTime(xpDateTime_t xpDateTime, isoTime_t* isoTime) -{ - if(isoTime!=NULL) - *isoTime=0; - - if(xpDateTime.date.year==0) - return(0); - - if(isoTime!=NULL) - *isoTime=isoTime_create(xpDateTime.time.hour,xpDateTime.time.minute,xpDateTime.time.second); - - return isoDate_create(xpDateTime.date.year,xpDateTime.date.month,xpDateTime.date.day); -} - -xpDateTime_t isoDateTime_to_xpDateTime(isoDate_t date, isoTime_t ti) -{ - return xpDateTime_create(isoDate_year(date),isoDate_month(date),isoDate_day(date) - ,isoTime_hour(ti),isoTime_minute(ti),(float)isoTime_second(ti),xpTimeZone_local()); -} - -isoDate_t time_to_isoDateTime(time_t ti, isoTime_t* isoTime) -{ - struct tm tm; - - if(isoTime!=NULL) - *isoTime=0; - - if(ti==0) - return(0); - - ZERO_VAR(tm); - if(gmtime_r(&ti,&tm)==NULL) - return(0); - - if(isoTime!=NULL) - *isoTime=isoTime_create(tm.tm_hour,tm.tm_min,tm.tm_sec); - - return isoDate_create(1900+tm.tm_year,1+tm.tm_mon,tm.tm_mday); -} - -isoTime_t time_to_isoTime(time_t ti) -{ - isoTime_t isoTime; - - time_to_isoDateTime(ti,&isoTime); - - return isoTime; -} - -time_t isoDateTime_to_time(isoDate_t date, isoTime_t ti) -{ - struct tm tm; - - ZERO_VAR(tm); - - if(date==0) - return(0); - - tm.tm_year = isoDate_year(date); - tm.tm_mon = isoDate_month(date); - tm.tm_mday = isoDate_day(date); - - tm.tm_hour = isoTime_hour(ti); - tm.tm_min = isoTime_minute(ti); - tm.tm_sec = isoTime_second(ti); - - return sane_mktime(&tm); -} - -BOOL isoTimeZone_parse(const char* str, xpTimeZone_t* zone) -{ - unsigned hour=0,minute=0; - - switch(*str) { - case 0: /* local time-zone */ - *zone = xpTimeZone_local(); - return TRUE; - case 'Z': /* UTC */ - *zone = 0; - return TRUE; - case '+': - case '-': /* "+/- HH[:]MM" */ - if(sscanf(str+1,"%2u%*s%2u",&hour,&minute)>=1) { - *zone = (hour*60) + minute; - if(*str=='-') - *zone = -(*zone); - return TRUE; - } - break; - } - return FALSE; -} - -xpDateTime_t isoDateTime_parse(const char* str) -{ - char zone[16]; - xpDateTime_t xpDateTime; - - zone[0]=0; - ZERO_VAR(xpDateTime); - - if((sscanf(str,"%4u-%2u-%2uT%2u:%2u:%f%6s" /* CCYY-MM-DDThh:MM:ss±hhmm */ - ,&xpDateTime.date.year - ,&xpDateTime.date.month - ,&xpDateTime.date.day - ,&xpDateTime.time.hour - ,&xpDateTime.time.minute - ,&xpDateTime.time.second - ,zone)>=2 - || sscanf(str,"%4u%2u%2uT%2u%2u%f%6s" /* CCYYMMDDThhmmss±hhmm */ - ,&xpDateTime.date.year - ,&xpDateTime.date.month - ,&xpDateTime.date.day - ,&xpDateTime.time.hour - ,&xpDateTime.time.minute - ,&xpDateTime.time.second - ,zone)>=4 - || sscanf(str,"%4u%2u%2u%2u%2u%f%6s" /* CCYYMMDDhhmmss±hhmm */ - ,&xpDateTime.date.year - ,&xpDateTime.date.month - ,&xpDateTime.date.day - ,&xpDateTime.time.hour - ,&xpDateTime.time.minute - ,&xpDateTime.time.second - ,zone)>=1 - ) && isoTimeZone_parse(zone,&xpDateTime.zone)) - return xpDateTime; - - return xpDateTime; -} +#if !defined(__BORLANDC__) /***********************************/ /* Borland DOS date/time functions */ /***********************************/ -#if !defined(__BORLANDC__) - #if defined(_WIN32) #include /* SYSTEMTIME and GetLocalTime() */ #else #include /* stuct timeval, gettimeofday() */ #endif -#include "datewrap.h" /* struct defs, verify prototypes */ - void xp_getdate(struct date* nyd) { time_t tim; @@ -339,3 +114,57 @@ void gettime(struct time* nyt) } #endif /* !Borland */ + +#if !defined(__unix__) + +/****************************************************************************/ +/* Win32 implementations of the recursive (thread-safe) versions of std C */ +/* time functions (gmtime, localtime, ctime, and asctime) used in Unix. */ +/* The native Win32 versions of these functions are already thread-safe. */ +/****************************************************************************/ + +struct tm* DLLCALL gmtime_r(const time_t* t, struct tm* tm) +{ + struct tm* tmp = gmtime(t); + + if(tmp==NULL) + return(NULL); + + *tm = *tmp; + return(tm); +} + +struct tm* DLLCALL localtime_r(const time_t* t, struct tm* tm) +{ + struct tm* tmp = localtime(t); + + if(tmp==NULL) + return(NULL); + + *tm = *tmp; + return(tm); +} + +char* DLLCALL ctime_r(const time_t *t, char *buf) +{ + char* p = ctime(t); + + if(p==NULL) + return(NULL); + + strcpy(buf,p); + return(buf); +} + +char* DLLCALL asctime_r(const struct tm *tm, char *buf) +{ + char* p = asctime(tm); + + if(p==NULL) + return(NULL); + + strcpy(buf,p); + return(buf); +} + +#endif /* !defined(__unix__) */