edelib 2.1.0
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
26EDELIB_NS_BEGIN
27
32class EDELIB_API TimeZone {
33private:
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);
41public:
46
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
83
140class EDELIB_API Date {
141private:
142 unsigned char dayval;
143 unsigned char monthval;
144 unsigned short yearval;
145
146public:
166
171 enum Day {
172 DayNow = 0
173 };
174
179 enum Year {
180 YearNow = 0
181 };
182
188
192 Date(const Date& d);
193
198 Date& operator=(const Date& d);
199
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
278
283
288
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
324inline bool operator==(const Date& d1, const Date& d2)
325{ return (d1.day() == d2.day() && d1.month() == d2.month() && d1.year() == d2.year()); }
326
327inline 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
332inline bool operator!=(const Date& d1, const Date& d2) { return !(d1 == d2); }
333inline bool operator>=(const Date& d1, const Date& d2) { return (d1 > d2 || d1 == d2); }
334inline bool operator<(const Date& d1, const Date& d2) { return (!(d1 > d2) && (d1 != d2)); }
335inline bool operator<=(const Date& d1, const Date& d2) { return (d1 == d2 || d1 < d2); }
336#endif
337
349class EDELIB_API Time {
350private:
351 unsigned char hourval;
352 unsigned char minval;
353 unsigned char secval;
354
355public:
361
365 Time(const Time& t);
366
370 Time& operator=(const Time& t);
371
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
424
429
434
439
448 static bool is_valid(unsigned char h, unsigned char m, unsigned char s);
449};
450
451#ifndef SKIP_DOCS
452inline bool operator==(const Time& t1, const Time& t2) {
453 return (t1.hour() == t2.hour() && t1.minute() == t2.minute() && t1.second() == t2.second());
454}
455
456inline 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
462inline 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
468inline bool operator!=(const Time& t1, const Time& t2) { return !(t1 == t2); }
469inline bool operator>=(const Time& t1, const Time& t2) { return (t1 > t2 || t1 == t2); }
470inline bool operator<=(const Time& t1, const Time& t2) { return (t1 == t2 || t1 < t2); }
471#endif
472
473EDELIB_NS_END
474#endif
A class for date manipulation.
Definition DateTime.h:140
unsigned char month(void) const
Definition DateTime.h:241
static bool is_valid(unsigned short y, unsigned char m, unsigned char d)
bool leap_year(void) const
Definition DateTime.h:236
Date operator--(int)
bool set(unsigned short y, unsigned char m, unsigned char d, DateType t=DATE_LOCAL)
unsigned char days_in_month() const
Date operator++(int)
static unsigned char days_in_month(unsigned short y, unsigned char m)
Date & operator=(const Date &d)
unsigned short day_of_year() const
Date & operator--()
static bool leap_year(unsigned short y)
Date & operator++()
unsigned char day(void) const
Definition DateTime.h:239
unsigned char day_of_week() const
bool system_set(void)
Day
Current day.
Definition DateTime.h:171
const char * month_name(void)
unsigned short year(void) const
Definition DateTime.h:243
const char * day_name(void)
Date(const Date &d)
Month
Abbreviated months.
Definition DateTime.h:151
@ Jul
July.
Definition DateTime.h:158
@ Mar
March.
Definition DateTime.h:154
@ Nov
November.
Definition DateTime.h:162
@ Aug
August.
Definition DateTime.h:159
@ Oct
October.
Definition DateTime.h:161
@ Feb
February.
Definition DateTime.h:153
@ Jun
June.
Definition DateTime.h:157
@ Dec
December.
Definition DateTime.h:163
@ May
May.
Definition DateTime.h:156
@ Apr
April.
Definition DateTime.h:155
@ Sep
September.
Definition DateTime.h:160
Year
Current year.
Definition DateTime.h:179
bool operator<(const String &str1, const char *str2)
Definition String.h:377
bool operator<=(const String &str1, const char *str2)
Definition String.h:383
bool operator!=(const String &str1, const char *str2)
Definition String.h:359
bool operator>=(const String &str1, const char *str2)
Definition String.h:371
bool operator==(const String &str1, const char *str2)
Definition String.h:353
A class for getting time from desired time zone.
Definition DateTime.h:32
const char * code(void)
Definition DateTime.h:61
unsigned long time(void)
Definition DateTime.h:72
bool set(const char *n)
const char * zone(void)
Definition DateTime.h:66
A class for time manipulation.
Definition DateTime.h:349
static bool is_valid(unsigned char h, unsigned char m, unsigned char s)
Time & operator--()
void set(unsigned char h, unsigned char m, unsigned char s=0)
void set_now(void)
Time operator--(int)
Time & operator++()
Time(const Time &t)
Time & operator=(const Time &t)
unsigned char hour(void) const
Definition DateTime.h:405
unsigned char minute(void) const
Definition DateTime.h:410
unsigned char second(void) const
Definition DateTime.h:415
bool system_set(void)
Time operator++(int)
DateType
Types of date settable via Date::set()
Definition DateTime.h:79
@ DATE_LOCAL
use local date
Definition DateTime.h:80
@ DATE_UTC
use UTC date
Definition DateTime.h:81