DEV Community

Franz Wong
Franz Wong

Posted on

Time change in daylight saving

I live in a country not following daylight saving time. By writing this article, it helps me to understand what happens when daylight saving time starts and ends.

You can't stick with UTC all the time

Ideally, we should stick with UTC whenever possible. But most of us live with local time. For example, trading hour of New York stock exchange opens at 09:30 New York local time (or Eastern time). 09:30 ET is 13:30 UTC in summer or 14:30 UTC in winter. If you schedule a task with UTC time to do something when market opens, then you need to be aware of time change.

We will use Eastern time as example in this article.

When daylight saving time starts

In March every year, we change from standard time to daylight saving time. We do a "clock forward", 1 hour is skipped in local time.

SOURCE_TIMEZONE=UTC && TARGET_TIMEZONE=America/New_York

# Change 2021-03-14 06:00 UTC to ET
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-03-14 06:00"''
# Sun Mar 14 01:00:00 EST 2021

# Change 2021-03-14 06:59 UTC to ET
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-03-14 06:59"''
# Sun Mar 14 01:59:00 EST 2021

# Change 2021-03-14 07:00 UTC to ET
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-03-14 07:00"''
# Sun Mar 14 03:00:00 EDT 2021
Enter fullscreen mode Exit fullscreen mode

From the above, 02:00 is skipped.

After 01:59 (EST), it jumps ahead to 03:00 (EDT). The timezone changes from EST (Eastern standard time) to EDT (Eastern daylight time).

So what if we try to format a time 2021-03-14 02:00 ET to UTC?

SOURCE_TIMEZONE=America/New_York && TARGET_TIMEZONE=UTC

# Change 2021-03-14 02:00 ET to UTC
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-03-14 02:00"''
# date: invalid date 'TZ="America/New_York" 2021-03-14 02:00'
Enter fullscreen mode Exit fullscreen mode

We can't do that or the behaviour is unknown.

When daylight saving time ends

In November every year, we change from daylight saving time to standard time. We do a "clock backward", 1 hour is repeated in local time.

SOURCE_TIMEZONE=UTC && TARGET_TIMEZONE=America/New_York

# Change 2021-11-07 05:00 UTC to ET
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-11-07 05:00"''
# Sun Nov  7 01:00:00 EDT 2021

# Change 2021-11-07 05:59 UTC to ET
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-11-07 05:59"''
# Sun Nov  7 01:59:00 EDT 2021

# Change 2021-11-07 06:00 UTC to ET
TZ=$TARGET_TIMEZONE date --date='TZ="'"$SOURCE_TIMEZONE"'" '"2021-11-07 06:00"''
# Sun Nov  7 01:00:00 EST 2021
Enter fullscreen mode Exit fullscreen mode

From the above, 01:00 is repeated.

After 01:59 (EDT), it falls back to 01:00 (EST). The timezone changes from EDT (Eastern daylight time) to EST (Eastern standard time).

Hope this help to maintain timezone sensitive application. I worked with a few data APIs (e.g. Reuters), some of their documentations contain mistakes with timezone.

Top comments (0)