Annotation of sbbs/src/xpdev/datewrap.c, revision 1.1.1.2

1.1       root        1: /* datewrap.c */
                      2: 
1.1.1.2 ! root        3: /* Wrappers for non-standard date and time functions */
1.1       root        4: 
1.1.1.2 ! root        5: /* $Id: datewrap.c,v 1.28 2008/02/23 22:18:37 rswindell Exp $ */
1.1       root        6: 
                      7: /****************************************************************************
                      8:  * @format.tab-size 4          (Plain Text/Source Code File Header)                    *
                      9:  * @format.use-tabs true       (see http://www.synchro.net/ptsc_hdr.html)              *
                     10:  *                                                                                                                                                     *
1.1.1.2 ! root       11:  * Copyright 2008 Rob Swindell - http://www.synchro.net/copyright.html         *
1.1       root       12:  *                                                                                                                                                     *
                     13:  * This library is free software; you can redistribute it and/or                       *
                     14:  * modify it under the terms of the GNU Lesser General Public License          *
                     15:  * as published by the Free Software Foundation; either version 2                      *
                     16:  * of the License, or (at your option) any later version.                                      *
                     17:  * See the GNU Lesser General Public License for more details: lgpl.txt or     *
                     18:  * http://www.fsf.org/copyleft/lesser.html                                                                     *
                     19:  *                                                                                                                                                     *
                     20:  * Anonymous FTP access to the most recent released source is available at     *
                     21:  * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net     *
                     22:  *                                                                                                                                                     *
                     23:  * Anonymous CVS access to the development source and modification history     *
                     24:  * is available at cvs.synchro.net:/cvsroot/sbbs, example:                                     *
                     25:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs login                       *
                     26:  *     (just hit return, no password is necessary)                                                     *
                     27:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src                *
                     28:  *                                                                                                                                                     *
                     29:  * For Synchronet coding style and modification guidelines, see                                *
                     30:  * http://www.synchro.net/source.html                                                                          *
                     31:  *                                                                                                                                                     *
                     32:  * You are encouraged to submit any modifications (preferably in Unix diff     *
                     33:  * format) via e-mail to [email protected]                                                                      *
                     34:  *                                                                                                                                                     *
                     35:  * Note: If this box doesn't appear square, then you need to fix your tabs.    *
                     36:  ****************************************************************************/
                     37: 
                     38: #include "string.h"    /* memset */
                     39: #include "genwrap.h"
1.1.1.2 ! root       40: #include "datewrap.h"
        !            41: 
        !            42: /* Return difference (in seconds) in time() result from standard */
        !            43: time_t checktime(void)
        !            44: {
        !            45:        time_t          t=0x2D24BD00L;  /* Correct time_t value on Jan-1-1994 */
        !            46:        struct tm       gmt;
        !            47:        struct tm       tm;
        !            48: 
        !            49:        memset(&tm,0,sizeof(tm));
        !            50:        tm.tm_year=94;
        !            51:        tm.tm_mday=1;
        !            52:        return mktime(&tm) - mktime(gmtime_r(&t,&gmt));
        !            53: }
1.1       root       54: 
                     55: /* Compensates for struct tm "weirdness" */
                     56: time_t sane_mktime(struct tm* tm)
                     57: {
                     58:        if(tm->tm_year>=1900)
                     59:                tm->tm_year-=1900;
                     60:        if(tm->tm_mon)          /* Month is zero-based */
                     61:                tm->tm_mon--;
                     62:        tm->tm_isdst=-1;        /* Auto-adjust for DST */
                     63: 
                     64:        return mktime(tm);
                     65: }
                     66: 
1.1.1.2 ! root       67: #if !defined(__BORLANDC__)
1.1       root       68: 
                     69: /***********************************/
                     70: /* Borland DOS date/time functions */
                     71: /***********************************/
                     72: 
                     73: #if defined(_WIN32)
                     74:        #include <windows.h>    /* SYSTEMTIME and GetLocalTime() */
                     75: #else
                     76:        #include <sys/time.h>   /* stuct timeval, gettimeofday() */
                     77: #endif
                     78: 
                     79: void xp_getdate(struct date* nyd)
                     80: {
                     81:        time_t tim;
                     82:        struct tm dte;
                     83: 
                     84:        tim=time(NULL);
                     85:        localtime_r(&tim,&dte);
                     86:        nyd->da_year=dte.tm_year+1900;
                     87:        nyd->da_day=dte.tm_mday;
                     88:        nyd->da_mon=dte.tm_mon+1;
                     89: }
                     90: 
                     91: void gettime(struct time* nyt)
                     92: {
                     93: #if defined(_WIN32)
                     94:        SYSTEMTIME systime;
                     95: 
                     96:        GetLocalTime(&systime);
                     97:        nyt->ti_hour=(unsigned char)systime.wHour;
                     98:        nyt->ti_min=(unsigned char)systime.wMinute;
                     99:        nyt->ti_sec=(unsigned char)systime.wSecond;
                    100:        nyt->ti_hund=systime.wMilliseconds/10;
                    101: #else  /* !Win32 (e.g. Unix) */
                    102:        struct tm dte;
                    103:        time_t t;
                    104:        struct timeval tim;
                    105: 
                    106:        gettimeofday(&tim, NULL);
                    107:        t=tim.tv_sec;
                    108:        localtime_r(&t,&dte);
                    109:        nyt->ti_min=dte.tm_min;
                    110:        nyt->ti_hour=dte.tm_hour;
                    111:        nyt->ti_sec=dte.tm_sec;
                    112:        nyt->ti_hund=tim.tv_usec/10000;
                    113: #endif
                    114: }
                    115: 
                    116: #endif /* !Borland */
1.1.1.2 ! root      117: 
        !           118: #if !defined(__unix__)
        !           119: 
        !           120: /****************************************************************************/
        !           121: /* Win32 implementations of the recursive (thread-safe) versions of std C      */
        !           122: /* time functions (gmtime, localtime, ctime, and asctime) used in Unix.                */
        !           123: /* The native Win32 versions of these functions are already thread-safe.       */
        !           124: /****************************************************************************/
        !           125: 
        !           126: struct tm* DLLCALL gmtime_r(const time_t* t, struct tm* tm)
        !           127: {
        !           128:        struct tm* tmp = gmtime(t);
        !           129: 
        !           130:        if(tmp==NULL)
        !           131:                return(NULL);
        !           132: 
        !           133:        *tm = *tmp;
        !           134:        return(tm);
        !           135: }
        !           136: 
        !           137: struct tm* DLLCALL localtime_r(const time_t* t, struct tm* tm)
        !           138: {
        !           139:        struct tm* tmp = localtime(t);
        !           140: 
        !           141:        if(tmp==NULL)
        !           142:                return(NULL);
        !           143: 
        !           144:        *tm = *tmp;
        !           145:        return(tm);
        !           146: }
        !           147: 
        !           148: char* DLLCALL ctime_r(const time_t *t, char *buf)
        !           149: {
        !           150:        char* p = ctime(t);
        !           151: 
        !           152:        if(p==NULL)
        !           153:                return(NULL);
        !           154: 
        !           155:        strcpy(buf,p);
        !           156:        return(buf);
        !           157: }
        !           158: 
        !           159: char* DLLCALL asctime_r(const struct tm *tm, char *buf)
        !           160: {
        !           161:        char* p = asctime(tm);
        !           162: 
        !           163:        if(p==NULL)
        !           164:                return(NULL);
        !           165: 
        !           166:        strcpy(buf,p);
        !           167:        return(buf);
        !           168: }
        !           169: 
        !           170: #endif /* !defined(__unix__) */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.