add a check_time_zone parameter when parsing a date

This commit is contained in:
Tomasz Sowa 2022-10-22 16:26:14 +02:00
parent e501a3f4a3
commit f97c06d441
1 changed files with 46 additions and 32 deletions

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2012-2021, Tomasz Sowa
* Copyright (c) 2012-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -494,11 +494,13 @@ public:
(by using IsCorrectDate())
*/
template<class CStringType>
bool Parse(const CStringType * str, const CStringType ** str_after = 0);
bool Parse(const CStringType * str, const CStringType ** str_after, bool check_time_zone = true);
template<class CStringType>
bool Parse(const CStringType * str, bool check_time_zone = true);
template<class StringType>
bool Parse(const StringType & str);
bool Parse(const StringType & str, bool check_time_zone = true);
private:
@ -897,53 +899,65 @@ bool Date::ParseMonthDayTime(const StringType & str)
template<class CStringType>
bool Date::Parse(const CStringType * str, const CStringType ** str_after)
bool Date::Parse(const CStringType * str, const CStringType ** str_after, bool check_time_zone)
{
const CStringType * after;
bool result = false;
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) )
if( ParseYearMonthDay(str, &after) )
{
SkipWhite(after);
result = true;
if( *after == 'Z' )
if( check_time_zone && *after == 'T' )
{
// ISO 8601 format
// https://en.wikipedia.org/wiki/ISO_8601
// at the moment skip the 'T' character only
after += 1;
}
else
if( ParseHourMinSec(after, &after) )
{
// we dont have to check errors here
ParseZoneOffset(after, &after);
SkipWhite(after);
result = true;
if( check_time_zone )
{
if( *after == 'Z' )
{
after += 1;
}
else
{
// we dont have to check errors here
ParseZoneOffset(after, &after);
}
}
}
}
}
SetAfter(after, str_after);
SetAfter(after, str_after);
if( result )
result = IsCorrectDate();
if( result )
result = IsCorrectDate();
return result;
return result;
}
template<class CStringType>
bool Date::Parse(const CStringType * str, bool check_time_zone)
{
const CStringType * str_after = nullptr;
return Parse(str, &str_after, check_time_zone);
}
template<class StringType>
bool Date::Parse(const StringType & str)
bool Date::Parse(const StringType & str, bool check_time_zone)
{
return Parse(str.c_str());
const typename StringType::value_type * after_string = nullptr;
return Parse(str.c_str(), &after_string, check_time_zone);
}