|
|
1.1 root 1: /* xpdatetime.h */
2:
3: /* Cross-platform (and eXtra Precision) date/time functions */
4:
5: /* $Id: xpdatetime.h,v 1.3 2008/02/23 21:05:30 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 2008 Rob Swindell - http://www.synchro.net/copyright.html *
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: #ifndef _XPDATETIME_H_
39: #define _XPDATETIME_H_
40:
41: #include "gen_defs.h" /* uint32_t and time_t */
42:
43: #if defined(__cplusplus)
44: extern "C" {
45: #endif
46:
47: /**************************************/
48: /* Cross-platform date/time functions */
49: /**************************************/
50:
51: #define INVALID_TIME (time_t)-1 /* time_t representation of an invalid date/time */
52:
53: typedef struct {
54: unsigned year; /* 0-9999 */
55: unsigned month; /* 1-12 */
56: unsigned day; /* 1-31 */
57: } xpDate_t;
58:
59: typedef struct {
60: unsigned hour; /* 0-23 */
61: unsigned minute; /* 0-59 */
62: float second; /* 0.0-59.999, supports fractional seconds */
63: } xpTime_t;
64:
65: typedef int xpTimeZone_t;
66: #define xpTimeZone_UTC 0
67: #define xpTimeZone_LOCAL 1
68:
69: typedef struct {
70: xpDate_t date;
71: xpTime_t time;
72: xpTimeZone_t zone; /* minutes +/- UTC */
73: } xpDateTime_t;
74:
75: xpDateTime_t xpDateTime_create(unsigned year, unsigned month, unsigned day
76: ,unsigned hour, unsigned minute, float second
77: ,xpTimeZone_t);
78: xpDateTime_t xpDateTime_now(void);
79: time_t xpDateTime_to_time(xpDateTime_t);
80: xpDateTime_t time_to_xpDateTime(time_t, xpTimeZone_t);
81: xpDateTime_t gmtime_to_xpDateTime(time_t);
82: xpTimeZone_t xpTimeZone_local(void);
83:
84: /**********************************************/
85: /* Decimal-coded ISO-8601 date/time functions */
86: /**********************************************/
87:
88: typedef uint32_t isoDate_t; /* CCYYMMDD (decimal) */
89: typedef uint32_t isoTime_t; /* HHMMSS (decimal) */
90:
91: #define isoDate_create(year,mon,day) (((year)*10000)+((mon)*100)+(day))
92: #define isoTime_create(hour,min,sec) (((hour)*10000)+((min)*100)+((unsigned)sec))
93:
94: #define isoDate_year(date) ((date)/10000)
95: #define isoDate_month(date) (((date)/100)%100)
96: #define isoDate_day(date) ((date)%100)
97:
98: #define isoTime_hour(time) ((time)/10000)
99: #define isoTime_minute(time) (((time)/100)%100)
100: #define isoTime_second(time) ((time)%100)
101:
102: BOOL isoTimeZoneStr_parse(const char* str, xpTimeZone_t*);
103: xpDateTime_t isoDateTimeStr_parse(const char* str);
104:
105: /**************************************************************/
106: /* Conversion between time_t (local and GMT) and isoDate/Time */
107: /**************************************************************/
108: isoTime_t time_to_isoTime(time_t);
109: isoTime_t gmtime_to_isoTime(time_t);
110: isoDate_t time_to_isoDateTime(time_t, isoTime_t*);
111: isoDate_t gmtime_to_isoDateTime(time_t, isoTime_t*);
112: time_t isoDateTime_to_time(isoDate_t, isoTime_t);
113: #define time_to_isoDate(t) time_to_isoDateTime(t,NULL)
114: #define gmtime_to_isoDate(t) gmtime_to_isoDateTime(t,NULL)
115:
116: /***************************************************/
117: /* Conversion between xpDate/Time and isoDate/Time */
118: /***************************************************/
119:
120: #define xpDate_to_isoDate(date) isoDate_create((date).year,(date).month,(date).day)
121: #define xpTime_to_isoTime(time) isoTime_create((time).hour,(time).minute,(unsigned)((time).second))
122:
123: xpDateTime_t isoDateTime_to_xpDateTime(isoDate_t, isoTime_t);
124: isoDate_t xpDateTime_to_isoDateTime(xpDateTime_t, isoTime_t*);
125:
126: /*****************************************************************/
127: /* Conversion from xpDate/Time/Zone to isoDate/Time/Zone Strings */
128: /*****************************************************************/
129:
130: /* NULL sep (separator) values are automatically replaced with ISO-standard separators */
131:
132: /* precision example output
133: * -2 "14"
134: * -1 "14:02"
135: * 0 "14:02:39"
136: * 1 "14.02:39.8"
137: * 2 "14.02:39.82"
138: * 3 "14.02:39.829"
139: */
140: char* xpDate_to_isoDateStr(xpDate_t
141: ,const char* sep
142: ,char* str, size_t maxlen);
143: char* xpTime_to_isoTimeStr(xpTime_t
144: ,const char* sep
145: ,int precision
146: ,char* str, size_t maxlen);
147: char* xpTimeZone_to_isoTimeZoneStr(xpTimeZone_t
148: ,const char* sep
149: ,char *str, size_t maxlen);
150: char* xpDateTime_to_isoDateTimeStr(xpDateTime_t
151: ,const char* date_sep, const char* datetime_sep, const char* time_sep
152: ,int precision
153: ,char* str, size_t maxlen);
154:
155: #if defined(__cplusplus)
156: }
157: #endif
158:
159: #endif /* Don't add anything after this line */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.