Implement new static function wxDateTime::GetFirstWeekDay()

This function tries to determine the preferred first day of week to use in
calendars. The procedure for obtaining this information is highly
platform-dependent, and is not possible on all platforms; in that case
Sunday is used as the fallback value.

Implementations are included for MSW, OSX, and Linux.
This commit is contained in:
Lauri Nurmi 2017-07-09 12:12:04 +03:00
parent 6527607af7
commit 94c35b2cdd
7 changed files with 142 additions and 0 deletions

41
configure vendored
View File

@ -32987,6 +32987,47 @@ $as_echo "$wx_cv_struct_tm_has_gmtoff" >&6; }
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for _NL_TIME_FIRST_WEEKDAY in langinfo.h" >&5
$as_echo_n "checking for _NL_TIME_FIRST_WEEKDAY in langinfo.h... " >&6; }
if ${wx_cv_have_nl_time_first_weekday+:} false; then :
$as_echo_n "(cached) " >&6
else
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#define _GNU_SOURCE
#include <langinfo.h>
int
main ()
{
_NL_TIME_FIRST_WEEKDAY;
;
return 0;
}
_ACEOF
if ac_fn_c_try_compile "$LINENO"; then :
wx_cv_have_nl_time_first_weekday=yes
else
wx_cv_have_nl_time_first_weekday=no
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $wx_cv_have_nl_time_first_weekday" >&5
$as_echo "$wx_cv_have_nl_time_first_weekday" >&6; }
if test "$wx_cv_have_nl_time_first_weekday" = "yes"; then
$as_echo "#define HAVE_NL_TIME_FIRST_WEEKDAY 1" >>confdefs.h
fi
SAMPLES_SUBDIRS="$SAMPLES_SUBDIRS typetest"
fi

View File

@ -5949,6 +5949,28 @@ if test "$wxUSE_DATETIME" = "yes"; then
AC_DEFINE(WX_GMTOFF_IN_TM)
fi
AC_CACHE_CHECK([for _NL_TIME_FIRST_WEEKDAY in langinfo.h],
wx_cv_have_nl_time_first_weekday,
[
AC_TRY_COMPILE(
[
#define _GNU_SOURCE
#include <langinfo.h>
],
[
_NL_TIME_FIRST_WEEKDAY;
],
[
wx_cv_have_nl_time_first_weekday=yes
],
wx_cv_have_nl_time_first_weekday=no
)
])
if test "$wx_cv_have_nl_time_first_weekday" = "yes"; then
AC_DEFINE(HAVE_NL_TIME_FIRST_WEEKDAY)
fi
SAMPLES_SUBDIRS="$SAMPLES_SUBDIRS typetest"
fi

View File

@ -392,6 +392,9 @@ public:
// returns true if the given year is a leap year in the given calendar
static bool IsLeapYear(int year = Inv_Year, Calendar cal = Gregorian);
// acquires the first day of week based on locale and/or OS settings
static bool GetFirstWeekDay(WeekDay *firstDay);
// get the century (19 for 1999, 20 for 2000 and -5 for 492 BC)
static int GetCentury(int year);

View File

@ -1483,6 +1483,18 @@ public:
static bool IsDSTApplicable(int year = Inv_Year,
Country country = Country_Default);
/**
Acquires the first weekday of a week based on locale and/or OS settings.
If the information was not available, returns @c Sun.
@param firstDay
The address of a WeekDay variable to which the first weekday will be
assigned to.
@return If the first day could not be determined, returns false,
and firstDay is set to a fallback value.
@since 3.1.1
*/
static bool GetFirstWeekDay(WeekDay *firstDay);
/**
Returns @true if the @a year is a leap one in the specified calendar.
This functions supports Gregorian and Julian calendars.

View File

@ -935,6 +935,9 @@
/* struct tm doesn't always have the tm_gmtoff field, define this if it does */
#undef WX_GMTOFF_IN_TM
/* check if nl_langinfo() can be called with argument _NL_TIME_FIRST_WEEKDAY */
#undef HAVE_NL_TIME_FIRST_WEEKDAY
/* Define if you have poll(2) function */
#undef HAVE_POLL

View File

@ -541,6 +541,55 @@ bool wxDateTime::IsLeapYear(int year, wxDateTime::Calendar cal)
}
}
#ifdef __WINDOWS__
#include "wx/msw/registry.h"
/* static */
bool wxDateTime::GetFirstWeekDay(wxDateTime::WeekDay *firstDay)
{
wxCHECK_MSG( firstDay, false, wxS("output parameter must be non-null") );
wxRegKey key(wxRegKey::HKCU, "Control Panel\\International");
wxString val;
if ( key.Exists() && key.HasValue("iFirstDayOfWeek") )
{
key.QueryValue("iFirstDayOfWeek", val);
*firstDay = wxDateTime::WeekDay((wxAtoi(val) + 1) % 7);
return true;
}
else
{
*firstDay = wxDateTime::Sun;
return false;
}
}
#elif defined(__APPLE__)
// implementation in utils_base.mm
#elif defined(HAVE_NL_TIME_FIRST_WEEKDAY)
#include <langinfo.h>
/* static */
bool wxDateTime::GetFirstWeekDay(wxDateTime::WeekDay *firstDay)
{
wxCHECK_MSG( firstDay, false, wxS("output parameter must be non-null") );
*firstDay = wxDateTime::WeekDay((*nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % 7);
return true;
}
#else
/* static */
bool wxDateTime::GetFirstWeekDay(wxDateTime::WeekDay *firstDay)
{
wxCHECK_MSG( firstDay, false, wxS("output parameter must be non-null") );
*firstDay = wxDateTime::Sun;
return false;
}
#endif
/* static */
int wxDateTime::GetCentury(int year)
{

View File

@ -16,6 +16,7 @@
#ifndef WX_PRECOMP
#include "wx/intl.h"
#include "wx/app.h"
#include "wx/datetime.h"
#endif
#include "wx/apptrait.h"
@ -157,3 +158,14 @@ wxString wxGetOsDescription()
return osDesc;
}
/* static */
bool wxDateTime::GetFirstWeekDay(wxDateTime::WeekDay *firstDay)
{
wxCHECK_MSG( firstDay, false, wxS("output parameter must be non-null") );
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setLocale:[NSLocale autoupdatingCurrentLocale]];
*firstDay = wxDateTime::WeekDay(([calendar firstWeekday] - 1) % 7);
return true;
}