Do nothing when converting wxDateTime to/from local time zone

In particular, do not (unexpectedly) adjust time by the DST.

Closes #16585.

See #10445.
This commit is contained in:
Vadim Zeitlin
2017-11-29 23:15:57 +01:00
parent 543c522cb8
commit c7c30504c8
3 changed files with 17 additions and 23 deletions

View File

@@ -1257,10 +1257,7 @@ public:
If @a noDST is @true, no DST adjustments will be made. If @a noDST is @true, no DST adjustments will be made.
Notice using wxDateTime::Local for @a tz parameter doesn't really make If @a tz parameter is wxDateTime::Local, no adjustment is performed.
sense and may result in unexpected results as it will return a
different object when DST is in use and @a noDST has its default value
of @false.
@return The date adjusted by the different between the given and the @return The date adjusted by the different between the given and the
local time zones. local time zones.
@@ -1297,9 +1294,7 @@ public:
If @a noDST is @true, no DST adjustments will be made. If @a noDST is @true, no DST adjustments will be made.
Notice that, as with FromTimezone(), using wxDateTime::Local as @a tz If @a tz parameter is wxDateTime::Local, no adjustment is performed.
doesn't really make sense and may return a different object when DST is
in effect and @a noDST is @false.
@return The date adjusted by the different between the local and the @return The date adjusted by the different between the local and the
given time zones. given time zones.

View File

@@ -2094,11 +2094,11 @@ wxDateTime& wxDateTime::MakeTimezone(const TimeZone& tz, bool noDST)
{ {
long secDiff = wxGetTimeZone() + tz.GetOffset(); long secDiff = wxGetTimeZone() + tz.GetOffset();
// We are converting from the local time, but local time zone does not // We are converting from the local time to some other time zone, but local
// include the DST offset (as it varies depending on the date), so we have // time zone does not include the DST offset (as it varies depending on the
// to handle DST manually, unless a special flag inhibiting this was // date), so we have to handle DST manually, unless a special flag
// specified. // inhibiting this was specified.
if ( !noDST && (IsDST() == 1) ) if ( !noDST && (IsDST() == 1) && !tz.IsLocal() )
{ {
secDiff -= DST_OFFSET; secDiff -= DST_OFFSET;
} }
@@ -2111,7 +2111,7 @@ wxDateTime& wxDateTime::MakeFromTimezone(const TimeZone& tz, bool noDST)
long secDiff = wxGetTimeZone() + tz.GetOffset(); long secDiff = wxGetTimeZone() + tz.GetOffset();
// See comment in MakeTimezone() above, the logic here is exactly the same. // See comment in MakeTimezone() above, the logic here is exactly the same.
if ( !noDST && (IsDST() == 1) ) if ( !noDST && (IsDST() == 1) && !tz.IsLocal() )
{ {
secDiff -= DST_OFFSET; secDiff -= DST_OFFSET;
} }

View File

@@ -1565,21 +1565,20 @@ void DateTimeTestCase::TestTranslateFromUnicodeFormat()
void DateTimeTestCase::TestConvToFromLocalTZ() void DateTimeTestCase::TestConvToFromLocalTZ()
{ {
// Choose a date when the DST is on in many time zones: in this case, // Choose a date when the DST is on in many time zones and verify that
// converting to/from local TZ does modify the object because it // converting from/to local time zone still doesn't modify time in this
// adds/subtracts DST to/from it, so to get the expected results we need to // case as this used to be broken.
// explicitly disable DST support in these functions.
wxDateTime dt(18, wxDateTime::Apr, 2017, 19); wxDateTime dt(18, wxDateTime::Apr, 2017, 19);
CPPUNIT_ASSERT_EQUAL( dt.FromTimezone(wxDateTime::Local, true), dt ); CHECK( dt.FromTimezone(wxDateTime::Local) == dt );
CPPUNIT_ASSERT_EQUAL( dt.ToTimezone(wxDateTime::Local, true), dt ); CHECK( dt.ToTimezone(wxDateTime::Local) == dt );
// And another one when it is off: in this case, there is no need to pass // For a date when the DST is not used, this always worked, but still
// "true" as "noDST" argument to these functions. // verify that it continues to.
dt = wxDateTime(18, wxDateTime::Jan, 2018, 19); dt = wxDateTime(18, wxDateTime::Jan, 2018, 19);
CPPUNIT_ASSERT_EQUAL( dt.FromTimezone(wxDateTime::Local), dt ); CHECK( dt.FromTimezone(wxDateTime::Local) == dt );
CPPUNIT_ASSERT_EQUAL( dt.ToTimezone(wxDateTime::Local), dt ); CHECK( dt.ToTimezone(wxDateTime::Local) == dt );
} }
static void DoTestSetFunctionsOnDST(const wxDateTime &orig) static void DoTestSetFunctionsOnDST(const wxDateTime &orig)