From 1e5598cde1673ddf0a35527522bd7dea8482f44c Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 6 Jul 2021 21:44:04 +0200 Subject: [PATCH] added to Date: SerializeMonthAsRoman(Stream & out, int month) - serialize month in Roman numerals added a param: 'bool roman_month' to some serialize methods --- src/date/date.h | 61 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/src/date/date.h b/src/date/date.h index 59938c3..c9ba433 100644 --- a/src/date/date.h +++ b/src/date/date.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012-2018, Tomasz Sowa + * Copyright (c) 2012-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -251,12 +251,21 @@ public: int WeekDay() const; + /* + this method outputs to the given stream the number of a month in Roman numerals + e.g: if month is equal to 3 then III will be put to 'out' + month should be in the range [1,12] + */ + template + static void SerializeMonthAsRoman(Stream & out, int month); + + /* this method outputs to the given stream: YYYY-MM-DD, eg. 1990-02-12 ISO 8601 format */ template - void SerializeYearMonthDay(Stream & out) const; + void SerializeYearMonthDay(Stream & out, bool roman_month = false) const; /* @@ -271,7 +280,7 @@ public: this method outputs to the given stream: MM-DD, eg. 02-12 (02 month, 12 day) */ template - void SerializeMonthDay(Stream & out) const; + void SerializeMonthDay(Stream & out, bool roman_month = false) const; /* @@ -286,7 +295,7 @@ public: ISO 8601 format */ template - void Serialize(Stream & out) const; + void Serialize(Stream & out, bool roman_month = false, bool with_seconds = true) const; /* @@ -547,13 +556,32 @@ void Date::SerializeInt(Stream & out, int val) const } +template +void Date::SerializeMonthAsRoman(Stream & out, int month) +{ + const char * month_roman[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII" }; + + if( month >= 1 && month <= 12 ) + { + const char * month_str = month_roman[month - 1]; + + for( ; *month_str ; ++month_str) + out << *month_str; + } +} + template -void Date::SerializeYearMonthDay(Stream & out) const +void Date::SerializeYearMonthDay(Stream & out, bool roman_month) const { SerializeInt(out, year, 4); out << '-'; - SerializeInt(out, month); + + if( roman_month ) + SerializeMonthAsRoman(out, month); + else + SerializeInt(out, month); + out << '-'; SerializeInt(out, day); } @@ -571,9 +599,13 @@ void Date::SerializeHourMinSec(Stream & out) const template -void Date::SerializeMonthDay(Stream & out) const +void Date::SerializeMonthDay(Stream & out, bool roman_month) const { - SerializeInt(out, month); + if( roman_month ) + SerializeMonthAsRoman(out, month); + else + SerializeInt(out, month); + out << '-'; SerializeInt(out, day); } @@ -589,17 +621,22 @@ void Date::SerializeHourMin(Stream & out) const template -void Date::Serialize(Stream & out) const +void Date::Serialize(Stream & out, bool roman_month, bool with_seconds) const { - SerializeYearMonthDay(out); + SerializeYearMonthDay(out, roman_month); out << ' '; - SerializeHourMinSec(out); + + if( with_seconds ) + SerializeHourMinSec(out); + else + SerializeHourMin(out); } + template void Date::SerializeISO(Stream & out) const { - SerializeYearMonthDay(out); + SerializeYearMonthDay(out, false); out << 'T'; SerializeHourMinSec(out); out << 'Z';