pikotools/date/date.h

815 lines
17 KiB
C++

/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* 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 <ctime>
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 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
{
public:
/*
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 the Unix Epoch), tm structure, and strings
*/
Date(const Date & d);
Date(time_t t);
Date(const tm & t);
template<class CStringType> Date(const CStringType * str);
template<class StringType> Date(const StringType & str);
Date & operator=(const Date & d);
Date & operator=(time_t t);
Date & operator=(const tm & t);
template<class CStringType> Date & operator=(const CStringType * str);
template<class StringType> Date & operator=(const StringType & str);
/*
adding/subtracting time_t (seconds from the 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 the 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 the 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 the 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();
/*
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);
/*
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<class Stream>
void SerializeYearMonthDay(Stream & out) const;
/*
this method outputs to the given stream: HH:MM:SS, eg: 13:05:39
ISO 8601 format
*/
template<class Stream>
void SerializeHourMinSec(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<class Stream>
void Serialize(Stream & out) const;
/*
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
*/
template<class CStringType>
bool ParseDayMonthYear(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
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<class CStringType>
bool ParseYearMonthDay(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
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<class CStringType>
bool ParseMonthDay(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
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<class CStringType>
bool ParseHourMinSec(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
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<class CStringType>
bool ParseHourMin(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
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<class CStringType>
bool ParseTime(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
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<class CStringType>
bool Parse(const CStringType * str, const CStringType ** str_after = 0);
template<class StringType>
bool Parse(const StringType & str);
private:
void AssertRange(int & val, int val_min, int val_max);
template<class Stream>
void SerializeInt(Stream & out, int val) const;
template<class CStringType>
void SetAfter(const CStringType * str, const CStringType ** str_after);
template<class CStringType>
void SkipWhite(const CStringType * & str);
template<class CStringType>
bool ReadInt(const CStringType * & str, int & result);
template<class CStringType>
bool SkipSeparator(const CStringType * & str, int separator, int separator2 = -1, int separator3 = -1);
};
template<class CStringType>
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<class StringType>
Date::Date(const StringType & str)
{
Clear();
Parse(str);
}
template<class CStringType>
Date & Date::operator=(const CStringType * str)
{
Parse(str);
return *this;
}
template<class StringType>
Date & Date::operator=(const StringType & str)
{
Parse(str);
return *this;
}
template<class Stream>
void Date::SerializeInt(Stream & out, int val) const
{
if( val >= 0 && val < 10 )
out << '0';
out << val;
}
template<class Stream>
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 << '-';
SerializeInt(out, month);
out << '-';
SerializeInt(out, day);
}
template<class Stream>
void Date::SerializeHourMinSec(Stream & out) const
{
SerializeInt(out, hour);
out << ':';
SerializeInt(out, min);
out << ':';
SerializeInt(out, sec);
}
template<class Stream>
void Date::Serialize(Stream & out) const
{
SerializeYearMonthDay(out);
out << ' ';
SerializeHourMinSec(out);
}
template<class CStringType>
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<class StringType>
bool Date::ParseDayMonthYear(const StringType & str)
{
return ParseDayMonthYear(str.c_str());
}
template<class CStringType>
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<class StringType>
bool Date::ParseYearMonthDay(const StringType & str)
{
return ParseYearMonthDay(str.c_str());
}
template<class CStringType>
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<class StringType>
bool Date::ParseMonthDay(const StringType & str)
{
return ParseMonthDay(str.c_str());
}
template<class CStringType>
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<class StringType>
bool Date::ParseHourMinSec(const StringType & str)
{
return ParseHourMinSec(str.c_str());
}
template<class CStringType>
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<class StringType>
bool Date::ParseHourMin(const StringType & str)
{
return ParseHourMin(str.c_str());
}
template<class CStringType>
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<class StringType>
bool Date::ParseTime(const StringType & str)
{
return ParseTime(str.c_str());
}
template<class CStringType>
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<class StringType>
bool Date::Parse(const StringType & str)
{
return Parse(str.c_str());
}
template<class CStringType>
void Date::SetAfter(const CStringType * str, const CStringType ** str_after)
{
if( str_after )
*str_after = str;
}
template<class CStringType>
void Date::SkipWhite(const CStringType * & str)
{
while( *str==' ' || *str=='\t' )
str += 1;
}
template<class CStringType>
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<class CStringType>
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;
}
} // namespace
#endif