|
|
1.1 root 1: /* date_str.c */
2:
3: /* Synchronet date/time string conversion routines */
4:
1.1.1.2 ! root 5: /* $Id: date_str.c,v 1.21 2004/09/08 03:35:34 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 2004 Rob Swindell - http://www.synchro.net/copyright.html *
1.1 root 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:
1.1.1.2 ! root 40: const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
! 41: const char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
! 42: ,"Jul","Aug","Sep","Oct","Nov","Dec"};
! 43:
1.1 root 44: /****************************************************************************/
45: /* Converts a date string in format MM/DD/YY into unix time format */
46: /****************************************************************************/
47: time_t DLLCALL dstrtounix(scfg_t* cfg, char *instr)
48: {
49: char* p;
50: char* day;
51: char str[16];
52: struct tm tm;
53:
54: if(!instr[0] || !strncmp(instr,"00/00/00",8))
55: return(0);
56:
57: if(isdigit(instr[0]) && isdigit(instr[1])
58: && isdigit(instr[3]) && isdigit(instr[4])
59: && isdigit(instr[6]) && isdigit(instr[7]))
60: p=instr; /* correctly formatted */
61: else {
62: p=instr; /* incorrectly formatted */
63: while(*p && isdigit(*p)) p++;
64: if(*p==0)
65: return(0);
66: p++;
67: day=p;
68: while(*p && isdigit(*p)) p++;
69: if(*p==0)
70: return(0);
71: p++;
72: sprintf(str,"%02u/%02u/%02u"
73: ,atoi(instr)%100,atoi(day)%100,atoi(p)%100);
74: p=str;
75: }
76:
77: memset(&tm,0,sizeof(tm));
78: tm.tm_year=((p[6]&0xf)*10)+(p[7]&0xf);
79: if (tm.tm_year<Y2K_2DIGIT_WINDOW)
80: tm.tm_year+=100;
81: if(cfg->sys_misc&SM_EURODATE) {
82: tm.tm_mon=((p[3]&0xf)*10)+(p[4]&0xf);
83: tm.tm_mday=((p[0]&0xf)*10)+(p[1]&0xf); }
84: else {
85: tm.tm_mon=((p[0]&0xf)*10)+(p[1]&0xf);
86: tm.tm_mday=((p[3]&0xf)*10)+(p[4]&0xf); }
87: if (tm.tm_mon)
88: tm.tm_mon--; /* zero-based month field */
89: return(mktime(&tm));
90: }
91:
92: /****************************************************************************/
93: /* Converts unix time format (long - time_t) into a char str MM/DD/YY */
94: /****************************************************************************/
95: char* DLLCALL unixtodstr(scfg_t* cfg, time_t unix_time, char *str)
96: {
1.1.1.2 ! root 97: struct tm tm;
1.1 root 98:
99: if(!unix_time)
100: strcpy(str,"00/00/00");
101: else {
1.1.1.2 ! root 102: if(localtime_r(&unix_time,&tm)==NULL) {
1.1 root 103: strcpy(str,"00/00/00");
104: return(str);
105: }
1.1.1.2 ! root 106: if(tm.tm_mon>11) { /* DOS leap year bug */
! 107: tm.tm_mon=0;
! 108: tm.tm_year++; }
! 109: if(tm.tm_mday>31)
! 110: tm.tm_mday=1;
1.1 root 111: if(cfg->sys_misc&SM_EURODATE)
1.1.1.2 ! root 112: sprintf(str,"%02u/%02u/%02u",tm.tm_mday,tm.tm_mon+1
! 113: ,TM_YEAR(tm.tm_year));
1.1 root 114: else
1.1.1.2 ! root 115: sprintf(str,"%02u/%02u/%02u",tm.tm_mon+1,tm.tm_mday
! 116: ,TM_YEAR(tm.tm_year)); }
1.1 root 117: return(str);
118: }
119:
120: /****************************************************************************/
121: /* Takes the value 'sec' and makes a string the format HH:MM:SS */
122: /****************************************************************************/
123: char* DLLCALL sectostr(uint sec,char *str)
124: {
125: uchar hour,min,sec2;
126:
127: hour=(sec/60)/60;
128: min=(sec/60)-(hour*60);
129: sec2=sec-((min+(hour*60))*60);
130: sprintf(str,"%2.2d:%2.2d:%2.2d",hour,min,sec2);
131: return(str);
132: }
133:
1.1.1.2 ! root 134: /****************************************************************************/
! 135: /****************************************************************************/
! 136: char* DLLCALL hhmmtostr(scfg_t* cfg, struct tm* tm, char* str)
! 137: {
! 138: if(cfg->sys_misc&SM_MILITARY)
! 139: sprintf(str,"%02d:%02d "
! 140: ,tm->tm_hour,tm->tm_min);
! 141: else
! 142: sprintf(str,"%02d:%02d%c"
! 143: ,tm->tm_hour>12 ? tm->tm_hour-12 : tm->tm_hour==0 ? 12 : tm->tm_hour
! 144: ,tm->tm_min,tm->tm_hour>=12 ? 'p' : 'a');
! 145: return(str);
! 146: }
! 147:
! 148: /****************************************************************************/
! 149: /* Generates a 24 character ASCII string that represents the time_t pointer */
! 150: /* Used as a replacement for ctime() */
! 151: /****************************************************************************/
! 152: char* DLLCALL timestr(scfg_t* cfg, time_t *intime, char* str)
! 153: {
! 154: char* mer;
! 155: uchar hour;
! 156: struct tm tm;
! 157:
! 158: if(localtime_r(intime,&tm)==NULL) {
! 159: strcpy(str,"Invalid Time");
! 160: return(str);
! 161: }
! 162: if(cfg->sys_misc&SM_MILITARY) {
! 163: sprintf(str,"%s %s %02u %4u %02u:%02u:%02u"
! 164: ,wday[tm.tm_wday],mon[tm.tm_mon],tm.tm_mday,1900+tm.tm_year
! 165: ,tm.tm_hour,tm.tm_min,tm.tm_sec);
! 166: return(str);
! 167: }
! 168: if(tm.tm_hour>=12) {
! 169: if(tm.tm_hour==12)
! 170: hour=12;
! 171: else
! 172: hour=tm.tm_hour-12;
! 173: mer="pm";
! 174: } else {
! 175: if(tm.tm_hour==0)
! 176: hour=12;
! 177: else
! 178: hour=tm.tm_hour;
! 179: mer="am";
! 180: }
! 181: sprintf(str,"%s %s %02u %4u %02u:%02u %s"
! 182: ,wday[tm.tm_wday],mon[tm.tm_mon],tm.tm_mday,1900+tm.tm_year
! 183: ,hour,tm.tm_min,mer);
! 184: return(str);
! 185: }
1.1 root 186:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.