Skip to content

Commit

Permalink
Add dateformat "Locale" with ICU, and UK/US/ISO date with boost
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Sep 12, 2024
1 parent 9dee343 commit aef7dfd
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
84 changes: 82 additions & 2 deletions libgnucash/engine/gnc-datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/locale.hpp>
#include <boost/regex.hpp>
#include <unicode/smpdtfmt.h>
#include <unicode/locid.h>
#include <unicode/udat.h>
#include <unicode/parsepos.h>
#include <unicode/calendar.h>
#include <libintl.h>
#include <locale.h>
#include <map>
Expand Down Expand Up @@ -70,20 +75,26 @@ 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);

/* 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
static constexpr auto ticks_per_second = INT64_C(1000000);
#else
static constexpr auto ticks_per_second = INT64_C(1000000000);
#endif

/* Vector of date formats understood by gnucash and corresponding regex
* to parse each from an external source
/* Vector of date formats understood by gnucash and corresponding
* boost string->date or regex to parse each from an external source
* Note: while the format names are using a "-" as separator, the
* regexes will accept any of "-/.' " and will also work for dates
* without separators.
*/
const std::vector<GncDateFormat> GncDate::c_formats ({
GncDateFormat { N_("Locale"), gregorian_date_from_locale_string },
GncDateFormat { N_("UK date"), boost::gregorian::from_uk_string },
GncDateFormat { N_("US date"), boost::gregorian::from_us_string },
GncDateFormat { N_("ISO date"), boost::gregorian::from_string },
GncDateFormat {
N_("y-m-d"),
"(?:" // either y-m-d
Expand Down Expand Up @@ -607,6 +618,69 @@ GncDateTimeImpl::timestamp()
return str.substr(0, 8) + str.substr(9, 15);
}

struct ICUResources
{
std::unique_ptr<icu::DateFormat> formatter;
std::unique_ptr<icu::Calendar> calendar;
};

static ICUResources&
get_icu_resources()
{
static ICUResources rv;

if (!rv.formatter)
{
icu::Locale locale;

const char* lc_time_locale = setlocale(LC_TIME, nullptr);
if (lc_time_locale != nullptr)
{
std::string localeStr(lc_time_locale);
size_t dotPos = localeStr.find('.');
if (dotPos != std::string::npos)
localeStr = localeStr.substr(0, dotPos);

locale = icu::Locale::createCanonical (localeStr.c_str());
}

rv.formatter.reset(icu::DateFormat::createDateInstance(icu::DateFormat::kDefault, locale));
if (!rv.formatter)
throw std::invalid_argument("Cannot create date formatter.");

UErrorCode status = U_ZERO_ERROR;
rv.calendar.reset(icu::Calendar::createInstance(locale, status));
if (U_FAILURE(status))
throw std::invalid_argument("Cannot create calendar instance.");

rv.calendar->setLenient(false);
}

return rv;
}

boost::gregorian::date
static gregorian_date_from_locale_string (const std::string& str)
{
ICUResources& resources = get_icu_resources();

icu::UnicodeString input = icu::UnicodeString::fromUTF8(str);
icu::ParsePosition parsePos;

UDate date = resources.formatter->parse(input, parsePos);
if (parsePos.getErrorIndex() != -1 || parsePos.getIndex() != input.length())
throw std::invalid_argument ("Cannot parse string");

UErrorCode status = U_ZERO_ERROR;
resources.calendar->setTime(date, status);
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));
}

/* Member function definitions for GncDateImpl.
*/
GncDateImpl::GncDateImpl(const std::string str, const std::string fmt) :
Expand All @@ -617,6 +691,12 @@ GncDateImpl::GncDateImpl(const std::string str, const std::string fmt) :
if (iter == GncDate::c_formats.cend())
throw std::invalid_argument(N_("Unknown date format specifier passed as argument."));

if (iter->m_str_to_date)
{
m_greg = (*iter->m_str_to_date)(str);
return;
}

boost::regex r(iter->m_re);
boost::smatch what;
if(!boost::regex_search(str, what, r)) // regex didn't find a match
Expand Down
9 changes: 9 additions & 0 deletions libgnucash/engine/gnc-datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include <memory>
#include <string>
#include <vector>
#include <functional>
#include <optional>

#include <boost/date_time/gregorian/gregorian.hpp>

typedef struct
{
Expand Down Expand Up @@ -172,6 +176,8 @@ class GncDateTime
* GncDate::c_formats class variable and work with those.
*/

using StringToDate = std::function<boost::gregorian::date(const std::string&)>;

class GncDateFormat
{
public:
Expand All @@ -182,13 +188,16 @@ class GncDateFormat
*/
GncDateFormat (const char* fmt, const char* re) :
m_fmt(fmt), m_re(re) {}
GncDateFormat (const char* fmt, StringToDate str_to_date) :
m_fmt(fmt), m_str_to_date(str_to_date) {}
/** A string representing the format. */
const std::string m_fmt;
private:
/** Regular expression associated with the format string. This is to and
* only be used internally by the gnc-datetime code.
*/
const std::string m_re;
std::optional<StringToDate> m_str_to_date;

friend class GncDateImpl;
};
Expand Down
26 changes: 26 additions & 0 deletions libgnucash/engine/test/gtest-gnc-datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ TEST(gnc_date_constructors, test_str_format_constructor)
{ "m-d", "6'8", curr_year, 6, 8},
{ "m-d", "0801", curr_year, 8, 1},

{ "UK date", "01/08/2013", 2013, 8, 1},
{ "UK date", "01/8/2013", 2013, 8, 1},
{ "UK date", "1/08/2013", 2013, 8, 1},
{ "UK date", "1/8/2013", 2013, 8, 1},
{ "UK date", "1 Aug 2013", 2013, 8, 1},
{ "UK date", "1 Sep 2013", 2013, 9, 1},
{ "UK date", "1 September 2013", 2013, 9, 1},
{ "UK date", "04/11/2009", 2009, 11, 4},
{ "UK date", "12.3.1985", 1985, 3, 12},

{ "US date", "08-01-2013", 2013, 8, 1},
{ "US date", "8-01-2013", 2013, 8, 1},
{ "US date", "08-1-2013", 2013, 8, 1},
{ "US date", "8-1-2013", 2013, 8, 1},
{ "US date", "11/04/2009", 2009, 11, 4},
{ "US date", "3.12.1985", 1985, 3, 12},
{ "US date", "November 4, 2009", 2009, 11, 4},
{ "US date", "Nov 4, 2009", 2009, 11, 4},

{ "ISO date", "2013-08-01", 2013, 8, 1},
{ "ISO date", "2013-08-1", 2013, 8, 1},
{ "ISO date", "2013-8-01", 2013, 8, 1},
{ "ISO date", "2013-8-1", 2013, 8, 1},
{ "ISO date", "2009/11/04", 2009, 11, 4},
{ "ISO date", "1985.3.12", 1985, 3, 12},

// ambiguous date formats
// current parser doesn't know how to disambiguate
// and hence refuses to parse
Expand Down

0 comments on commit aef7dfd

Please sign in to comment.