From f97c06d4418cc06115b1ba17d8a95a2eee745a8b Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 22 Oct 2022 16:26:14 +0200 Subject: [PATCH] add a check_time_zone parameter when parsing a date --- src/date/date.h | 78 +++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/src/date/date.h b/src/date/date.h index 6400ea6..34b94ef 100644 --- a/src/date/date.h +++ b/src/date/date.h @@ -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 - 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 + bool Parse(const CStringType * str, bool check_time_zone = true); template - 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 -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 +bool Date::Parse(const CStringType * str, bool check_time_zone) +{ + const CStringType * str_after = nullptr; + return Parse(str, &str_after, check_time_zone); } template -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); }