DateTime.h
/*
* $Id: DateTime.h 2967 2009-12-02 14:31:34Z karijes $
*
* Classes related to date/time and timezones.
* Copyright (c) 2005-2007 edelib authors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __EDELIB_DATETIME_H__
#define __EDELIB_DATETIME_H__
#include "edelib-global.h"
EDELIB_NS_BEGIN
class EDELIB_API TimeZone {
private:
char* zoneval;
char* zcode;
unsigned long timeval;
bool load(const char* zone);
bool load_local(void);
void clear(void);
public:
TimeZone();
~TimeZone();
bool set(const char* n);
const char* code(void) { return (zcode ? zcode : "??"); }
const char* zone(void) { return (zoneval ? zoneval : "Unknown"); }
unsigned long time(void) { return timeval; }
};
enum DateType {
DATE_LOCAL = 0,
DATE_UTC
};
class EDELIB_API Date {
private:
unsigned char dayval;
unsigned char monthval;
unsigned short yearval;
public:
enum Month {
Jan = 1,
Feb,
Mar,
Apr,
May,
Jun,
Jul,
Aug,
Sep,
Oct,
Nov,
Dec,
MonthNow
};
enum Day {
DayNow = 0
};
enum Year {
YearNow = 0
};
Date();
Date(const Date& d);
Date& operator=(const Date& d);
~Date();
bool set(unsigned short y, unsigned char m, unsigned char d, DateType t = DATE_LOCAL);
bool system_set(void);
bool leap_year(void) const { return leap_year(yearval); }
unsigned char day(void) const { return dayval; }
unsigned char month(void) const { return monthval; }
unsigned short year(void) const { return yearval; }
const char* day_name(void);
const char* month_name(void);
unsigned char days_in_month() const;
unsigned char day_of_week() const;
unsigned short day_of_year() const;
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
static bool leap_year(unsigned short y);
static unsigned char days_in_month(unsigned short y, unsigned char m);
static bool is_valid(unsigned short y, unsigned char m, unsigned char d);
};
#ifndef SKIP_DOCS
inline bool operator==(const Date& d1, const Date& d2)
{ return (d1.day() == d2.day() && d1.month() == d2.month() && d1.year() == d2.year()); }
inline bool operator>(const Date& d1, const Date& d2) {
return (d1.year() > d2.year() || (d1.year() == d2.year() && d1.month() > d2.month()) ||
(d1.year() == d2.year() && d1.month() == d2.month() && d1.day() > d2.day()));
}
inline bool operator!=(const Date& d1, const Date& d2) { return !(d1 == d2); }
inline bool operator>=(const Date& d1, const Date& d2) { return (d1 > d2 || d1 == d2); }
inline bool operator<(const Date& d1, const Date& d2) { return (!(d1 > d2) && (d1 != d2)); }
inline bool operator<=(const Date& d1, const Date& d2) { return (d1 == d2 || d1 < d2); }
#endif
class EDELIB_API Time {
private:
unsigned char hourval;
unsigned char minval;
unsigned char secval;
public:
Time();
Time(const Time& t);
Time& operator=(const Time& t);
~Time();
void set(unsigned char h, unsigned char m, unsigned char s = 0);
void set_now(void);
bool system_set(void);
unsigned char hour(void) const { return hourval; }
unsigned char minute(void) const { return minval; }
unsigned char second(void) const { return secval; }
Time& operator++();
Time operator++(int);
Time& operator--();
Time operator--(int);
static bool is_valid(unsigned char h, unsigned char m, unsigned char s);
};
#ifndef SKIP_DOCS
inline bool operator==(const Time& t1, const Time& t2) {
return (t1.hour() == t2.hour() && t1.minute() == t2.minute() && t1.second() == t2.second());
}
inline bool operator>(const Time& t1, const Time& t2) {
return (t1.hour() > t2.hour() ||
(t1.hour() == t2.hour() && t1.second() > t2.second()) ||
t1.second() == t2.second());
}
inline bool operator<(const Time& t1, const Time& t2) {
return (t1.hour() < t2.hour() ||
(t1.hour() == t2.hour() && t1.second() < t2.second()) ||
t1.second() == t2.second());
}
inline bool operator!=(const Time& t1, const Time& t2) { return !(t1 == t2); }
inline bool operator>=(const Time& t1, const Time& t2) { return (t1 > t2 || t1 == t2); }
inline bool operator<=(const Time& t1, const Time& t2) { return (t1 == t2 || t1 < t2); }
#endif
EDELIB_NS_END
#endif
edelib::Date
A class for date manipulation.
Definition: DateTime.h:140
edelib::Date::Mar
March.
Definition: DateTime.h:154
edelib::Date::Sep
September.
Definition: DateTime.h:160
edelib::Time::second
unsigned char second(void) const
Definition: DateTime.h:415
edelib::Time
A class for time manipulation.
Definition: DateTime.h:349
edelib::Date::May
May.
Definition: DateTime.h:156
edelib::Date::Day
Day
Current day.
Definition: DateTime.h:171
edelib::DateType
DateType
Types of date settable via Date::set()
Definition: DateTime.h:79
edelib::Date::Feb
February.
Definition: DateTime.h:153
edelib::Date::month
unsigned char month(void) const
Definition: DateTime.h:241
edelib::DATE_UTC
use UTC date
Definition: DateTime.h:81
edelib::DATE_LOCAL
use local date
Definition: DateTime.h:80
edelib::TimeZone::zone
const char * zone(void)
Definition: DateTime.h:66
edelib::Date::Jul
July.
Definition: DateTime.h:158
edelib::Date::Jun
June.
Definition: DateTime.h:157
edelib::Date::Year
Year
Current year.
Definition: DateTime.h:179
edelib::Date::Dec
December.
Definition: DateTime.h:163
edelib::Date::Apr
April.
Definition: DateTime.h:155
edelib::Date::leap_year
bool leap_year(void) const
Definition: DateTime.h:236
edelib::Date::day
unsigned char day(void) const
Definition: DateTime.h:239
edelib::Time::hour
unsigned char hour(void) const
Definition: DateTime.h:405
edelib::Date::Nov
November.
Definition: DateTime.h:162
edelib::TimeZone::time
unsigned long time(void)
Definition: DateTime.h:72
edelib::Date::Oct
October.
Definition: DateTime.h:161
edelib::Date::Aug
August.
Definition: DateTime.h:159
edelib::Date::year
unsigned short year(void) const
Definition: DateTime.h:243
edelib::Date::Month
Month
Abbreviated months.
Definition: DateTime.h:151
edelib::TimeZone::code
const char * code(void)
Definition: DateTime.h:61
edelib::TimeZone
A class for getting time from desired time zone.
Definition: DateTime.h:32
edelib::Time::minute
unsigned char minute(void) const
Definition: DateTime.h:410