edelib 1.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List

DateTime.h
1 /*
2 * $Id: DateTime.h 2967 2009-12-02 14:31:34Z karijes $
3 *
4 * Classes related to date/time and timezones.
5 * Copyright (c) 2005-2007 edelib authors
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef __EDELIB_DATETIME_H__
22 #define __EDELIB_DATETIME_H__
23
24 #include "edelib-global.h"
25
26 EDELIB_NS_BEGIN
27
32 class EDELIB_API TimeZone {
33 private:
34 char* zoneval;
35 char* zcode;
36 unsigned long timeval;
37
38 bool load(const char* zone);
39 bool load_local(void);
40 void clear(void);
41 public:
45 TimeZone();
46
50 ~TimeZone();
51
56 bool set(const char* n);
57
61 const char* code(void) { return (zcode ? zcode : "??"); }
62
66 const char* zone(void) { return (zoneval ? zoneval : "Unknown"); }
67
72 unsigned long time(void) { return timeval; }
73 };
74
79 enum DateType {
80 DATE_LOCAL = 0,
81 DATE_UTC
82 };
83
140 class EDELIB_API Date {
141 private:
142 unsigned char dayval;
143 unsigned char monthval;
144 unsigned short yearval;
145
146 public:
151 enum Month {
152 Jan = 1,
153 Feb,
154 Mar,
155 Apr,
156 May,
157 Jun,
158 Jul,
159 Aug,
160 Sep,
161 Oct,
162 Nov,
163 Dec,
164 MonthNow
165 };
166
171 enum Day {
172 DayNow = 0
173 };
174
179 enum Year {
180 YearNow = 0
181 };
182
187 Date();
188
192 Date(const Date& d);
193
198 Date& operator=(const Date& d);
199
203 ~Date();
204
220 bool set(unsigned short y, unsigned char m, unsigned char d, DateType t = DATE_LOCAL);
221
233 bool system_set(void);
234
236 bool leap_year(void) const { return leap_year(yearval); }
237
239 unsigned char day(void) const { return dayval; }
241 unsigned char month(void) const { return monthval; }
243 unsigned short year(void) const { return yearval; }
244
250 const char* day_name(void);
251
257 const char* month_name(void);
258
259
261 unsigned char days_in_month() const;
267 unsigned char day_of_week() const;
268
270 unsigned short day_of_year() const;
271
277 Date& operator++();
278
282 Date operator++(int);
283
287 Date& operator--();
288
292 Date operator--(int);
293
300 static bool leap_year(unsigned short y);
301
310 static unsigned char days_in_month(unsigned short y, unsigned char m);
311
320 static bool is_valid(unsigned short y, unsigned char m, unsigned char d);
321 };
322
323 #ifndef SKIP_DOCS
324 inline bool operator==(const Date& d1, const Date& d2)
325 { return (d1.day() == d2.day() && d1.month() == d2.month() && d1.year() == d2.year()); }
326
327 inline bool operator>(const Date& d1, const Date& d2) {
328 return (d1.year() > d2.year() || (d1.year() == d2.year() && d1.month() > d2.month()) ||
329 (d1.year() == d2.year() && d1.month() == d2.month() && d1.day() > d2.day()));
330 }
331
332 inline bool operator!=(const Date& d1, const Date& d2) { return !(d1 == d2); }
333 inline bool operator>=(const Date& d1, const Date& d2) { return (d1 > d2 || d1 == d2); }
334 inline bool operator<(const Date& d1, const Date& d2) { return (!(d1 > d2) && (d1 != d2)); }
335 inline bool operator<=(const Date& d1, const Date& d2) { return (d1 == d2 || d1 < d2); }
336 #endif
337
349 class EDELIB_API Time {
350 private:
351 unsigned char hourval;
352 unsigned char minval;
353 unsigned char secval;
354
355 public:
360 Time();
361
365 Time(const Time& t);
366
370 Time& operator=(const Time& t);
371
375 ~Time();
376
384 void set(unsigned char h, unsigned char m, unsigned char s = 0);
385
391 void set_now(void);
392
400 bool system_set(void);
401
405 unsigned char hour(void) const { return hourval; }
406
410 unsigned char minute(void) const { return minval; }
411
415 unsigned char second(void) const { return secval; }
416
423 Time& operator++();
424
428 Time operator++(int);
429
433 Time& operator--();
434
438 Time operator--(int);
439
448 static bool is_valid(unsigned char h, unsigned char m, unsigned char s);
449 };
450
451 #ifndef SKIP_DOCS
452 inline bool operator==(const Time& t1, const Time& t2) {
453 return (t1.hour() == t2.hour() && t1.minute() == t2.minute() && t1.second() == t2.second());
454 }
455
456 inline bool operator>(const Time& t1, const Time& t2) {
457 return (t1.hour() > t2.hour() ||
458 (t1.hour() == t2.hour() && t1.second() > t2.second()) ||
459 t1.second() == t2.second());
460 }
461
462 inline bool operator<(const Time& t1, const Time& t2) {
463 return (t1.hour() < t2.hour() ||
464 (t1.hour() == t2.hour() && t1.second() < t2.second()) ||
465 t1.second() == t2.second());
466 }
467
468 inline bool operator!=(const Time& t1, const Time& t2) { return !(t1 == t2); }
469 inline bool operator>=(const Time& t1, const Time& t2) { return (t1 > t2 || t1 == t2); }
470 inline bool operator<=(const Time& t1, const Time& t2) { return (t1 == t2 || t1 < t2); }
471 #endif
472
473 EDELIB_NS_END
474 #endif