|
|
1.1 root 1: /* date_str.c */
2:
3: /* Synchronet date/time string conversion routines */
4:
5: /* $Id: date_str.c,v 1.22 2006/04/05 09:45:21 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 2006 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: 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:
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: tm.tm_isdst=-1; /* Do not adjust for DST */
90: return(mktime(&tm));
91: }
92:
93: /****************************************************************************/
94: /* Converts unix time format (long - time_t) into a char str MM/DD/YY */
95: /****************************************************************************/
96: char* DLLCALL unixtodstr(scfg_t* cfg, time_t unix_time, char *str)
97: {
98: struct tm tm;
99:
100: if(!unix_time)
101: strcpy(str,"00/00/00");
102: else {
103: if(localtime_r(&unix_time,&tm)==NULL) {
104: strcpy(str,"00/00/00");
105: return(str);
106: }
107: if(tm.tm_mon>11) { /* DOS leap year bug */
108: tm.tm_mon=0;
109: tm.tm_year++; }
110: if(tm.tm_mday>31)
111: tm.tm_mday=1;
112: if(cfg->sys_misc&SM_EURODATE)
113: sprintf(str,"%02u/%02u/%02u",tm.tm_mday,tm.tm_mon+1
114: ,TM_YEAR(tm.tm_year));
115: else
116: sprintf(str,"%02u/%02u/%02u",tm.tm_mon+1,tm.tm_mday
117: ,TM_YEAR(tm.tm_year)); }
118: return(str);
119: }
120:
121: /****************************************************************************/
122: /* Takes the value 'sec' and makes a string the format HH:MM:SS */
123: /****************************************************************************/
124: char* DLLCALL sectostr(uint sec,char *str)
125: {
126: uchar hour,min,sec2;
127:
128: hour=(sec/60)/60;
129: min=(sec/60)-(hour*60);
130: sec2=sec-((min+(hour*60))*60);
131: sprintf(str,"%2.2d:%2.2d:%2.2d",hour,min,sec2);
132: return(str);
133: }
134:
135: /****************************************************************************/
136: /****************************************************************************/
137: char* DLLCALL hhmmtostr(scfg_t* cfg, struct tm* tm, char* str)
138: {
139: if(cfg->sys_misc&SM_MILITARY)
140: sprintf(str,"%02d:%02d "
141: ,tm->tm_hour,tm->tm_min);
142: else
143: sprintf(str,"%02d:%02d%c"
144: ,tm->tm_hour>12 ? tm->tm_hour-12 : tm->tm_hour==0 ? 12 : tm->tm_hour
145: ,tm->tm_min,tm->tm_hour>=12 ? 'p' : 'a');
146: return(str);
147: }
148:
149: /****************************************************************************/
150: /* Generates a 24 character ASCII string that represents the time_t pointer */
151: /* Used as a replacement for ctime() */
152: /****************************************************************************/
153: char* DLLCALL timestr(scfg_t* cfg, time_t *intime, char* str)
154: {
155: char* mer;
156: uchar hour;
157: struct tm tm;
158:
159: if(localtime_r(intime,&tm)==NULL) {
160: strcpy(str,"Invalid Time");
161: return(str);
162: }
163: if(cfg->sys_misc&SM_MILITARY) {
164: sprintf(str,"%s %s %02u %4u %02u:%02u:%02u"
165: ,wday[tm.tm_wday],mon[tm.tm_mon],tm.tm_mday,1900+tm.tm_year
166: ,tm.tm_hour,tm.tm_min,tm.tm_sec);
167: return(str);
168: }
169: if(tm.tm_hour>=12) {
170: if(tm.tm_hour==12)
171: hour=12;
172: else
173: hour=tm.tm_hour-12;
174: mer="pm";
175: } else {
176: if(tm.tm_hour==0)
177: hour=12;
178: else
179: hour=tm.tm_hour;
180: mer="am";
181: }
182: sprintf(str,"%s %s %02u %4u %02u:%02u %s"
183: ,wday[tm.tm_wday],mon[tm.tm_mon],tm.tm_mday,1900+tm.tm_year
184: ,hour,tm.tm_min,mer);
185: return(str);
186: }
187:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.