2012-05-05 17:08:04 +02:00
|
|
|
/*
|
|
|
|
* 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>
|
2012-05-27 00:38:18 +02:00
|
|
|
#include <string>
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace PT
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
this class represents a Date (year, month, day, hour, min, sec)
|
2012-05-23 01:47:41 +02:00
|
|
|
it has O(1) algorithm when converting from/to time_t (seconds from the Unix Epoch)
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
algorithm description:
|
|
|
|
http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html
|
|
|
|
http://alcor.concordia.ca/~gpkatch/gdate-method.html
|
2012-05-23 01:47:41 +02:00
|
|
|
|
|
|
|
current limitation:
|
|
|
|
we do not support leap seconds
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
class Date
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02: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
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
Date();
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
converting from Date, time_t (seconds from the Unix Epoch), tm structure, and strings
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
Date(const Date & d);
|
|
|
|
Date(time_t t);
|
|
|
|
Date(const tm & t);
|
2012-05-27 00:38:18 +02:00
|
|
|
Date(const char * str);
|
|
|
|
Date(const wchar_t * str);
|
|
|
|
Date(const std::string & str);
|
|
|
|
Date(const std::wstring & str);
|
2012-05-23 01:47:41 +02:00
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
Date & operator=(const Date & d);
|
|
|
|
Date & operator=(time_t t);
|
|
|
|
Date & operator=(const tm & t);
|
2012-05-27 00:38:18 +02:00
|
|
|
Date & operator=(const char * str);
|
|
|
|
Date & operator=(const wchar_t * str);
|
|
|
|
Date & operator=(const std::string & str);
|
|
|
|
Date & operator=(const std::wstring & str);
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
adding/subtracting time_t (seconds from the Unix Epoch)
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
Date operator+(time_t t) const;
|
|
|
|
Date operator-(time_t t) const;
|
|
|
|
Date & operator+=(time_t t);
|
|
|
|
Date & operator-=(time_t t);
|
|
|
|
|
|
|
|
|
2012-05-27 00:38:18 +02:00
|
|
|
/*
|
|
|
|
swapping the contents of *this with date
|
|
|
|
*/
|
|
|
|
void Swap(Date & date);
|
|
|
|
|
|
|
|
|
2012-05-05 17:13:45 +02:00
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
converts time_t in seconds (from the Unix Epoch) to this object
|
2012-05-05 17:13:45 +02:00
|
|
|
*/
|
|
|
|
void FromTime(time_t t);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
converts tm structure to this object
|
|
|
|
*/
|
|
|
|
void FromTm(const tm & t);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
returns time_t (in seconds from the Unix Epoch)
|
2012-05-05 17:13:45 +02:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
set the Unix Epoch: 1970.01.01 00:00:00
|
2012-05-05 17:08:04 +02: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();
|
|
|
|
|
|
|
|
|
2012-05-23 01:47:41 +02:00
|
|
|
/*
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
/*
|
|
|
|
returns how many days there is in a month
|
|
|
|
y - year 1970 - ...
|
|
|
|
m - month 1-12
|
|
|
|
*/
|
2012-05-23 01:47:41 +02:00
|
|
|
static int MonthLen(int y, int m);
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
this method outputs to the given stream: YYYY-MM-DD, eg. 1990-02-12
|
2012-05-13 23:49:06 +02:00
|
|
|
ISO 8601 format
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
template<class Stream>
|
2012-05-23 01:47:41 +02:00
|
|
|
void SerializeYearMonthDay(Stream & out) const;
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-05-13 23:49:06 +02:00
|
|
|
this method outputs to the given stream: HH:MM:SS, eg: 13:05:39
|
|
|
|
ISO 8601 format
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
template<class Stream>
|
2012-05-23 01:47:41 +02:00
|
|
|
void SerializeHourMinSec(Stream & out) const;
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
|
2012-06-23 01:35:16 +02:00
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
/*
|
2012-05-13 23:49:06 +02:00
|
|
|
this method outputs to the given stream: YYYY-MM-DD HH:MM:SS, eg: 1990-02-12 13:05:39
|
|
|
|
ISO 8601 format
|
2012-05-05 17:08:04 +02:00
|
|
|
*/
|
|
|
|
template<class Stream>
|
|
|
|
void Serialize(Stream & out) const;
|
|
|
|
|
2012-05-13 23:49:06 +02:00
|
|
|
|
|
|
|
/*
|
2012-05-23 01:47:41 +02:00
|
|
|
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
|
2012-05-13 23:49:06 +02:00
|
|
|
*/
|
2012-05-23 01:47:41 +02:00
|
|
|
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
|
2012-05-30 23:48:44 +02:00
|
|
|
*/
|
2012-05-23 01:47:41 +02:00
|
|
|
template<class CStringType>
|
|
|
|
bool ParseTime(const CStringType * str, const CStringType ** str_after = 0);
|
|
|
|
|
|
|
|
template<class StringType>
|
|
|
|
bool ParseTime(const StringType & str);
|
|
|
|
|
|
|
|
|
2012-05-30 23:48:44 +02:00
|
|
|
/*
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
2012-05-23 01:47:41 +02:00
|
|
|
/*
|
|
|
|
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);
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void AssertRange(int & val, int val_min, int val_max);
|
|
|
|
|
|
|
|
template<class Stream>
|
|
|
|
void SerializeInt(Stream & out, int val) const;
|
|
|
|
|
2012-05-23 01:47:41 +02:00
|
|
|
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);
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 01:47:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
template<class Stream>
|
|
|
|
void Date::SerializeInt(Stream & out, int val) const
|
|
|
|
{
|
2012-05-23 01:47:41 +02:00
|
|
|
if( val >= 0 && val < 10 )
|
2012-05-05 17:08:04 +02:00
|
|
|
out << '0';
|
|
|
|
|
|
|
|
out << val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class Stream>
|
2012-05-23 01:47:41 +02:00
|
|
|
void Date::SerializeYearMonthDay(Stream & out) const
|
2012-05-05 17:08:04 +02:00
|
|
|
{
|
2012-05-13 23:49:06 +02:00
|
|
|
// !! IMPROVE ME the year should be printed with 4 digits, e.g. 0001 when the year is equal to one
|
|
|
|
out << year << '-';
|
2012-05-05 17:08:04 +02:00
|
|
|
SerializeInt(out, month);
|
2012-05-13 23:49:06 +02:00
|
|
|
out << '-';
|
2012-05-05 17:08:04 +02:00
|
|
|
SerializeInt(out, day);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class Stream>
|
2012-05-23 01:47:41 +02:00
|
|
|
void Date::SerializeHourMinSec(Stream & out) const
|
2012-05-05 17:08:04 +02:00
|
|
|
{
|
|
|
|
SerializeInt(out, hour);
|
|
|
|
out << ':';
|
|
|
|
SerializeInt(out, min);
|
|
|
|
out << ':';
|
|
|
|
SerializeInt(out, sec);
|
|
|
|
}
|
|
|
|
|
2012-05-23 01:47:41 +02:00
|
|
|
|
2012-06-23 01:35:16 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-05 17:08:04 +02:00
|
|
|
template<class Stream>
|
|
|
|
void Date::Serialize(Stream & out) const
|
|
|
|
{
|
2012-05-23 01:47:41 +02:00
|
|
|
SerializeYearMonthDay(out);
|
2012-05-05 17:08:04 +02:00
|
|
|
out << ' ';
|
2012-05-23 01:47:41 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-30 23:48:44 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 01:47:41 +02:00
|
|
|
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;
|
2012-05-05 17:08:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif
|