diff --git a/configure b/configure index 3b84460c04..648d42cafb 100755 --- a/configure +++ b/configure @@ -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 + +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 diff --git a/configure.in b/configure.in index 906d7eb615..dc964baae4 100644 --- a/configure.in +++ b/configure.in @@ -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 + ], + [ + _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 diff --git a/include/wx/datetime.h b/include/wx/datetime.h index b2a33ffd9a..e1d392a9bd 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -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); diff --git a/interface/wx/datetime.h b/interface/wx/datetime.h index 70c344c4e3..f12cb3a96e 100644 --- a/interface/wx/datetime.h +++ b/interface/wx/datetime.h @@ -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. diff --git a/setup.h.in b/setup.h.in index 01cf34efbe..a4da53aa94 100644 --- a/setup.h.in +++ b/setup.h.in @@ -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 diff --git a/src/common/datetime.cpp b/src/common/datetime.cpp index eddaf7977e..46c27b8eb7 100644 --- a/src/common/datetime.cpp +++ b/src/common/datetime.cpp @@ -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 + +/* 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) { diff --git a/src/osx/cocoa/utils_base.mm b/src/osx/cocoa/utils_base.mm index 37734f7464..e9d1d87518 100644 --- a/src/osx/cocoa/utils_base.mm +++ b/src/osx/cocoa/utils_base.mm @@ -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; +}