srawlins / timezone

Time zone database and time zone aware DateTime object for Dart.
BSD 2-Clause "Simplified" License
102 stars 53 forks source link

Incorrectly timezoned if created with a specific value #148

Open rafaellop opened 1 year ago

rafaellop commented 1 year ago

The hour is incorrectly timezoned if a date is instantiated by now() or by a value. For example I have this function:

  tz.TZDateTime _nextInstanceOfHour({required int hour}) {
    final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
    tz.TZDateTime scheduledDate = tz.TZDateTime(tz.local, now.year, now.month, now.day, hour);
   ...
}

The now variable is correctly set according to my timezone while it seems that the second variable called scheduledDate doesn't consider the tz.local at all. Example with some numbers:

For the local time of e.g. 2022-11-21 10:59 the now variable is set to 2022-11-21 9:59 However the scheduledDate with the hour parameter set to e.g. 11 is just left as 2022-11-21 11:00 when it should be 2022-11-21 10:00.

I'm on GMT+01:00.

rafaellop commented 1 year ago

Additional info:

The timezone is initialized like this:

tz.initializeTimeZones();

but if I add additional initialization code:

final String timeZoneName = await FlutterNativeTimezone.getLocalTimezone();
tz.setLocalLocation(tz.getLocation(timeZoneName));

Then both dates in the above example are the same, but not properly 1 hour shifted. I mean the first date is assigned to the now variable is not 2022-11-21 10:59 and the second remains the same 2022-11-21 10:00. In the effect if I use these datetimes in a local notification setup it is fired an hour later than expected.