From c92f9e0a179866c61a3cb136c8c872c23ef1e6c8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 6 Oct 2019 17:08:53 +0200 Subject: [PATCH] Document that wxDateTime::UNow() returns time in local time zone This is its actual behaviour and it's the right thing to do, as it's consistent with Now() -- even though the documentation wrongly stated otherwise (since 324ab5e2dbfcae3582d9405819b46fef5e7c0e59). Also add a unit test checking that UNow() == Now(), except for the milliseconds. See #14148. Closes #18524. Closes https://github.com/wxWidgets/wxWidgets/pull/1594 --- interface/wx/datetime.h | 8 ++++---- tests/datetime/datetimetest.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/interface/wx/datetime.h b/interface/wx/datetime.h index 68f8630713..1ee65c549c 100644 --- a/interface/wx/datetime.h +++ b/interface/wx/datetime.h @@ -1527,7 +1527,7 @@ public: static bool IsWestEuropeanCountry(Country country = Country_Default); /** - Returns the object corresponding to the current time. + Returns the object corresponding to the current time in local time zone. Example: @@ -1572,11 +1572,11 @@ public: static wxDateTime Today(); /** - Returns the object corresponding to the current UTC time including the + Returns the object corresponding to the current time including the milliseconds. - Notice that unlike Now(), this method creates a wxDateTime object - corresponding to UTC, not local, time. + Like Now(), this method creates the wxDateTime object corresponding to + the current moment in local time. @see Now(), wxGetUTCTimeMillis() */ diff --git a/tests/datetime/datetimetest.cpp b/tests/datetime/datetimetest.cpp index 14df03153a..22b4f8dfcd 100644 --- a/tests/datetime/datetimetest.cpp +++ b/tests/datetime/datetimetest.cpp @@ -1701,4 +1701,34 @@ TEST_CASE("wxDateTime-BST-bugs", "[datetime][dst][BST][.]") } } +TEST_CASE("wxDateTime::UNow", "[datetime][now][unow]") +{ + const wxDateTime now = wxDateTime::Now(); + const wxDateTime unow = wxDateTime::UNow(); + + CHECK( now.GetYear() == unow.GetYear() ); + CHECK( now.GetMonth() == unow.GetMonth() ); + CHECK( now.GetDay() == unow.GetDay() ); + CHECK( now.GetHour() == unow.GetHour() ); + CHECK( now.GetMinute() == unow.GetMinute() ); + CHECK( now.GetSecond() == unow.GetSecond() ); + + CHECK( now.GetMillisecond() == 0 ); + + // Just checking unow.GetMillisecond() == 0 would fail once per 1000 test + // runs on average, which is certainly not a lot, but still try to avoid + // such spurious failures. + bool gotMS = false; + for ( int i = 0; i < 3; ++i ) + { + if ( wxDateTime::UNow().GetMillisecond() != 0 ) + { + gotMS = true; + break; + } + } + + CHECK( gotMS ); +} + #endif // wxUSE_DATETIME