srawlins/timezone

A bug with copying time?

AAverin opened this issue · 1 comments

I have stumbled into an odd issue with from method.

My expectation was that having some TZDateTime, copying it with copyWith() and passing to tz.TZDateTime.from would result in the same time, provided that tz.Locaiton is the same, which it is.
But instead there seems to be a "shift" in time by timezone.

What is the correct way to copy TZDateTime? copyWith method falls back to original DateTime.copyWith extension.

Here is a test that fails:

import 'package:flutter_test/flutter_test.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

test("can copy datetime", () {
    tz.initializeTimeZones();
    final location = tz.getLocation("UTC");
    final testTime = tz.TZDateTime(location, 2024, 5, 7, 13, 30, 30, 30, 40);

    expect(
        testTime.copyWith(), equals(DateTime(2024, 5, 7, 13, 30, 30, 30, 40)),
        reason: "copyWith didn't copy");

    //this is an unexpected failure
    expect(
        tz.TZDateTime.from(testTime.copyWith(), location),
        equals(equals(
          testTime,
        )),
        reason: "TZDateTime.from didn't match");

    //This also fails, which is what I would expect
    expect(
        testTime.copyWith(),
        equals(equals(
          testTime,
        )),
        reason: "TZDateTime.from didn't match");
}

I have switched to using regular constructor instead of from, so not changing some date but just creating a new TZDateTime with desired parameters.
Still, from behaviour looks a bit confusing