[gnc-date][API] find locale's start of week using ICU.

gnc_start_of_week

* ICU has a mature C++ api, so prefer that one in our C++ code
* Use PERR instead of fprintf for consistent reporting
* Add the ICU specific linker flags to the test case
This commit is contained in:
Geert Janssens 2019-08-14 22:35:58 +08:00 committed by Christopher Lam
parent bdbb06b722
commit ec6602adf9
3 changed files with 31 additions and 0 deletions

View File

@ -48,6 +48,7 @@ extern "C"
} }
#include <cinttypes> #include <cinttypes>
#include <unicode/calendar.h>
#include "gnc-date.h" #include "gnc-date.h"
#include "gnc-date-p.h" #include "gnc-date-p.h"
@ -201,6 +202,30 @@ gnc_gmtime (const time64 *secs)
} }
gint
gnc_start_of_week (void)
{
/* icu's day of week is 1 based. Using 0 here to mean unset or error while setting */
static int cached_result = 0;
if (!cached_result)
{
UErrorCode err = U_ZERO_ERROR;
auto cal = icu::Calendar::createInstance (err);
if (!cal)
{
PERR("ICU error: %s\n", u_errorName (err));
return 0;
}
/* 1 for sunday, 2 for monday, etc. */
cached_result = cal->getFirstDayOfWeek (err);
delete cal;
}
return cached_result;
}
time64 time64
gnc_mktime (struct tm* time) gnc_mktime (struct tm* time)
{ {

View File

@ -185,6 +185,11 @@ struct tm* gnc_localtime_r (const time64 *secs, struct tm* time);
*/ */
struct tm* gnc_gmtime (const time64 *secs); struct tm* gnc_gmtime (const time64 *secs);
/** \brief returns an integer corresponding to locale start of week
* \return An integer 1=Sunday, 2=Monday etc. If error, return 0.
*/
gint gnc_start_of_week (void);
/** \brief calculate seconds from the epoch given a time struct /** \brief calculate seconds from the epoch given a time struct
* \param time: A struct tm* containing the date-time information. * \param time: A struct tm* containing the date-time information.
* The time is understood to be in the current local time zone. * The time is understood to be in the current local time zone.

View File

@ -99,6 +99,7 @@ set(gtest_qof_LIBS
${GOBJECT_LDFLAGS} ${GOBJECT_LDFLAGS}
${GMODULE_LDFLAGS} ${GMODULE_LDFLAGS}
${GTHREAD_LDFLAGS} ${GTHREAD_LDFLAGS}
${ICU4C_I18N_LDFLAGS}
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${GTEST_LIB}) ${GTEST_LIB})