Annotation of sbbs/sbbs3/date_str.c, revision 1.1.1.1

1.1       root        1: /* date_str.c */
                      2: 
                      3: /* Synchronet date/time string conversion routines */
                      4: 
                      5: /* $Id: date_str.c,v 1.5 2000/11/14 02:29:08 rswindell Exp $ */
                      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:  *                                                                                                                                                     *
                     11:  * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html         *
                     12:  *                                                                                                                                                     *
                     13:  * This program is free software; you can redistribute it and/or                       *
                     14:  * modify it under the terms of the GNU 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 General Public License for more details: gpl.txt or                     *
                     18:  * http://www.fsf.org/copyleft/gpl.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 "sbbs.h"
                     39: 
                     40: /****************************************************************************/
                     41: /* Converts a date string in format MM/DD/YY into unix time format                     */
                     42: /****************************************************************************/
                     43: time_t DLLCALL dstrtounix(scfg_t* cfg, char *instr)
                     44: {
                     45:        char*   p;
                     46:        char*   day;
                     47:        char    str[16];
                     48:        struct tm tm;
                     49: 
                     50:        if(!instr[0] || !strncmp(instr,"00/00/00",8))
                     51:                return(0);
                     52: 
                     53:        if(isdigit(instr[0]) && isdigit(instr[1])
                     54:                && isdigit(instr[3]) && isdigit(instr[4])
                     55:                && isdigit(instr[6]) && isdigit(instr[7]))
                     56:                p=instr;        /* correctly formatted */
                     57:        else {
                     58:                p=instr;        /* incorrectly formatted */
                     59:                while(*p && isdigit(*p)) p++;
                     60:                if(*p==0)
                     61:                        return(0);
                     62:                p++;
                     63:                day=p;
                     64:                while(*p && isdigit(*p)) p++;
                     65:                if(*p==0)
                     66:                        return(0);
                     67:                p++;
                     68:                sprintf(str,"%02u/%02u/%02u"
                     69:                        ,atoi(instr)%100,atoi(day)%100,atoi(p)%100);
                     70:                p=str;
                     71:        }
                     72: 
                     73:        memset(&tm,0,sizeof(tm));
                     74:        tm.tm_year=((p[6]&0xf)*10)+(p[7]&0xf);
                     75:        if (tm.tm_year<Y2K_2DIGIT_WINDOW)
                     76:                tm.tm_year+=100;
                     77:        if(cfg->sys_misc&SM_EURODATE) {
                     78:                tm.tm_mon=((p[3]&0xf)*10)+(p[4]&0xf);
                     79:                tm.tm_mday=((p[0]&0xf)*10)+(p[1]&0xf); }
                     80:        else {
                     81:                tm.tm_mon=((p[0]&0xf)*10)+(p[1]&0xf);
                     82:                tm.tm_mday=((p[3]&0xf)*10)+(p[4]&0xf); }
                     83:        if (tm.tm_mon)
                     84:                tm.tm_mon--;    /* zero-based month field */
                     85:        return(mktime(&tm));
                     86: }
                     87: 
                     88: /****************************************************************************/
                     89: /* Converts unix time format (long - time_t) into a char str MM/DD/YY          */
                     90: /****************************************************************************/
                     91: char* DLLCALL unixtodstr(scfg_t* cfg, time_t unix_time, char *str)
                     92: {
                     93:        struct tm* tm;
                     94: 
                     95:        if(!unix_time)
                     96:                strcpy(str,"00/00/00");
                     97:        else {
                     98:                tm=gmtime(&unix_time);
                     99:                if(tm==NULL) {
                    100:                        strcpy(str,"00/00/00");
                    101:                        return(str);
                    102:                }
                    103:                if(tm->tm_mon>11) {       /* DOS leap year bug */
                    104:                        tm->tm_mon=0;
                    105:                        tm->tm_year++; }
                    106:                if(tm->tm_mday>31)
                    107:                        tm->tm_mday=1;
                    108:                if(cfg->sys_misc&SM_EURODATE)
                    109:                        sprintf(str,"%02u/%02u/%02u",tm->tm_mday,tm->tm_mon+1
                    110:                                ,TM_YEAR(tm->tm_year));
                    111:                else
                    112:                        sprintf(str,"%02u/%02u/%02u",tm->tm_mon+1,tm->tm_mday
                    113:                                ,TM_YEAR(tm->tm_year)); }
                    114:        return(str);
                    115: }
                    116: 
                    117: /****************************************************************************/
                    118: /* Takes the value 'sec' and makes a string the format HH:MM:SS             */
                    119: /****************************************************************************/
                    120: char* DLLCALL sectostr(uint sec,char *str)
                    121: {
                    122:     uchar hour,min,sec2;
                    123: 
                    124:        hour=(sec/60)/60;
                    125:        min=(sec/60)-(hour*60);
                    126:        sec2=sec-((min+(hour*60))*60);
                    127:        sprintf(str,"%2.2d:%2.2d:%2.2d",hour,min,sec2);
                    128:        return(str);
                    129: }
                    130: 
                    131: 

unix.superglobalmegacorp.com

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