moved all directories to src subdirectory
This commit is contained in:
496
src/date/date.cpp
Normal file
496
src/date/date.cpp
Normal file
@@ -0,0 +1,496 @@
|
||||
/*
|
||||
* 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-2018, 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.
|
||||
*/
|
||||
|
||||
#include "date.h"
|
||||
|
||||
// for memset
|
||||
#include <string.h>
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
Date::Date()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
|
||||
Date::Date(const Date & d)
|
||||
{
|
||||
operator=(d);
|
||||
}
|
||||
|
||||
|
||||
Date::Date(time_t t)
|
||||
{
|
||||
FromTime(t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Date::Date(const tm & t)
|
||||
{
|
||||
FromTm(t);
|
||||
}
|
||||
|
||||
|
||||
Date::Date(const char * str)
|
||||
{
|
||||
// parsing can be break in the middle of the string (if errors)
|
||||
// and some values would not be initialized
|
||||
Clear();
|
||||
Parse(str);
|
||||
}
|
||||
|
||||
|
||||
Date::Date(const wchar_t * str)
|
||||
{
|
||||
Clear();
|
||||
Parse(str);
|
||||
}
|
||||
|
||||
|
||||
Date::Date(const std::string & str)
|
||||
{
|
||||
Clear();
|
||||
Parse(str);
|
||||
}
|
||||
|
||||
|
||||
Date::Date(const std::wstring & str)
|
||||
{
|
||||
Clear();
|
||||
Parse(str);
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator=(const Date & d)
|
||||
{
|
||||
year = d.year;
|
||||
month = d.month;
|
||||
day = d.day;
|
||||
hour = d.hour;
|
||||
min = d.min;
|
||||
sec = d.sec;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator=(time_t t)
|
||||
{
|
||||
FromTime(t);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Date & Date::operator=(const tm & t)
|
||||
{
|
||||
FromTm(t);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator=(const char * str)
|
||||
{
|
||||
Parse(str);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator=(const wchar_t * str)
|
||||
{
|
||||
Parse(str);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator=(const std::string & str)
|
||||
{
|
||||
Parse(str);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator=(const std::wstring & str)
|
||||
{
|
||||
Parse(str);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Date Date::operator+(time_t t) const
|
||||
{
|
||||
time_t t0 = ToTime();
|
||||
Date d(t0 + t);
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
Date Date::operator-(time_t t) const
|
||||
{
|
||||
time_t t0 = ToTime();
|
||||
Date d(t0 - t);
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator+=(time_t t)
|
||||
{
|
||||
time_t t0 = ToTime();
|
||||
FromTime(t0 + t);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Date & Date::operator-=(time_t t)
|
||||
{
|
||||
time_t t0 = ToTime();
|
||||
FromTime(t0 - t);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void Date::Swap(Date & date)
|
||||
{
|
||||
Date temp(*this);
|
||||
|
||||
*this = date;
|
||||
date = temp;
|
||||
}
|
||||
|
||||
|
||||
time_t Date::operator-(const Date & d) const
|
||||
{
|
||||
time_t t0 = ToTime();
|
||||
time_t t1 = d.ToTime();
|
||||
time_t res;
|
||||
|
||||
if( t1 >= t0 )
|
||||
res = t1 - t0;
|
||||
else
|
||||
res = t0 - t1;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
bool Date::IsTheSameDay(const Date & d) const
|
||||
{
|
||||
return year == d.year && month == d.month && day == d.day;
|
||||
}
|
||||
|
||||
|
||||
bool Date::IsTheSameHour(const Date & d) const
|
||||
{
|
||||
return hour == d.hour && min == d.min && sec == d.sec;
|
||||
}
|
||||
|
||||
|
||||
bool Date::operator==(const Date & d) const
|
||||
{
|
||||
return IsTheSameDay(d) && IsTheSameHour(d);
|
||||
}
|
||||
|
||||
|
||||
bool Date::operator!=(const Date & d) const
|
||||
{
|
||||
return !operator==(d);
|
||||
}
|
||||
|
||||
|
||||
int Date::Compare(const Date & d) const
|
||||
{
|
||||
if( year != d.year )
|
||||
return year - d.year;
|
||||
|
||||
if( month != d.month )
|
||||
return month - d.month;
|
||||
|
||||
if( day != d.day )
|
||||
return day - d.day;
|
||||
|
||||
if( hour != d.hour )
|
||||
return hour - d.hour;
|
||||
|
||||
if( min != d.min )
|
||||
return min - d.min;
|
||||
|
||||
if( sec != d.sec )
|
||||
return sec - d.sec;
|
||||
|
||||
// dates are equal
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
bool Date::operator>(const Date & d) const
|
||||
{
|
||||
return Compare(d) > 0;
|
||||
}
|
||||
|
||||
|
||||
bool Date::operator>=(const Date & d) const
|
||||
{
|
||||
return Compare(d) >= 0;
|
||||
}
|
||||
|
||||
|
||||
bool Date::operator<(const Date & d) const
|
||||
{
|
||||
return Compare(d) < 0;
|
||||
}
|
||||
|
||||
|
||||
bool Date::operator<=(const Date & d) const
|
||||
{
|
||||
return Compare(d) <= 0;
|
||||
}
|
||||
|
||||
|
||||
void Date::Clear()
|
||||
{
|
||||
year = 1970;
|
||||
month = 1;
|
||||
day = 1;
|
||||
hour = 0;
|
||||
min = 0;
|
||||
sec = 0;
|
||||
}
|
||||
|
||||
|
||||
void Date::AssertRange(int & val, int val_min, int val_max)
|
||||
{
|
||||
if( val < val_min )
|
||||
val = val_min;
|
||||
|
||||
if( val > val_max )
|
||||
val = val_max;
|
||||
}
|
||||
|
||||
|
||||
void Date::AssertCorrectDate()
|
||||
{
|
||||
// 10000 is only a 'cosmetic' limit
|
||||
// we can make calculations with greater values
|
||||
AssertRange(year, 1970, 10000);
|
||||
AssertRange(month, 1, 12);
|
||||
AssertRange(day, 1, MonthLen(year, month));
|
||||
AssertRange(hour, 0, 23);
|
||||
AssertRange(min, 0, 59);
|
||||
AssertRange(sec, 0, 59);
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
if( m == 2 && IsYearLeap(y) )
|
||||
return 29;
|
||||
|
||||
const int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
|
||||
if( m>=1 && m<=12 )
|
||||
return days[m-1];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Date::IsYearLeap(int y)
|
||||
{
|
||||
return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
|
||||
}
|
||||
|
||||
|
||||
bool Date::IsYearLeap() const
|
||||
{
|
||||
return IsYearLeap(year);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
return 'days' starts from 0000:03:01 (year 0, month 3 - March, day 1)
|
||||
*/
|
||||
long long Date::ToDays() const
|
||||
{
|
||||
long long m = (month + 9) % 12;
|
||||
long long y = year - m/10;
|
||||
|
||||
return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (day - 1);
|
||||
}
|
||||
|
||||
|
||||
time_t Date::ToTime() const
|
||||
{
|
||||
time_t res = time_t((ToDays() - 719468) * 60*60*24);
|
||||
res += hour * 60 * 60;
|
||||
res += min * 60;
|
||||
res += sec;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
tm Date::ToTm() const
|
||||
{
|
||||
tm t;
|
||||
|
||||
memset(&t, 0, sizeof(tm));
|
||||
t.tm_year = year - 1900;
|
||||
t.tm_mon = month - 1;
|
||||
t.tm_mday = day;
|
||||
t.tm_hour = hour;
|
||||
t.tm_min = min;
|
||||
t.tm_sec = sec;
|
||||
t.tm_wday = WeekDay();
|
||||
// t.tm_yday is not set
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
this method calculates year, month and a day from given 'days'
|
||||
'days' starts from 0000:03:01 (year 0, month 3 - March, day 1)
|
||||
*/
|
||||
void Date::FromDays(long long days)
|
||||
{
|
||||
//static_assert( sizeof(long long) >= 8 , "operation here should be used at least with 64 bits precision");
|
||||
|
||||
year = int(((long long)(10000)*days + 14780)/3652425);
|
||||
int delta = int(days - (365*year + year/4 - year/100 + year/400));
|
||||
|
||||
if( delta < 0 )
|
||||
{
|
||||
year -= 1;
|
||||
delta = int(days - (365*year + year/4 - year/100 + year/400));
|
||||
}
|
||||
|
||||
int mi = (100*delta + 52)/3060;
|
||||
month = (mi + 2)%12 + 1;
|
||||
year = year + (mi + 2)/12;
|
||||
day = delta - (mi*306 + 5)/10 + 1;
|
||||
}
|
||||
|
||||
|
||||
void Date::FromTime(time_t t)
|
||||
{
|
||||
time_t days = t / 60 / 60 / 24;
|
||||
time_t diff = t - days * 60 * 60 * 24;
|
||||
|
||||
hour = int(diff / 60 / 60);
|
||||
min = int((diff - hour * 60 * 60) / 60);
|
||||
sec = int((diff - hour * 60 * 60 - min * 60));
|
||||
|
||||
FromDays((long long)(days) + 719468);
|
||||
}
|
||||
|
||||
|
||||
void Date::FromTm(const tm & t)
|
||||
{
|
||||
year = t.tm_year + 1900;
|
||||
month = t.tm_mon + 1;
|
||||
day = t.tm_mday;
|
||||
hour = t.tm_hour;
|
||||
min = t.tm_min;
|
||||
sec = t.tm_sec;
|
||||
}
|
||||
|
||||
|
||||
int Date::WeekDay() const
|
||||
{
|
||||
long long d = ToDays();
|
||||
|
||||
return (int)((d+3) % 7);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
996
src/date/date.h
Normal file
996
src/date/date.h
Normal file
@@ -0,0 +1,996 @@
|
||||
/*
|
||||
* 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-2018, 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>
|
||||
#include <string>
|
||||
#include "convert/inttostr.h"
|
||||
|
||||
|
||||
|
||||
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);
|
||||
Date(const char * str);
|
||||
Date(const wchar_t * str);
|
||||
Date(const std::string & str);
|
||||
Date(const std::wstring & str);
|
||||
|
||||
Date & operator=(const Date & d);
|
||||
Date & operator=(time_t t);
|
||||
Date & operator=(const tm & t);
|
||||
Date & operator=(const char * str);
|
||||
Date & operator=(const wchar_t * str);
|
||||
Date & operator=(const std::string & str);
|
||||
Date & operator=(const std::wstring & 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);
|
||||
|
||||
|
||||
/*
|
||||
swapping the contents of *this with date
|
||||
*/
|
||||
void Swap(Date & date);
|
||||
|
||||
|
||||
/*
|
||||
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: MM-DD, eg. 02-12 (02 month, 12 day)
|
||||
*/
|
||||
template<class Stream>
|
||||
void SerializeMonthDay(Stream & out) const;
|
||||
|
||||
|
||||
/*
|
||||
this method outputs to the given stream: HH:MM, eg: 13:05
|
||||
*/
|
||||
template<class Stream>
|
||||
void SerializeHourMin(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;
|
||||
|
||||
|
||||
/*
|
||||
this method outputs to the given stream: YYYY-MM-DDTHH:MM:SSZ, eg: 1990-02-12T13:05:39Z
|
||||
ISO 8601 format
|
||||
*/
|
||||
template<class Stream>
|
||||
void SerializeISO(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);
|
||||
|
||||
template<class CStringType>
|
||||
bool ParseZoneOffset(const CStringType * str, const CStringType ** str_after = 0);
|
||||
|
||||
template<class StringType>
|
||||
bool ParseZoneOffset(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 month, day, hour and minutes (if exists) and seconds (if exists)
|
||||
the input string can be as follows:
|
||||
"10-23 14" -- only month, day and hour given (min and sec will be zero)
|
||||
"10-23 14:10" -- month, day and hour with minutes (sec will be zero)
|
||||
"10-23 14:10:35" -- month, day, hour, minutes and seconds
|
||||
|
||||
white characters are ommited so these are valid strings too:
|
||||
" 10 - 23 14 : 10 : 35 "
|
||||
" 10 - 23 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 ParseMonthDayTime(const CStringType * str, const CStringType ** str_after = 0);
|
||||
|
||||
template<class StringType>
|
||||
bool ParseMonthDayTime(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, size_t min_width) const;
|
||||
|
||||
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, size_t max_digits = 0);
|
||||
|
||||
template<class CStringType>
|
||||
bool SkipSeparator(const CStringType * & str, int separator, int separator2 = -1, int separator3 = -1);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class Stream>
|
||||
void Date::SerializeInt(Stream & out, int val, size_t min_width) const
|
||||
{
|
||||
char buf[64];
|
||||
size_t len;
|
||||
|
||||
if( Toa(val, buf, sizeof(buf) / sizeof(char), 10, &len) )
|
||||
{
|
||||
for(size_t i = len ; i < min_width ; ++i)
|
||||
{
|
||||
out << '0';
|
||||
}
|
||||
|
||||
out << buf;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Stream>
|
||||
void Date::SerializeInt(Stream & out, int val) const
|
||||
{
|
||||
SerializeInt(out, val, 2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class Stream>
|
||||
void Date::SerializeYearMonthDay(Stream & out) const
|
||||
{
|
||||
SerializeInt(out, year, 4);
|
||||
out << '-';
|
||||
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::SerializeMonthDay(Stream & out) const
|
||||
{
|
||||
SerializeInt(out, month);
|
||||
out << '-';
|
||||
SerializeInt(out, day);
|
||||
}
|
||||
|
||||
|
||||
template<class Stream>
|
||||
void Date::SerializeHourMin(Stream & out) const
|
||||
{
|
||||
SerializeInt(out, hour);
|
||||
out << ':';
|
||||
SerializeInt(out, min);
|
||||
}
|
||||
|
||||
|
||||
template<class Stream>
|
||||
void Date::Serialize(Stream & out) const
|
||||
{
|
||||
SerializeYearMonthDay(out);
|
||||
out << ' ';
|
||||
SerializeHourMinSec(out);
|
||||
}
|
||||
|
||||
template<class Stream>
|
||||
void Date::SerializeISO(Stream & out) const
|
||||
{
|
||||
SerializeYearMonthDay(out);
|
||||
out << 'T';
|
||||
SerializeHourMinSec(out);
|
||||
out << 'Z';
|
||||
}
|
||||
|
||||
|
||||
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::ParseZoneOffset(const CStringType * str, const CStringType ** str_after)
|
||||
{
|
||||
bool result = false;
|
||||
bool is_sign = false;
|
||||
int offset_hour = 0;
|
||||
int offset_min = 0;
|
||||
|
||||
SkipWhite(str);
|
||||
|
||||
if( *str == '-' || *str == '+' )
|
||||
{
|
||||
if( *str == '-' )
|
||||
is_sign = true;
|
||||
|
||||
str += 1;
|
||||
|
||||
if( ReadInt(str, offset_hour, 2) && offset_hour >= -12 && offset_hour <= 14 )
|
||||
{
|
||||
SkipWhite(str);
|
||||
SetAfter(str, str_after);
|
||||
|
||||
if( *str == ':' )
|
||||
{
|
||||
str += 1;
|
||||
SkipWhite(str);
|
||||
SetAfter(str, str_after);
|
||||
}
|
||||
|
||||
if( ReadInt(str, offset_min, 2) && offset_min > -60 && offset_min < 60 )
|
||||
{
|
||||
SetAfter(str, str_after);
|
||||
}
|
||||
else
|
||||
{
|
||||
offset_min = 0;
|
||||
}
|
||||
|
||||
time_t offset = (time_t)offset_hour * 60 * 60 + (time_t)offset_min * 60;
|
||||
result = true;
|
||||
|
||||
if( is_sign )
|
||||
offset = -offset;
|
||||
|
||||
FromTime(ToTime() - offset);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
bool Date::ParseZoneOffset(const StringType & str)
|
||||
{
|
||||
return ParseZoneOffset(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::ParseMonthDayTime(const CStringType * str, const CStringType ** str_after)
|
||||
{
|
||||
const CStringType * after;
|
||||
bool result = false;
|
||||
|
||||
if( ParseMonthDay(str, &after) )
|
||||
if( ParseTime(after, &after) )
|
||||
result = true;
|
||||
|
||||
SetAfter(after, str_after);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
bool Date::ParseMonthDayTime(const StringType & str)
|
||||
{
|
||||
return ParseMonthDayTime(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) )
|
||||
{
|
||||
SkipWhite(after);
|
||||
|
||||
if( *after == 'T' )
|
||||
{
|
||||
// ISO 8601 format
|
||||
// https://en.wikipedia.org/wiki/ISO_8601
|
||||
// at the moment skip the 'T' character only
|
||||
after += 1;
|
||||
}
|
||||
|
||||
if( ParseHourMinSec(after, &after) )
|
||||
{
|
||||
SkipWhite(after);
|
||||
result = true;
|
||||
|
||||
if( *after == 'Z' )
|
||||
{
|
||||
after += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we dont have to check errors here
|
||||
ParseZoneOffset(after, &after);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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, size_t max_digits)
|
||||
{
|
||||
bool something_read = false;
|
||||
|
||||
SkipWhite(str);
|
||||
result = 0;
|
||||
size_t len = 0;
|
||||
|
||||
while( *str >= '0' && *str <= '9' && (max_digits == 0 || len < max_digits))
|
||||
{
|
||||
result = result * 10 + (*str - '0');
|
||||
str += 1;
|
||||
len += 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
|
Reference in New Issue
Block a user