diff --git a/date/date.cpp b/date/date.cpp index 236fda6..2346426 100644 --- a/date/date.cpp +++ b/date/date.cpp @@ -255,6 +255,35 @@ void Date::AssertCorrectDate() } +bool Date::IsCorrectDate() +{ + // 10000 is only a 'cosmetic' limit + // we can make calculations with greater values + + if( year < 1970 || year > 10000 ) + return false; + + if( month < 1 || month > 12 ) + return false; + + if( day < 1 || day > MonthLen(year, month) ) + return false; + + if( hour < 0 || hour > 23 ) + return false; + + if( min < 0 || min > 59 ) + return false; + + if( sec < 0 || sec > 59 ) + return false; + +return true; +} + + + + int Date::MonthLen(int y, int m) { @@ -381,5 +410,7 @@ return (int)((d+3) % 7); + + } // namespace diff --git a/date/date.h b/date/date.h index 91ea3f0..433e9ea 100644 --- a/date/date.h +++ b/date/date.h @@ -48,11 +48,14 @@ namespace PT /* this class represents a Date (year, month, day, hour, min, sec) - it has O(1) algorithm when converting from/to time_t (seconds from unix epoch) + it has O(1) algorithm when converting from/to time_t (seconds from the Unix Epoch) algorithm description: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html http://alcor.concordia.ca/~gpkatch/gdate-method.html + + current limitation: + we do not support leap seconds */ class Date { @@ -60,24 +63,41 @@ public: /* - default c-ctor sets the unix epoch (Clear method): 1970.01.01 00:00:00 + the date + */ + int year; // 1970 - ... + int month; // 1 - 12 + int day; // 1 - 31 + int hour; // 0 - 23 + int min; // 0 - 59 + int sec; // 0 - 59 + + + + /* + default c-ctor sets the Unix Epoch (Clear method): 1970.01.01 00:00:00 */ Date(); /* - converting from Date, time_t (seconds from unix epoch), tm structure + converting from Date, time_t (seconds from the Unix Epoch), tm structure, and strings */ Date(const Date & d); Date(time_t t); Date(const tm & t); + template Date(const CStringType * str); + template Date(const StringType & str); + Date & operator=(const Date & d); Date & operator=(time_t t); Date & operator=(const tm & t); + template Date & operator=(const CStringType * str); + template Date & operator=(const StringType & str); /* - adding/subtracting time_t (seconds from unix epoch) + adding/subtracting time_t (seconds from the Unix Epoch) */ Date operator+(time_t t) const; Date operator-(time_t t) const; @@ -86,7 +106,7 @@ public: /* - converts time_t in seconds (from unix epoch) to this object + converts time_t in seconds (from the Unix Epoch) to this object */ void FromTime(time_t t); @@ -98,7 +118,7 @@ public: /* - returns time_t (in seconds from unix epoch) + returns time_t (in seconds from the Unix Epoch) */ time_t ToTime() const; @@ -158,7 +178,7 @@ public: /* - set unix epoch: 1970.01.01 00:00:00 + set the Unix Epoch: 1970.01.01 00:00:00 */ void Clear(); @@ -175,12 +195,24 @@ public: void AssertCorrectDate(); + /* + return true if values are from the correct range + year: 1970 - 10000 + month: 1 - 12 + day: 1 - MonthLen + hour: 0 - 23 + min: 0 - 59 + sec: 0 - 59 + */ + bool IsCorrectDate(); + + /* returns how many days there is in a month y - year 1970 - ... m - month 1-12 */ - static int MonthLen(int y, int m); + static int MonthLen(int y, int m); /* @@ -207,11 +239,11 @@ public: /* - this method outputs to the given stream: YYYY-MM-DD, eg. 1990.02.12 + this method outputs to the given stream: YYYY-MM-DD, eg. 1990-02-12 ISO 8601 format */ template - void SerializeDay(Stream & out) const; + void SerializeYearMonthDay(Stream & out) const; /* @@ -219,7 +251,7 @@ public: ISO 8601 format */ template - void SerializeHour(Stream & out) const; + void SerializeHourMinSec(Stream & out) const; /* @@ -231,14 +263,174 @@ public: /* - the date + parsing day month and year + the input string can be as follows: + "12-10-2008" + + white characters are ommited and the method stops after reading the year + so the input string can be: + " 12 - 10 - 2008some text " + a white character means a space or a tab + + as a separator can be '-' '/' or '.' + so below strings have the same meaning: + " 12.10.2008 " + " 12/10 / 2008 " + + this method doesn't test if the values are correct + use IsCorrectDate() to check */ - int year; // 1970 - ... - int month; // 1 - 12 - int day; // 1 - 31 - int hour; // 0 - 23 - int min; // 0 - 59 - int sec; // 0 - 59 + template + bool ParseDayMonthYear(const CStringType * str, const CStringType ** str_after = 0); + + template + bool ParseDayMonthYear(const StringType & str); + + + /* + parsing year month and day + the input string can be as follows: + "2008-10-12" + + white characters are ommited and the method stops after reading the day + so the input string can be: + " 2008 - 10 - 12some text " + a white character means a space or a tab + + as a separator can be '-' '/' or '.' + so below strings have the same meaning: + " 2008.10.12 " + " 2008/10 / 12 " + + this method doesn't test if the values are correct + use IsCorrectDate() to check + */ + template + bool ParseYearMonthDay(const CStringType * str, const CStringType ** str_after = 0); + + template + bool ParseYearMonthDay(const StringType & str); + + + /* + parsing month and day + the input string can be as follows: + "10-12" (month: 10, day: 12) + + white characters are ommited and the method stops after reading the day + so the input string can be: + " 10 - 12some text " + a white character means a space or a tab + + as a separator can be '-' '/' or '.' + so below strings have the same meaning: + " 10.12 " + " 10 / 12 " + + this method doesn't test if the values are correct + use IsCorrectDate() to check + */ + template + bool ParseMonthDay(const CStringType * str, const CStringType ** str_after = 0); + + template + bool ParseMonthDay(const StringType & str); + + + /* + parsing hour minutes and seconds + the input string can be as follows: + "14:10:35" + + white characters are ommited and the method stops after reading seconds + so the input string can be: + " 14 : 10 : 35some text " + a white character means a space or a tab + + a separator is only the ':' character + + this method doesn't test if the values are correct + use IsCorrectDate() to check + */ + template + bool ParseHourMinSec(const CStringType * str, const CStringType ** str_after = 0); + + template + bool ParseHourMinSec(const StringType & str); + + + /* + parsing hour and minutes + the input string can be as follows: + "14:10" + + white characters are ommited and the method stops after reading minutes + so the input string can be: + " 14 : 10some text " + a white character means a space or a tab + + a separator is only the ':' character + + this method doesn't test if the values are correct + use IsCorrectDate() to check + */ + template + bool ParseHourMin(const CStringType * str, const CStringType ** str_after = 0); + + template + bool ParseHourMin(const StringType & str); + + + /* + parsing hour and minutes (if exists) and seconds (if exists) + the input string can be as follows: + "14" -- only an hour given (min and sec will be zero) + "14:10" -- hour with minutes (sec will be zero) + "14:10:35" -- hour, minutes and seconds + + white characters are ommited so these are valid strings too: + " 14 : 10 : 35 " + " 14 : 10 : 35some text " + a white character means a space or a tab + + this method doesn't test if the values are correct + use IsCorrectDate() to check + */ + template + bool ParseTime(const CStringType * str, const CStringType ** str_after = 0); + + template + bool ParseTime(const StringType & str); + + + /* + parsing year/month/day hour:min:sec + the input strings can be as follows: + "2008-10-12 14:10:35" + "2008/10/12 14:10:35" + "2008.10.12 14:10:35" + "2008-10/12 14:10:35" + + white characters are ommited + so the input string can be: + " 2008 - 10 / 12 14 : 10 : 35 " + a white character means a space or a tab + + as a separator for year/month/day can be '-' '/' or '.' + see ParseYearMonthDay() for details + + as a separator for hour:min:sec is the ':' character + see ParseHourMinSec() for details + + at the end the method checks if the values are correct + (by using IsCorrectDate()) + */ + template + bool Parse(const CStringType * str, const CStringType ** str_after = 0); + + template + bool Parse(const StringType & str); + private: @@ -248,14 +440,60 @@ private: template void SerializeInt(Stream & out, int val) const; + template + void SetAfter(const CStringType * str, const CStringType ** str_after); + + template + void SkipWhite(const CStringType * & str); + + template + bool ReadInt(const CStringType * & str, int & result); + + template + bool SkipSeparator(const CStringType * & str, int separator, int separator2 = -1, int separator3 = -1); + }; +template +Date::Date(const CStringType * str) +{ + // parsing can be break in the middle of the string (if errors) + // and some values would not be initialized + Clear(); + Parse(str); +} + + +template +Date::Date(const StringType & str) +{ + Clear(); + Parse(str); +} + + +template +Date & Date::operator=(const CStringType * str) +{ + Parse(str); +} + + +template +Date & Date::operator=(const StringType & str) +{ + Parse(str); +} + + + + template void Date::SerializeInt(Stream & out, int val) const { - if( val < 10 ) + if( val >= 0 && val < 10 ) out << '0'; out << val; @@ -264,7 +502,7 @@ void Date::SerializeInt(Stream & out, int val) const template -void Date::SerializeDay(Stream & out) const +void Date::SerializeYearMonthDay(Stream & out) const { // !! IMPROVE ME the year should be printed with 4 digits, e.g. 0001 when the year is equal to one out << year << '-'; @@ -275,7 +513,7 @@ void Date::SerializeDay(Stream & out) const template -void Date::SerializeHour(Stream & out) const +void Date::SerializeHourMinSec(Stream & out) const { SerializeInt(out, hour); out << ':'; @@ -284,12 +522,283 @@ void Date::SerializeHour(Stream & out) const SerializeInt(out, sec); } + template void Date::Serialize(Stream & out) const { - SerializeDay(out); + SerializeYearMonthDay(out); out << ' '; - SerializeHour(out); + SerializeHourMinSec(out); +} + + + +template +bool Date::ParseDayMonthYear(const CStringType * str, const CStringType ** str_after) +{ +bool result = false; + + if( ReadInt(str, day) && SkipSeparator(str, '.', '-', '/') ) + if( ReadInt(str, month) && SkipSeparator(str, '.', '-', '/') ) + if( ReadInt(str, year) ) + result = true; + + SetAfter(str, str_after); + +return result; +} + + +template +bool Date::ParseDayMonthYear(const StringType & str) +{ + return ParseDayMonthYear(str.c_str()); +} + + + + +template +bool Date::ParseYearMonthDay(const CStringType * str, const CStringType ** str_after) +{ +bool result = false; + + if( ReadInt(str, year) && SkipSeparator(str, '.', '-', '/') ) + if( ReadInt(str, month) && SkipSeparator(str, '.', '-', '/') ) + if( ReadInt(str, day) ) + result = true; + + SetAfter(str, str_after); + +return result; +} + + +template +bool Date::ParseYearMonthDay(const StringType & str) +{ + return ParseYearMonthDay(str.c_str()); +} + + + + +template +bool Date::ParseMonthDay(const CStringType * str, const CStringType ** str_after) +{ +bool result = false; + + if( ReadInt(str, month) && SkipSeparator(str, '.', '-', '/') ) + if( ReadInt(str, day) ) + result = true; + + SetAfter(str, str_after); + +return result; +} + + +template +bool Date::ParseMonthDay(const StringType & str) +{ + return ParseMonthDay(str.c_str()); +} + + + +template +bool Date::ParseHourMinSec(const CStringType * str, const CStringType ** str_after) +{ +bool result = false; + + if( ReadInt(str, hour) && SkipSeparator(str, ':') ) + if( ReadInt(str, min) && SkipSeparator(str, ':') ) + if( ReadInt(str, sec) ) + result = true; + + SetAfter(str, str_after); + +return result; +} + + +template +bool Date::ParseHourMinSec(const StringType & str) +{ + return ParseHourMinSec(str.c_str()); +} + + + +template +bool Date::ParseHourMin(const CStringType * str, const CStringType ** str_after) +{ +bool result = false; + + if( ReadInt(str, hour) && SkipSeparator(str, ':') ) + if( ReadInt(str, min) ) + result = true; + + SetAfter(str, str_after); + +return result; +} + + +template +bool Date::ParseHourMin(const StringType & str) +{ + return ParseHourMin(str.c_str()); +} + + + +template +bool Date::ParseTime(const CStringType * str, const CStringType ** str_after) +{ + if( !ReadInt(str, hour) ) + { + SetAfter(str, str_after); + return false; + } + + min = 0; + sec = 0; + + if( !SkipSeparator(str, ':') ) + { + SetAfter(str, str_after); + return true; // only an hour given + } + + if( !ReadInt(str, min) ) + { + SetAfter(str, str_after); + return false; + } + + if( !SkipSeparator(str, ':') ) + { + SetAfter(str, str_after); + return true; // only an hour and minutes given + } + + if( !ReadInt(str, sec) ) + { + SetAfter(str, str_after); + return false; + } + + SetAfter(str, str_after); + +return true; +} + + +template +bool Date::ParseTime(const StringType & str) +{ + return ParseTime(str.c_str()); +} + + + +template +bool Date::Parse(const CStringType * str, const CStringType ** str_after) +{ +const CStringType * after; +bool result = false; + + if( ParseYearMonthDay(str, &after) ) + if( ParseHourMinSec(after, &after) ) + result = true; + + SetAfter(after, str_after); + + if( result ) + result = IsCorrectDate(); + +return result; +} + + +template +bool Date::Parse(const StringType & str) +{ + return Parse(str.c_str()); +} + + + + +template +void Date::SetAfter(const CStringType * str, const CStringType ** str_after) +{ + if( str_after ) + *str_after = str; +} + + + +template +void Date::SkipWhite(const CStringType * & str) +{ + while( *str==' ' || *str=='\t' ) + str += 1; +} + + + +template +bool Date::ReadInt(const CStringType * & str, int & result) +{ +bool something_read = false; + + SkipWhite(str); + result = 0; + + while( *str >= '0' && *str <= '9' ) + { + result = result * 10 + (*str - '0'); + str += 1; + something_read = true; + + if( result > 10000 ) + { + // we assumed the max year to be 10000 + return false; + } + } + +return something_read; +} + + + +template +bool Date::SkipSeparator(const CStringType * & str, int separator, int separator2, int separator3) +{ + SkipWhite(str); + + if( *str == separator ) + { + str += 1; + return true; + } + + if( separator2 != -1 && *str == separator2 ) + { + str += 1; + return true; + } + + if( separator3 != -1 && *str == separator3 ) + { + str += 1; + return true; + } + +return false; }