00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef TNTDB_DATETIME_H
00030 #define TNTDB_DATETIME_H
00031
00032 #include <string>
00033 #include <tntdb/date.h>
00034 #include <tntdb/time.h>
00035
00036 namespace tntdb
00037 {
00041 class Datetime
00042 {
00043 unsigned short year;
00044 unsigned short month;
00045 unsigned short day;
00046 unsigned short hour;
00047 unsigned short minute;
00048 unsigned short second;
00049 unsigned short millis;
00050
00051 public:
00053 Datetime()
00054 : year(0),
00055 month(0),
00056 day(0),
00057 hour(0),
00058 minute(0),
00059 second(0),
00060 millis(0)
00061 { }
00062
00063 Datetime(const Date& date, const Time& time)
00064 : year(date.getYear()),
00065 month(date.getMonth()),
00066 day(date.getDay()),
00067 hour(time.getHour()),
00068 minute(time.getMinute()),
00069 second(time.getSecond()),
00070 millis(time.getMillis())
00071 { }
00072
00075 Datetime(unsigned short year_,
00076 unsigned short month_,
00077 unsigned short day_,
00078 unsigned short hour_,
00079 unsigned short minute_,
00080 unsigned short second_,
00081 unsigned short millis_ = 0)
00082 : year(year_),
00083 month(month_),
00084 day(day_),
00085 hour(hour_),
00086 minute(minute_),
00087 second(second_),
00088 millis(millis_)
00089 { }
00090
00091 static Datetime localtime();
00092 static Datetime gmtime();
00093
00095 unsigned short getYear() const { return year; }
00097 unsigned short getMonth() const { return month; }
00099 unsigned short getDay() const { return day; }
00101 unsigned short getHour() const { return hour; }
00103 unsigned short getMinute() const { return minute; }
00105 unsigned short getSecond() const { return second; }
00107 unsigned short getMillis() const { return millis; }
00108
00109 Date getDate() const { return Date(year, month, day); }
00110 Time getTime() const { return Time(hour, minute, second, millis); }
00111
00112 bool isNull() const { return month == 0; }
00113
00115 std::string getIso() const;
00118 static Datetime fromIso(const std::string& s);
00119
00122 void set(unsigned short year_,
00123 unsigned short month_,
00124 unsigned short day_,
00125 unsigned short hour_,
00126 unsigned short minute_,
00127 unsigned short second_,
00128 unsigned short millis_ = 0)
00129 {
00130 year = year_;
00131 month = month_;
00132 day = day_;
00133 hour = hour_;
00134 minute = minute_;
00135 second = second_;
00136 millis = millis_;
00137 }
00138
00139 bool operator== (const Datetime& dt) const
00140 { return year == dt.year
00141 && month == dt.month
00142 && day == dt.day
00143 && hour == dt.hour
00144 && minute == dt.minute
00145 && second == dt.second
00146 && millis == dt.millis; }
00147
00148 bool operator!= (const Datetime& dt) const
00149 { return !operator==(dt); }
00150
00151 bool operator< (const Datetime& dt) const
00152 {
00153 return year < dt.year ? true
00154 : year > dt.year ? false
00155 : month < dt.month ? true
00156 : month > dt.month ? false
00157 : day < dt.day ? true
00158 : day > dt.day ? false
00159 : hour < dt.hour ? true
00160 : hour > dt.hour ? false
00161 : minute < dt.minute ? true
00162 : minute > dt.minute ? false
00163 : second < dt.second ? true
00164 : second > dt.second ? false
00165 : millis < dt.millis;
00166 }
00167
00168 bool operator> (const Datetime& dt) const
00169 { return dt < *this; }
00170
00171 bool operator<= (const Datetime& dt) const
00172 { return !(*this > dt); }
00173
00174 bool operator>= (const Datetime& dt) const
00175 { return !(*this < dt); }
00176 };
00177 }
00178
00179 #endif // TNTDB_DATETIME_H
00180