File: datetime.h.html

package info (click to toggle)
rudiments 0.31-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,516 kB
  • ctags: 3,248
  • sloc: asm: 23,776; cpp: 22,792; sh: 7,769; ansic: 1,769; makefile: 1,054; xml: 169; perl: 19
file content (251 lines) | stat: -rw-r--r-- 20,864 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<html>
<head>
<title>~/src/firstworks/rudiments-0.31/include/rudiments/datetime.h.html</title>
<meta name="Generator" content="Vim/7.0">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#0000ff">// Copyright (c) 2000-2002 David Muse</font>
<font color="#0000ff">// See the COPYING file for more information.</font>

<font color="#a020f0">#ifndef RUDIMENTS_DATETIME_H</font>
<font color="#a020f0">#define RUDIMENTS_DATETIME_H</font>

<font color="#a020f0">#include </font><font color="#ff00ff">&lt;rudiments/private/datetimeincludes.h&gt;</font>

<font color="#0000ff">// The datetime class provides methods for converting date/time formats and</font>
<font color="#0000ff">// accessing various date/time values.</font>

<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
<font color="#2e8b57"><b>namespace</b></font> rudiments {
<font color="#a020f0">#endif</font>

<font color="#2e8b57"><b>class</b></font> datetimeprivate;

<font color="#2e8b57"><b>class</b></font> datetime {
        <font color="#a52a2a"><b>public</b></font>:

                <font color="#0000ff">// if you need a quick conversion, use one of these methods</font>
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>char</b></font>   *getString(<font color="#2e8b57"><b>time_t</b></font> seconds);
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>char</b></font>   *getString(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>struct</b></font> tm *tmstruct);
                        <font color="#0000ff">// returns &quot;mm/dd/yyyy hh:mm:ss TZN&quot;</font>
                        <font color="#0000ff">// Note that this method allocates a buffer to return</font>
                        <font color="#0000ff">// the string in which must be deleted by the calling</font>
                        <font color="#0000ff">// program.</font>
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>time_t</b></font> getEpoch(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *datestring);
                        <font color="#0000ff">// parses &quot;mm/dd/yyyy hh:mm:ss TZN&quot; and returns the</font>
                        <font color="#0000ff">// number of seconds since 1970.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Note that TZN must be a valid timezone.  Otherwise</font>
                        <font color="#0000ff">// GMT is assumed.</font>
                        <font color="#0000ff">//</font>
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>time_t</b></font> getEpoch(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>struct</b></font> tm *tmstruct);
                        <font color="#0000ff">// converts &quot;tmstruct&quot; to the number of seconds</font>
                        <font color="#0000ff">// since 1970.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Note that in &quot;tmstruct&quot;, the timezone and GMT offset</font>
                        <font color="#0000ff">// must be set to valid values.  Otherwise GMT is</font>
                        <font color="#0000ff">// assumed.</font>


                <font color="#0000ff">// if you need anything other than a quick conversion,</font>
                <font color="#0000ff">// use these methods</font>
                datetime();
                ~datetime();

                <font color="#2e8b57"><b>bool</b></font>  initialize(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *tmstring);
                        <font color="#0000ff">// Parses &quot;tmstring&quot; and sets the date and time</font>
                        <font color="#0000ff">// represented in the class to that time.</font>
                        <font color="#0000ff">// Datestring must be &quot;mm/dd/yyyy hh:mm:ss TZN&quot;.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Note that TZN must be a valid timezone.  Otherwise</font>
                        <font color="#0000ff">// GMT is assumed.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>
                <font color="#2e8b57"><b>bool</b></font>  initialize(<font color="#2e8b57"><b>time_t</b></font> seconds);
                        <font color="#0000ff">// Processes &quot;seconds&quot; and sets the date and time</font>
                        <font color="#0000ff">// represented in the class to that time.</font>
                        <font color="#0000ff">// &quot;seconds&quot; is the number of seconds since 1970;</font>
                        <font color="#0000ff">// output by many standard time functions.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>
                <font color="#2e8b57"><b>bool</b></font>  initialize(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>struct</b></font> tm *tmstruct);
                        <font color="#0000ff">// Processes &quot;tmstruct&quot; and sets the date and time</font>
                        <font color="#0000ff">// represented in the class to that time.</font>
                        <font color="#0000ff">// &quot;tmstruct&quot; is a (struct tm *); output by </font>
                        <font color="#0000ff">// many standard time functions.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Note that in &quot;tmstruct&quot;, the timezone and GMT offset</font>
                        <font color="#0000ff">// must be set to valid values.  Otherwise GMT is</font>
                        <font color="#0000ff">// assumed.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>


                <font color="#2e8b57"><b>bool</b></font>  getSystemDateAndTime();
                        <font color="#0000ff">// Sets the date and time represented in the class to</font>
                        <font color="#0000ff">// the date and time stored in the system clock.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>
                <font color="#2e8b57"><b>bool</b></font>  getHardwareDateAndTime(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *hwtz);
                        <font color="#0000ff">// This method only works if your system has a working</font>
                        <font color="#0000ff">// real-time clock at /dev/rtc.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Sets the date and time represented in the class to</font>
                        <font color="#0000ff">// the date and time stored in the hardware clock.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// &quot;hwtz&quot; must be set to the timezone that the hardware</font>
                        <font color="#0000ff">// clock is using.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>
                <font color="#2e8b57"><b>bool</b></font>  getAdjustedHardwareDateAndTime(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *hwtz);
                        <font color="#0000ff">// This method only works if your system has a working</font>
                        <font color="#0000ff">// real-time clock at /dev/rtc.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Gets the date and time from the hardware clock,</font>
                        <font color="#0000ff">// then adjusts it to the timezone used by the system.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>
                <font color="#2e8b57"><b>bool</b></font>  setSystemDateAndTime();
                        <font color="#0000ff">// Sets the system clock's date and time to the date</font>
                        <font color="#0000ff">// and time currently represented in the class.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>
                <font color="#2e8b57"><b>bool</b></font>  setHardwareDateAndTime(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *hwtz);
                        <font color="#0000ff">// This method only works if your system has a working</font>
                        <font color="#0000ff">// real-time clock at /dev/rtc.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Sets the hardware clock's date and time to the date</font>
                        <font color="#0000ff">// and time currently represented in the class.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// &quot;hwtz&quot; must be set to the timezone that the system</font>
                        <font color="#0000ff">// clock using.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>


                <font color="#0000ff">// These methods return commonly needed time/date values</font>
                <font color="#2e8b57"><b>int32_t</b></font>               getHour() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getMinutes() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getSeconds() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getMonth() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font>    *getMonthName() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font>    *getMonthAbbreviation() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getDayOfMonth() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getDayOfWeek() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getDayOfYear() <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int32_t</b></font>               getYear() <font color="#2e8b57"><b>const</b></font>;

                <font color="#2e8b57"><b>bool</b></font>  isDaylightSavingsTime() <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// returns 1 if daylight savings time is currently</font>
                        <font color="#0000ff">// in effect and 0 if it isn't</font>

                <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font>    *getTimeZoneString() <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// returns a 3 character string representing the</font>
                        <font color="#0000ff">// time zone</font>

                <font color="#2e8b57"><b>int32_t</b></font>       getTimeZoneOffset() <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// returns the offset from GMT (in seconds)</font>

                <font color="#2e8b57"><b>bool</b></font>  adjustTimeZone(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *newtz);
                        <font color="#0000ff">// Recalculates the time currently represented in the</font>
                        <font color="#0000ff">// class to correspond to the time zone &quot;newtz&quot;.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// If, in the new timezone, daylight savings time is</font>
                        <font color="#0000ff">// currently in effect, the time zone will be adjusted</font>
                        <font color="#0000ff">// accordingly.  Ie.  if &quot;newtz&quot; = EST and daylight</font>
                        <font color="#0000ff">// savings time is in effect in EST5EDT, then EDT</font>
                        <font color="#0000ff">// will be used instead.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>

                <font color="#2e8b57"><b>bool</b></font>  adjustTimeZone(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *newtz, <font color="#2e8b57"><b>bool</b></font> ignoredst);
                        <font color="#0000ff">// Recalculates the time currently represented in the</font>
                        <font color="#0000ff">// class to correspond to the time zone &quot;newtz&quot;.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// If ignoredst is false, this method works like</font>
                        <font color="#0000ff">// adjustTimezone(const char *newtz).  However if</font>
                        <font color="#0000ff">// ignoredst is true, this method will not adjust</font>
                        <font color="#0000ff">// EST to EDT if daylight savings time is in</font>
                        <font color="#0000ff">// effect in EST5EDT.</font>
                        <font color="#0000ff">//</font>
                        <font color="#0000ff">// Returns true on success and false on failure.</font>


                <font color="#0000ff">// These methods allow you to change the time currently</font>
                <font color="#0000ff">// represented in the class.</font>
                <font color="#2e8b57"><b>bool</b></font>  setSeconds(<font color="#2e8b57"><b>int32_t</b></font> seconds);
                <font color="#2e8b57"><b>bool</b></font>  setMinutes(<font color="#2e8b57"><b>int32_t</b></font> minutes);
                <font color="#2e8b57"><b>bool</b></font>  setHours(<font color="#2e8b57"><b>int32_t</b></font> hours);
                <font color="#2e8b57"><b>bool</b></font>  setDays(<font color="#2e8b57"><b>int32_t</b></font> days);
                <font color="#2e8b57"><b>bool</b></font>  setMonths(<font color="#2e8b57"><b>int32_t</b></font> months);
                <font color="#2e8b57"><b>bool</b></font>  setYears(<font color="#2e8b57"><b>int32_t</b></font> years);


                <font color="#0000ff">// These methods allow you to add discrete time intervals to</font>
                <font color="#0000ff">// the time currently represented in the class.</font>
                <font color="#2e8b57"><b>bool</b></font>  addSeconds(<font color="#2e8b57"><b>int32_t</b></font> seconds);
                <font color="#2e8b57"><b>bool</b></font>  addMinutes(<font color="#2e8b57"><b>int32_t</b></font> minutes);
                <font color="#2e8b57"><b>bool</b></font>  addHours(<font color="#2e8b57"><b>int32_t</b></font> hours);
                <font color="#2e8b57"><b>bool</b></font>  addDays(<font color="#2e8b57"><b>int32_t</b></font> days);
                <font color="#2e8b57"><b>bool</b></font>  addMonths(<font color="#2e8b57"><b>int32_t</b></font> months);
                <font color="#2e8b57"><b>bool</b></font>  addYears(<font color="#2e8b57"><b>int32_t</b></font> years);


                <font color="#0000ff">// These methods output conversions to other date/time</font>
                <font color="#0000ff">// formats.</font>
                <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font>    *getString();
                        <font color="#0000ff">// returns &quot;mm/dd/yyyy hh:mm:ss TZN&quot;</font>
                        <font color="#0000ff">// (Note that this method returns a pointer to an</font>
                        <font color="#0000ff">// internal string which will be deleted if the</font>
                        <font color="#0000ff">// class instance is deleted.)</font>
                <font color="#2e8b57"><b>time_t</b></font>                getEpoch() <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// returns the number of seconds since 1970</font>
                <font color="#2e8b57"><b>struct</b></font> tm     *getTm();
                        <font color="#0000ff">// returns a pointer to the internal &quot;struct tm&quot;</font>


                <font color="#0000ff">// These methods return timezone data</font>
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> * <font color="#2e8b57"><b>const</b></font>     *getTimeZoneAbbreviations();
                                <font color="#0000ff">// Returns a NULL terminated array of timezone</font>
                                <font color="#0000ff">// abbreviations.</font>
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>int32_t</b></font>          *getTimeZoneOffsets();
                                <font color="#0000ff">// Returns an array of timezone offsets from</font>
                                <font color="#0000ff">// GMT (in seconds).  Each element of this</font>
                                <font color="#0000ff">// array corresponds to an element of the</font>
                                <font color="#0000ff">// array returned b getTimeZoneAbbreviations().</font>

                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>bool</b></font>   validDateTime(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *string);
                                <font color="#0000ff">// Returns true if string represents a valid</font>
                                <font color="#0000ff">// date/time and false otherwise.</font>
                                <font color="#0000ff">//</font>
                                <font color="#0000ff">// &quot;string&quot; should be formatted like:</font>
                                <font color="#0000ff">// 00/00/0000 00:00:00</font>
                                <font color="#0000ff">// and may optionally be followed by a space</font>
                                <font color="#0000ff">// and a timezone.</font>


                <font color="#0000ff">// Many of the functions that the datetime class uses internally</font>
                <font color="#0000ff">// are not reentrant and thus not thread-safe.  Indeed, for</font>
                <font color="#0000ff">// some functions, there is no thread-safe version.  If your</font>
                <font color="#0000ff">// application is multi-threaded, you must use this method to</font>
                <font color="#0000ff">// supply a mutex and ensure thread safety.</font>
                <font color="#0000ff">//</font>
                <font color="#0000ff">// If you don't supply a mutex, the methods in this class</font>
                <font color="#0000ff">// will still work, but will not be thread-safe.</font>
                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>void</b></font>   setTimeMutex(mutex *mtx);
                                <font color="#0000ff">// Allows you to supply a mutex to regulate</font>
                                <font color="#0000ff">// access to the non-reentrant functions.</font>

<font color="#a020f0">        #include </font><font color="#ff00ff">&lt;rudiments/private/datetime.h&gt;</font>
};

<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
}
<font color="#a020f0">#endif</font>

<font color="#a020f0">#endif</font>
</pre>
</body>
</html>