/* * This file is a part of PikoTools * and is distributed under the (new) BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2012, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name Tomasz Sowa nor the names of contributors to this * project may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef headerfile_picotools_mainparser_mainparser #define headerfile_picotools_mainparser_mainparser #include 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) algorithm description: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html http://alcor.concordia.ca/~gpkatch/gdate-method.html */ class Date { public: /* 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 */ Date(const Date & d); Date(time_t t); Date(const tm & t); Date & operator=(const Date & d); Date & operator=(time_t t); Date & operator=(const tm & t); /* adding/subtracting time_t (seconds from unix epoch) */ Date operator+(time_t t) const; Date operator-(time_t t) const; Date & operator+=(time_t t); Date & operator-=(time_t t); /* converts time_t in seconds (from unix epoch) to this object */ void FromTime(time_t t); /* converts tm structure to this object */ void FromTm(const tm & t); /* returns time_t (in seconds from unix epoch) */ time_t ToTime() const; /* return tm structure only tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec fields are set the rest is equal to zero */ tm ToTm() const; /* getting/setting the number of days from 0000:03:01 (year 0, month 3 - March, day 1) (ToDays() and FromDays() can work even with a year less than 1970) */ long long ToDays() const; void FromDays(long long g); /* returns a difference in second between two dates (always a value greater than zero) */ time_t operator-(const Date & d) const; /* 'Compare' returns zero if this and d are equal return value less than zero if this is lower than d and a value greater than zero if this is greater than d */ int Compare(const Date & d) const; /* returns true if year, month and day are the same */ bool IsTheSameDay(const Date & d) const; /* returns true if hour, min and sec are the same */ bool IsTheSameHour(const Date & d) const; /* operators for comparing */ bool operator==(const Date & d) const; bool operator!=(const Date & d) const; bool operator>(const Date & d) const; bool operator>=(const Date & d) const; bool operator<(const Date & d) const; bool operator<=(const Date & d) const; /* set unix epoch: 1970.01.01 00:00:00 */ void Clear(); /* assert a correct date (values will be from the correct range) year: 1970 - 10000 month: 1 - 12 day: 1 - MonthLen hour: 0 - 23 min: 0 - 59 sec: 0 - 59 */ void AssertCorrectDate(); /* returns how many days there is in a month y - year 1970 - ... m - month 1-12 */ static int MonthLen(int y, int m); /* returns true if 'y' is a leap year leap year has one additional day (in february) - so the year lasts 366 days */ static bool IsYearLeap(int y); /* returns true if the currecn year is a leap year */ bool IsYearLeap() const; /* returns a day index from a week sunday - 0 monday - 1 ... saturday - 6 */ int WeekDay() const; /* this method outputs to the given stream: YYYY-MM-DD, eg. 1990.02.12 ISO 8601 format */ template void SerializeDay(Stream & out) const; /* this method outputs to the given stream: HH:MM:SS, eg: 13:05:39 ISO 8601 format */ template void SerializeHour(Stream & out) const; /* this method outputs to the given stream: YYYY-MM-DD HH:MM:SS, eg: 1990-02-12 13:05:39 ISO 8601 format */ template void Serialize(Stream & out) const; /* 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 private: void AssertRange(int & val, int val_min, int val_max); template void SerializeInt(Stream & out, int val) const; }; template void Date::SerializeInt(Stream & out, int val) const { if( val < 10 ) out << '0'; out << val; } template void Date::SerializeDay(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 << '-'; SerializeInt(out, month); out << '-'; SerializeInt(out, day); } template void Date::SerializeHour(Stream & out) const { SerializeInt(out, hour); out << ':'; SerializeInt(out, min); out << ':'; SerializeInt(out, sec); } template void Date::Serialize(Stream & out) const { SerializeDay(out); out << ' '; SerializeHour(out); } } // namespace #endif