Skip to content

Commit

Permalink
fixup!
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Sep 13, 2024
1 parent b28408d commit 80e260f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
14 changes: 7 additions & 7 deletions libgnucash/engine/gnc-datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static const TZ_Ptr utc_zone(new boost::local_time::posix_time_zone("UTC-0"));
void _set_tzp(TimeZoneProvider& tz);
void _reset_tzp();

static boost::gregorian::date gregorian_date_from_locale_string (const std::string& str);
static Date gregorian_date_from_locale_string (const std::string& str);

/* To ensure things aren't overly screwed up by setting the nanosecond clock for boost::date_time. Don't do it, though, it doesn't get us anything and slows down the date/time library. */
#ifndef BOOST_DATE_TIME_HAS_NANOSECONDS
Expand All @@ -91,7 +91,6 @@ static constexpr auto ticks_per_second = INT64_C(1000000000);
* without separators.
*/
const std::vector<GncDateFormat> GncDate::c_formats ({
GncDateFormat { N_("Locale"), gregorian_date_from_locale_string },
GncDateFormat {
N_("y-m-d"),
boost::gregorian::from_string,
Expand Down Expand Up @@ -156,7 +155,8 @@ const std::vector<GncDateFormat> GncDate::c_formats ({
"(?<DAY>[0-9]{2})"
"(?<YEAR>[0-9]+)?"
")"
}
},
GncDateFormat { N_("Locale"), gregorian_date_from_locale_string },
});

/** Private implementation of GncDateTime. See the documentation for that class.
Expand Down Expand Up @@ -659,7 +659,7 @@ get_icu_resources()
return rv;
}

boost::gregorian::date
Date
static gregorian_date_from_locale_string (const std::string& str)
{
ICUResources& resources = get_icu_resources();
Expand All @@ -676,9 +676,9 @@ static gregorian_date_from_locale_string (const std::string& str)
if (U_FAILURE(status))
throw std::invalid_argument ("Cannot set calendar time");

return boost::gregorian::date (resources.calendar->get(UCAL_YEAR, status),
resources.calendar->get(UCAL_MONTH, status) + 1,
resources.calendar->get(UCAL_DATE, status));
return Date (resources.calendar->get(UCAL_YEAR, status),
resources.calendar->get(UCAL_MONTH, status) + 1,
resources.calendar->get(UCAL_DATE, status));
}

/* Member function definitions for GncDateImpl.
Expand Down
57 changes: 29 additions & 28 deletions libgnucash/engine/test/gtest-gnc-datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ TEST(gnc_date_constructors, test_str_format_constructor)
{ "y-m-d", "1985.3.12", 1985, 3, 12},
{ "y-m-d", "3'6'8", 2003, 6, 8},
{ "y-m-d", "20130801", 2013, 8, 1},
{ "y-m-d", "2013 Aug 1", 2013, 8, 1},
{ "y-m-d", "2013 Aug 01",2013, 8, 1},
{ "y-m-d", "2013 August 01", 2013, 8, 1},
{ "y-m-d", "2013-August-1", 2013, 8, 1},
{ "y-m-d", "2009/Nov/04",2009, 11, 4},
{ "y-m-d","1985.Mar.12", 1985, 3, 12},
{ "d-m-y", "01-08-2013", 2013, 8, 1},
{ "d-m-y", "01-8-2013", 2013, 8, 1},
{ "d-m-y", "1-08-2013", 2013, 8, 1},
Expand All @@ -101,6 +107,11 @@ TEST(gnc_date_constructors, test_str_format_constructor)
{ "d-m-y", "12.3.1985", 1985, 3, 12},
{ "d-m-y", "8'6'3", 2003, 6, 8},
{ "d-m-y", "01082013", 2013, 8, 1},
{ "d-m-y", "1 Aug 2013", 2013, 8, 1},
{ "d-m-y", "1 Sep 2013", 2013, 9, 1},
{ "d-m-y", "1 September 2013", 2013, 9, 1},
{ "d-m-y", "04/11/2009", 2009, 11, 4},
{ "d-m-y", "12.3.1985", 1985, 3, 12},
{ "m-d-y", "08-01-2013", 2013, 8, 1},
{ "m-d-y", "8-01-2013", 2013, 8, 1},
{ "m-d-y", "08-1-2013", 2013, 8, 1},
Expand All @@ -113,6 +124,8 @@ TEST(gnc_date_constructors, test_str_format_constructor)
{ "m-d-y", "3.12.1985", 1985, 3, 12},
{ "m-d-y", "6'8'3", 2003, 6, 8},
{ "m-d-y", "08012013", 2013, 8, 1},
{ "m-d-y", "November 4, 2009", 2009, 11, 4},
{ "m-d-y", "Nov 4, 2009", 2009, 11, 4},
{ "d-m", "01-08", curr_year, 8, 1},
{ "d-m", "01-8", curr_year, 8, 1},
{ "d-m", "1-08", curr_year, 8, 1},
Expand All @@ -130,45 +143,29 @@ TEST(gnc_date_constructors, test_str_format_constructor)
{ "m-d", "6'8", curr_year, 6, 8},
{ "m-d", "0801", curr_year, 8, 1},

{ "d-m-y", "1 Aug 2013", 2013, 8, 1},
{ "d-m-y", "1 Sep 2013", 2013, 9, 1},
{ "d-m-y", "1 September 2013", 2013, 9, 1},
{ "d-m-y", "04/11/2009", 2009, 11, 4},
{ "d-m-y", "12.3.1985", 1985, 3, 12},

// invalid dates
{ "d-m-y", "0 Aug 2013", -1, -1, -1},
{ "d-m-y", "31 Sep 2013", -1, -1, -1},
{ "d-m-y", "31 September 2013", -1, -1, -1},
{ "d-m-y", "1 Sep 13", -1, -1, -1},
{ "d-m-y", "1 September 13", -1, -1, -1},
{ "d-m-y", "31/11/2009", -1, -1, -1},
{ "d-m-y", "34.3.1985", -1, -1, -1},

{ "m-d-y", "November 4, 2009", 2009, 11, 4},
{ "m-d-y", "Nov 4, 2009", 2009, 11, 4},

{ "m-d-y", "November 4, 24", -1, -1, -1},
{ "m-d-y", "Nov 4, 23", -1, -1, -1},
{ "d-m-y", "31/11/2009", -2, -2, -2},
{ "d-m-y", "34.3.1985", -2, -2, -2},
{ "m-d-y", "November 41, 2009", -1, -1, -1},
{ "m-d-y", "Nov 31, 2009", -1, -1, -1},
{ "m-d-y", "Nov 29, 24", -1, -1, -1},

{ "y-m-d", "2013 Aug 1", 2013, 8, 1},
{ "y-m-d", "2013 Aug 01", 2013, 8, 1},
{ "y-m-d", "2013 August 01", 2013, 8, 1},
{ "y-m-d", "2013-August-1", 2013, 8, 1},
{ "y-m-d", "2009/Nov/04", 2009, 11, 4},
{ "y-m-d", "1985.Mar.12", 1985, 3, 12},

{ "y-m-d", "2013 Aug 0", -1, -1, -1},
{ "y-m-d", "2013 Feb 30", -1, -1, -1},
{ "y-m-d", "2013 August 0", -1, -1, -1},
{ "y-m-d", "2013-June-31", -1, -1, -1},
{ "y-m-d", "13-June-21", -1, -1, -1},
{ "y-m-d", "2009/Nov/0", -1, -1, -1},
{ "m-m-d", "04/11/09", -1, -1, -1},
{ "y-m-d", "1985.Mar.32", -1, -1, -1},

// 2-digit dates are not parsable with months as words
{ "d-m-y", "1 Sep 13", -1, -1, -1},
{ "d-m-y", "1 September 13", -1, -1, -1},
{ "m-d-y", "November 4, 24", -1, -1, -1},
{ "m-d-y", "Nov 4, 23", -1, -1, -1},
{ "m-d-y", "Nov 29, 24", -1, -1, -1},
{ "y-m-d", "13-June-11", -1, -1, -1},

// ambiguous date formats
// current parser doesn't know how to disambiguate
// and hence refuses to parse
Expand Down Expand Up @@ -221,10 +218,14 @@ TEST(gnc_date_constructors, test_str_format_constructor)
got_month = test_ymd.month;
got_day = test_ymd.day;
}
catch (...)
catch (const std::invalid_argument&)
{
got_year = got_month = got_day = -1;
}
catch (const std::out_of_range&)
{
got_year = got_month = got_day = -2;
}

EXPECT_TRUE ((got_year == test_dates[i].exp_year) &&
(got_month == test_dates[i].exp_month) &&
Expand Down

0 comments on commit 80e260f

Please sign in to comment.