Save Changes Bug 792106 - Wrong dates displayed

The first attempt to fix this, a17bc85a, doesn't work because the
boost::date_time constructor gets enough information in most cases to
generate a date, just not the one we expect. This change looks for '-' in
the fourth position and if it's there assumes iso-extended format, otherwise
it assumes delimiter-less ISO without the 'T', i.e. %Y%m%d%H%M%S.
This commit is contained in:
John Ralls 2018-01-09 14:58:43 -08:00
parent fcf88262ca
commit ebbcd30f39
2 changed files with 15 additions and 9 deletions

View File

@ -294,16 +294,12 @@ GncDateTimeImpl::GncDateTimeImpl(const std::string str) :
auto input_facet(new Facet());
std::istringstream ss(str.substr(0, tzpos));
ss.imbue(std::locale(std::locale(), input_facet));
input_facet->set_iso_extended_format();
if (str.find("-") == 4)
input_facet->set_iso_extended_format();
else /* Not in iso format, try squashed format. */
input_facet->format("%Y%m%d%H%M%S");
PTime pdt(not_a_date_time);
ss >> pdt;
if (pdt.is_special())
{
input_facet->format("%Y%m%d%H%M%S");
ss.clear(); //Reset to the beginning.
ss.seekg(0);
ss >> pdt;
}
m_time = LDT(pdt.date(), pdt.time_of_day(), tzptr,
LDTBase::NOT_DATE_TIME_ON_ERROR);
}
@ -450,7 +446,7 @@ GncDateTime::GncDateTime(const time64 time) :
m_impl(new GncDateTimeImpl(time)) {}
GncDateTime::GncDateTime(const struct tm tm) :
m_impl(new GncDateTimeImpl(tm)) {}
GncDateTime::GncDateTime(const std::string str, std::string fmt) :
GncDateTime::GncDateTime(const std::string str) :
m_impl(new GncDateTimeImpl(str)) {}
GncDateTime::~GncDateTime() = default;

View File

@ -293,6 +293,16 @@ TEST(gnc_datetime_constructors, test_string_constructor)
EXPECT_EQ(tm.tm_hour, 15);
EXPECT_EQ(tm.tm_min, 8);
EXPECT_EQ(tm.tm_sec, 19);
/* Squashed format from SQLite3 databases */
timestr = "20151205115703";
GncDateTime time4(timestr);
tm = time4.utc_tm();
EXPECT_EQ(tm.tm_year, 115);
EXPECT_EQ(tm.tm_mon, 11);
EXPECT_EQ(tm.tm_mday, 5);
EXPECT_EQ(tm.tm_hour,11);
EXPECT_EQ(tm.tm_min, 57);
EXPECT_EQ(tm.tm_sec, 3);
}
TEST(gnc_datetime_constructors, test_struct_tm_constructor)