Skip to content

Commit 5d08bb9

Browse files
authored
Merge pull request #2146 from Exiv2/mainDatesWithZeros
DateValue is now a bit more permissive with malformed dates
2 parents c115d4d + 56c52be commit 5d08bb9

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/value.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -914,21 +914,35 @@ namespace Exiv2 {
914914
{
915915
// ISO 8601 date formats:
916916
// https://web.archive.org/web/20171020084445/https://www.loc.gov/standards/datetime/ISO_DIS%208601-1.pdf
917-
static const std::regex reExtended(R"(^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))");
918-
static const std::regex reBasic(R"(^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01]))");
917+
static const std::regex reExtended(R"(^(\d{4})-(\d{2})-(\d{2}))");
918+
static const std::regex reBasic(R"(^(\d{4})(\d{2})(\d{2}))");
919919
std::smatch sm;
920920

921+
auto printWarning = [](){
922+
#ifndef SUPPRESS_WARNINGS
923+
EXV_WARNING << Error(kerUnsupportedDateFormat) << "\n";
924+
#endif
925+
};
926+
921927
// Note: We use here regex_search instead of regex_match, because the string can be longer than expected and
922928
// also contain the time
923929
if (std::regex_search(buf, sm, reExtended) || std::regex_search(buf, sm, reBasic)) {
924-
date_.year = std::stoi(sm[1].str());
925-
date_.month = std::stoi(sm[2].str());
926-
date_.day = std::stoi(sm[3].str());
927-
return 0;
930+
date_.year = std::stoi(sm[1].str());
931+
date_.month = std::stoi(sm[2].str());
932+
if (date_.month > 12) {
933+
date_.month = 0;
934+
printWarning();
935+
return 1;
936+
}
937+
date_.day = std::stoi(sm[3].str());
938+
if (date_.day > 31) {
939+
date_.day = 0;
940+
printWarning();
941+
return 1;
942+
}
943+
return 0;
928944
}
929-
#ifndef SUPPRESS_WARNINGS
930-
EXV_WARNING << Error(kerUnsupportedDateFormat) << "\n";
931-
#endif
945+
printWarning();
932946
return 1;
933947
}
934948

unitTests/test_DateValue.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ TEST(ADateValue, readFromStringWithExpectedSizeAndDashes)
101101
ASSERT_EQ(2, dateValue.getDate().day);
102102
}
103103

104+
TEST(ADateValue, readFromStringWithExpectedSizeAndDashesAndZeroes)
105+
{
106+
DateValue dateValue;
107+
const std::string date ("2018-00-00");
108+
ASSERT_EQ(0, dateValue.read(date));
109+
ASSERT_EQ(2018, dateValue.getDate().year);
110+
ASSERT_EQ(0, dateValue.getDate().month);
111+
ASSERT_EQ(0, dateValue.getDate().day);
112+
}
113+
104114
TEST(ADateValue, readFromStringWithExpectedSizeWithoutDashes)
105115
{
106116
DateValue dateValue;
@@ -111,6 +121,17 @@ TEST(ADateValue, readFromStringWithExpectedSizeWithoutDashes)
111121
ASSERT_EQ(2, dateValue.getDate().day);
112122
}
113123

124+
TEST(ADateValue, readFromStringWithExpectedSizeWithoutDashesAndZeroes)
125+
{
126+
DateValue dateValue;
127+
const std::string date ("20180000");
128+
ASSERT_EQ(0, dateValue.read(date));
129+
ASSERT_EQ(2018, dateValue.getDate().year);
130+
ASSERT_EQ(0, dateValue.getDate().month);
131+
ASSERT_EQ(0, dateValue.getDate().day);
132+
}
133+
134+
114135
TEST(ADateValue, readFromStringWithTime)
115136
{
116137
DateValue dateValue;

0 commit comments

Comments
 (0)