question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

keepLocalTime applies day light savings

See original GitHub issue

Describe the bug I have recently begun using Luxon and I’m really enjoying it so far. Apologies if I have misunderstood the purpose of the keepLocalTime option. As I understood it, it should return a local time with a different offset but the same date and time. But what I am seeing is day light savings being applied.

To Reproduce

const ny = DateTime.fromISO('2021-03-28T23:59:00.000Z')
  .setZone('America/New_York', { keepLocalTime: true })

ny.toFormat('DDDD HH:mm')
// "Monday, 29 March 2021 00:59"  (Actual - Unexpected)
// "Monday, 28 March 2021 23:59" (Expected)

const ny2 = luxon.DateTime.fromISO('2021-02-28T23:59:00.000Z')
  .setZone('America/New_York', { keepLocalTime: true })

ny2.toFormat('DDDD HH:mm')
// "Sunday, 28 February 2021 23:59" (Expected)

Actual vs Expected behavior I would have expected ny.toFormat('DDDD HH:mm') to have given me "Sunday, 28 March 2021 23:59" but instead it appears to apply DST and prints "Monday, 29 March 2021 00:59"

Desktop:

  • OS: Linux Kubuntu
  • Browser: Firefox 94.0
  • Luxon version: 2.1.1
  • Your timezone: Europe/London

Again apologies if I have misunderstood the purpose of the keepLocalTime and this is actually intentional behaviour.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
End-Scommented, Nov 16, 2021

That makes a lot of sense and will help me resolve my issue. Thank you for taking the time to explain it.

1reaction
icambroncommented, Nov 16, 2021

Oh, I see. No, this is not a bug. What’s happening is this:

// parse a UTC date string and convert to system time (which for you is BST)
const aLocalDt =  DateTime.fromISO('2021-03-28T23:59:00.000Z');

aLocalDt.toISO() // pretty sure this produces 00:59+1:00 for you

// convert to US EDT but holding the 00:59 constant
aLocalDt.setZone("America/New_York", { keepLocalTime: true });

In other words, the DST shift comes from your local zone before the setZone() has ever been called.

What you meant was to keep the offset from the string. You can do that one of a few ways:

  1. Explicitly keep the DateTime in UTC, like DateTime.fromISO(s, { zone: "UTC"})
  2. Accept the offset from the string as not just the zone to interpret the string in, but also keep the resulting DateTime in: DateTime.fromISO(s, { setZone: true })
  3. Switch to UTC explicitly: DateTime.fromISO(s).setZone("utc").setZone("America/New_York", { keepLocalTime: true })
  4. Run your whole environment in UTC instead of Europe/London
Read more comments on GitHub >

github_iconTop Results From Across the Web

Python daylight savings time - Stack Overflow
Using time.localtime() , you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect...
Read more >
Time Zones and Resolving Ambiguity - Temporal documentation
When Daylight Saving Time (DST) starts or if a country moves to another time zone, then local clocks will instantly change. Exact time...
Read more >
How to Handle Time Zones using DateTime and Luxon
Raise your hand if you've ever had issues dealing with time zones, or even if you've asked, "How do I convert a Date...
Read more >
Daylight Saving Time | State Legislation
This page reviews current state legislation for Daylight Saving Time. ... The daylight saving time (DST) period in the U.S. begins each year ......
Read more >
When React Native Date Pickers and Daylight Savings Collide
This time, however, we do change the moment in time the timestamp represents, using the keepLocalTime: true option. The new DateTime will look...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found