marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.01k stars 112 forks source link

`utcToZonedTime` should not be doing any conversion when `"UTC"` is passed in as the time zone #250

Open TheSonOfThomp opened 10 months ago

TheSonOfThomp commented 10 months ago

I was debugging, and wrote the following jest test:

describe('utcToZonedTime', () => {
  const date = new Date(Date.UTC(2023, 8, 1));

  test('converting to UTC has no effect', () => {
    const utc = utcToZonedTime(date, 'UTC');
    expect(utc.toISOString()).toEqual(date.toISOString());
  });
});

This test fails with the following result

  ● utcToZonedTime › converting to UTC has no effect

    expect(received).toEqual(expected) // deep equality

    Expected: "2023-09-01T00:00:00.000Z"
    Received: "2023-09-01T04:00:00.000Z"

My local time zone is -4:00, which makes sense if this is adding 4 to convert to "UTC". But shouldn't this function assume the provided date is in UTC already, and not the client time zone?

IB21-A commented 5 months ago

I just discovered this same issue while writing a test. It doesn't matter what format I pass it, even an ISO formatted string like '2023-09-01T00:00:00.000Z', it always assumes the provided date is my local time zone. I hope there are some plans to address this