diff --git a/include/litesql/datetime.hpp b/include/litesql/datetime.hpp index 2ec7f45b..12335073 100644 --- a/include/litesql/datetime.hpp +++ b/include/litesql/datetime.hpp @@ -7,7 +7,7 @@ #ifndef litesql_datetime_hpp #define litesql_datetime_hpp /** \file datetime.hpp - constains Date, Time and DateTime - classes */ + contains classes for handling date and time data. */ #include "field.hpp" #include namespace litesql { @@ -55,24 +55,7 @@ class Date { Date& setTimeStamp(time_t t); std::string asString(const std::string& format="%u") const; }; -/** holds time of day */ -class Time { - /** secs after midnight */ - int value; -public: - Time(int secs=0); - Time(int hour, int min, int sec); - int hour() const; - int min() const; - int sec() const; - int secs() const; - std::string asString(const std::string& format="%u") const; - - Time& setHour(int d); - Time& setMin(int m); - Time& setSec(int y); - Time& setSecs(int secs); -}; + /** holds date and time of day */ class DateTime { time_t value; @@ -99,19 +82,14 @@ class DateTime { template <> Date convert(const std::string& value); template <> -Time convert(const std::string& value); -template <> DateTime convert(const std::string& value); template <> std::string convert(const Date& value); template <> -std::string convert(const Time& value); -template <> std::string convert(const DateTime& value); std::ostream& operator << (std::ostream& os, const Date& d); -std::ostream& operator << (std::ostream& os, const Time& d); std::ostream& operator << (std::ostream& os, const DateTime& d); } diff --git a/src/library/datetime.cpp b/src/library/datetime.cpp index 3cabe8d0..19efe21b 100644 --- a/src/library/datetime.cpp +++ b/src/library/datetime.cpp @@ -5,342 +5,219 @@ * See LICENSE for copyright information. */ #include "litesql/datetime.hpp" #include "compatibility.hpp" -#include #include +#include +using std::ostream; using std::string; using std::vector; -using std::ostream; - namespace litesql { TimeStruct::TimeStruct(time_t t) { - if (!t) - t = time(NULL); - localtime_r(&t, &mytm); -} -int TimeStruct::day() const { - return mytm.tm_mday; -} -int TimeStruct::dayOfWeek() const { - return mytm.tm_wday; -} -int TimeStruct::dayOfYear() const { - return mytm.tm_yday; -} -int TimeStruct::month() const { - return mytm.tm_mon + 1; -} -int TimeStruct::year() const { - return mytm.tm_year + 1900; -} -int TimeStruct::hour() const { - return mytm.tm_hour; -} -int TimeStruct::min() const { - return mytm.tm_min; -} -int TimeStruct::sec() const { - return mytm.tm_sec; -} + if (!t) + t = time(NULL); + localtime_r(&t, &mytm); +} +int TimeStruct::day() const { return mytm.tm_mday; } +int TimeStruct::dayOfWeek() const { return mytm.tm_wday; } +int TimeStruct::dayOfYear() const { return mytm.tm_yday; } +int TimeStruct::month() const { return mytm.tm_mon + 1; } +int TimeStruct::year() const { return mytm.tm_year + 1900; } +int TimeStruct::hour() const { return mytm.tm_hour; } +int TimeStruct::min() const { return mytm.tm_min; } +int TimeStruct::sec() const { return mytm.tm_sec; } time_t TimeStruct::timeStamp() const { - return mktime(const_cast(&mytm)); + return mktime(const_cast(&mytm)); } -TimeStruct& TimeStruct::setDay(int day) { - mytm.tm_mday = day; - return *this; +TimeStruct &TimeStruct::setDay(int day) { + mytm.tm_mday = day; + return *this; } -TimeStruct& TimeStruct::setMonth(int month) { - mytm.tm_mon = month - 1; - return *this; +TimeStruct &TimeStruct::setMonth(int month) { + mytm.tm_mon = month - 1; + return *this; } -TimeStruct& TimeStruct::setYear(int year) { - mytm.tm_year = year - 1900; - return *this; +TimeStruct &TimeStruct::setYear(int year) { + mytm.tm_year = year - 1900; + return *this; } -TimeStruct& TimeStruct::setHour(int hour) { - mytm.tm_hour = hour; - return *this; +TimeStruct &TimeStruct::setHour(int hour) { + mytm.tm_hour = hour; + return *this; } -TimeStruct& TimeStruct::setMin(int min) { - mytm.tm_min = min; - return *this; +TimeStruct &TimeStruct::setMin(int min) { + mytm.tm_min = min; + return *this; } -TimeStruct& TimeStruct::setSec(int sec) { - mytm.tm_sec = sec; - return *this; +TimeStruct &TimeStruct::setSec(int sec) { + mytm.tm_sec = sec; + return *this; } -TimeStruct& TimeStruct::setTimeStamp(time_t t) { - localtime_r(&t, &mytm); - return *this; +TimeStruct &TimeStruct::setTimeStamp(time_t t) { + localtime_r(&t, &mytm); + return *this; } - + Date::Date(time_t t) : value(t) { - if (value == 0) - value = time(NULL); + if (value == 0) + value = time(NULL); } Date::Date(int day, int month, int year) { - value = TimeStruct().setDay(day).setMonth(month).setYear(year) - .timeStamp(); -} -int Date::day() const { - return TimeStruct(value).day(); -} -int Date::dayOfWeek() const { - return TimeStruct(value).dayOfWeek(); -} -int Date::month() const { - return TimeStruct(value).month(); -} -int Date::year() const { - return TimeStruct(value).year(); -} -time_t Date::timeStamp() const { - return value; -} -TimeStruct Date::timeStruct() const { - return TimeStruct(value); -} -Date& Date::setDay(int d) { - value = TimeStruct(value).setDay(d).timeStamp(); - return *this; -} -Date& Date::setMonth(int m) { - value = TimeStruct(value).setMonth(m).timeStamp(); - return *this; -} -Date& Date::setYear(int y) { - value = TimeStruct(value).setYear(y).timeStamp(); - return *this; -} -Date& Date::setTimeStamp(time_t t) { - value = t; - return *this; -} -string Date::asString(const string& format) const { - if (format == "%u") { - char buf[32]; - snprintf(buf, 32, "%lu", value); - return buf; - } - vector data = split(format, "%"); - TimeStruct ts(value); - string res = data[0]; - for (size_t i = 1; i < data.size(); i++) { - string rest = data[i].substr(1, data[i].size()); - switch(data[i][0]) { - case 'd': - res += toString(ts.day()) + rest; - break; - case 'm': - res += toString(ts.month()) + rest; - break; - case 'y': - res += toString(ts.year()) + rest; - } - } - return res; -} -Time::Time(int secs) : value(secs) { -} -Time::Time(int hour, int min, int sec) { - value = hour * 3600 + min * 60 + sec; -} -int Time::hour() const { - return value / 3600; -} -int Time::min() const { - return (value / 60) % 60; -} -int Time::sec() const { - return value % 60; -} -int Time::secs() const { - return value; -} -Time& Time::setHour(int h) { - value = h * 3600 + min() * 60 + sec(); - return *this; -} -Time& Time::setMin(int m) { - value = hour() * 3600 + m * 60 + sec(); - return *this; -} -Time& Time::setSec(int s) { - value = hour() * 3600 + min() * 60 + s; - return *this; -} -Time& Time::setSecs(int secs) { - value = secs; - return *this; -} -string Time::asString(const string& format) const { - if (format == "%u") { - char buf[32]; - snprintf(buf, 32, "%d", value); - return buf; + value = TimeStruct().setDay(day).setMonth(month).setYear(year).timeStamp(); +} +int Date::day() const { return TimeStruct(value).day(); } +int Date::dayOfWeek() const { return TimeStruct(value).dayOfWeek(); } +int Date::month() const { return TimeStruct(value).month(); } +int Date::year() const { return TimeStruct(value).year(); } +time_t Date::timeStamp() const { return value; } +TimeStruct Date::timeStruct() const { return TimeStruct(value); } +Date &Date::setDay(int d) { + value = TimeStruct(value).setDay(d).timeStamp(); + return *this; +} +Date &Date::setMonth(int m) { + value = TimeStruct(value).setMonth(m).timeStamp(); + return *this; +} +Date &Date::setYear(int y) { + value = TimeStruct(value).setYear(y).timeStamp(); + return *this; +} +Date &Date::setTimeStamp(time_t t) { + value = t; + return *this; +} +string Date::asString(const string &format) const { + if (format == "%u") { + char buf[32]; + snprintf(buf, 32, "%lu", value); + return buf; + } + vector data = split(format, "%"); + TimeStruct ts(value); + string res = data[0]; + for (size_t i = 1; i < data.size(); i++) { + string rest = data[i].substr(1, data[i].size()); + switch (data[i][0]) { + case 'd': + res += toString(ts.day()) + rest; + break; + case 'm': + res += toString(ts.month()) + rest; + break; + case 'y': + res += toString(ts.year()) + rest; } - vector data= split(format, "%"); - string res = data[0]; - for (size_t i = 1; i < data.size(); i++) { - string rest = data[i].substr(1, data[i].size()); - switch(data[i][0]) { - case 'h': - res += toString(hour()) + rest; - break; - case 'M': - if (min() < 10) - res += "0"; - res += toString(min()) + rest; - break; - case 's': - if (sec() < 10) - res += "0"; - res += toString(sec()) + rest; - } - } - return res; -} -DateTime::DateTime(time_t t) { - value = (t==0) ? time(NULL) : t; + } + return res; } -int DateTime::day() const { - return timeStruct().day(); -} -int DateTime::month() const { - return timeStruct().month(); -} -int DateTime::year() const { - return timeStruct().year(); -} -int DateTime::hour() const { - return timeStruct().hour(); -} -int DateTime::min() const { - return timeStruct().min(); -} -int DateTime::sec() const { - return timeStruct().sec(); -} -time_t DateTime::timeStamp() const { - return timeStruct().timeStamp(); -} -TimeStruct DateTime::timeStruct() const { - return TimeStruct(value); -} -DateTime& DateTime::setDay(int d) { - value = TimeStruct(value).setDay(d).timeStamp(); - return *this; -} -DateTime& DateTime::setMonth(int m) { - value = TimeStruct(value).setMonth(m).timeStamp(); - return *this; -} -DateTime& DateTime::setYear(int y) { - value = TimeStruct(value).setYear(y).timeStamp(); - return *this; -} -DateTime& DateTime::setHour(int h) { - value = TimeStruct(value).setHour(h).timeStamp(); - return *this; -} -DateTime& DateTime::setMin(int m) { - value = TimeStruct(value).setMin(m).timeStamp(); - return *this; -} -DateTime& DateTime::setSec(int s) { - value = TimeStruct(value).setSec(s).timeStamp(); - return *this; -} -string DateTime::asString(const string& format) const { - if (format == "%u") { - char buf[32]; - snprintf(buf, 32, "%lu", value); - return buf; - } - vector data = split(format, "%"); - TimeStruct ts(value); - string res = data[0]; - for (size_t i = 1; i < data.size(); i++) { - string rest = data[i].substr(1, data[i].size()); - switch(data[i][0]) { - case 'd': - res += toString(ts.day()) + rest; - break; - case 'm': - res += toString(ts.month()) + rest; - break; - case 'y': - res += toString(ts.year()) + rest; - break; - case 'h': - res += toString(ts.hour()) + rest; - break; - case 'M': - if (ts.min() < 10) - res += "0"; - res += toString(ts.min()) + rest; - break; - case 's': - if (ts.sec() < 10) - res += "0"; - res += toString(ts.sec()) + rest; - break; - } +DateTime::DateTime(time_t t) { value = (t == 0) ? time(NULL) : t; } + +int DateTime::day() const { return timeStruct().day(); } + +int DateTime::month() const { return timeStruct().month(); } + +int DateTime::year() const { return timeStruct().year(); } + +int DateTime::hour() const { return timeStruct().hour(); } + +int DateTime::min() const { return timeStruct().min(); } + +int DateTime::sec() const { return timeStruct().sec(); } + +time_t DateTime::timeStamp() const { return value; } + +TimeStruct DateTime::timeStruct() const { return TimeStruct(value); } + +DateTime &DateTime::setDay(int d) { + value = TimeStruct(value).setDay(d).timeStamp(); + return *this; +} +DateTime &DateTime::setMonth(int m) { + value = TimeStruct(value).setMonth(m).timeStamp(); + return *this; +} +DateTime &DateTime::setYear(int y) { + value = TimeStruct(value).setYear(y).timeStamp(); + return *this; +} +DateTime &DateTime::setHour(int h) { + value = TimeStruct(value).setHour(h).timeStamp(); + return *this; +} +DateTime &DateTime::setMin(int m) { + value = TimeStruct(value).setMin(m).timeStamp(); + return *this; +} +DateTime &DateTime::setSec(int s) { + value = TimeStruct(value).setSec(s).timeStamp(); + return *this; +} +string DateTime::asString(const string &format) const { + if (format == "%u") { + char buf[32]; + snprintf(buf, 32, "%lu", value); + return buf; + } + vector data = split(format, "%"); + TimeStruct ts(value); + string res = data[0]; + for (size_t i = 1; i < data.size(); i++) { + string rest = data[i].substr(1, data[i].size()); + switch (data[i][0]) { + case 'd': + res += toString(ts.day()) + rest; + break; + case 'm': + res += toString(ts.month()) + rest; + break; + case 'y': + res += toString(ts.year()) + rest; + break; + case 'h': + res += toString(ts.hour()) + rest; + break; + case 'M': + if (ts.min() < 10) + res += "0"; + res += toString(ts.min()) + rest; + break; + case 's': + if (ts.sec() < 10) + res += "0"; + res += toString(ts.sec()) + rest; + break; } - return res; -} -template <> -Date convert(const string& value) { - return Date(atoi(value)); + } + return res; } -template <> -Time convert(const string& value) { - return Time(atoi(value)); -} -template <> -DateTime convert(const string& value) { - return DateTime(atoi(value)); -} -template <> -Date convert(int value) { - return Date(value); +template <> Date convert(const string &value) { + return Date(atoi(value)); } -template <> -Time convert(int value) { - return Time(value); +template <> DateTime convert(const string &value) { + return DateTime(atoi(value)); } -template <> -DateTime convert(int value) { - return DateTime(value); +template <> Date convert(int value) { return Date(value); } +template <> DateTime convert(int value) { + return DateTime(value); } -template <> -DateTime convert(time_t value) { - return DateTime(value); -} - -template <> -std::string convert(const Date& value) { - return toString(value.timeStamp()); +template <> DateTime convert(time_t value) { + return DateTime(value); } -template <> -std::string convert(const Time& value) { - return toString(value.secs()); + +template <> std::string convert(const Date &value) { + return toString(value.timeStamp()); } template <> -std::string convert(const DateTime& value) { - return toString(value.timeStamp()); +std::string convert(const DateTime &value) { + return toString(value.timeStamp()); } -ostream& operator << (ostream& os, const Date& d) { - return os << d.asString(); -} -ostream& operator << (ostream& os, const Time& d) { - return os << d.asString(); -} -ostream& operator << (ostream& os, const DateTime& d) { - return os << d.asString(); -} +ostream &operator<<(ostream &os, const Date &d) { return os << d.asString(); } + +ostream &operator<<(ostream &os, const DateTime &d) { + return os << d.asString(); } +} // namespace litesql diff --git a/src/tests/test-datetime.cpp b/src/tests/test-datetime.cpp index fc872aec..74eccc2f 100644 --- a/src/tests/test-datetime.cpp +++ b/src/tests/test-datetime.cpp @@ -31,15 +31,5 @@ int main(int /*argc*/, char * /*argv*/[]) { assert(d.timeStamp() == d2.timeStamp()); - // TC1 for Time - Time t; - std::string tstring = t.asString(); - - Time t2 = convert(tstring); - - assert(t.secs() == t2.secs()); - - - return 0; }