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