Daylight Saving Time, UTC Offset, US Timezones
Last week, 03/11/2022, some of our users reported that newly create booking in our studio booking app are shifted by one hour. Weirdly, there are no release in that week. After wasting some hours for debugging, i found that the problem was coming from Daylight Saving Time (DST).
In short, in the springtime you turn your clock forward one hour and in the autumn you turn your clock back an hour.
If you use utc-offset manually to calculate the time conversion, the offset value will be shifted on specific dates, here's for example
03/12/2022, UTC Offset for Los Angeles Timezone : -8 Hours
03/13/2022, UTC Offset for Los Angeles Timezone : -7 Hours
11/06/2022, UTC Offset for Los Angeles Timezone : -8 Hours
So, how to avoid that kind of bug?
1. Avoid using utc-offset to do conversion between different timezone.
If your application are written in javascript, use library like moment-timezone or luxon. That library will do the right timezone conversion for you
2. If you're forced to use utc-offset, calculate the utc offset based on selected datetime, not only current time
In my case, it was google card service datetime picker, we need to set utc-offset manually on the datetime picker to do timezone conversion
https://developers.google.com/apps-script/reference/card-service/date-time-picker#setTimeZoneOffsetInMins(Integer)
In that kind of scenario, instead of calculate the utc offset based on current time and current timezoneconst utcOffset = moment().utfOffset()
use the selected datetime, and selected timezone
const utfOffset = moment.tz(selectedDatetime, selectedTimezone).utcOffset());
That's all.
Comments