Reputation: 18034
In JavaScript (running in the browser), I have two time zones in the form continent/location
. How do I decide if they are the same time zone?
Note that just checking the current offset does not work. The two time zones could be different zones that just happen to have the same offset today. I could check the offsets for every day of the year, but that doesn't sound feasible.
Examples:
Europe/Oslo
and Europe/Brussels
are the same (both central European time / central European summer time).Europe/Oslo
and Europe/London
are not the same.Upvotes: 1
Views: 410
Reputation: 90
Those identifiers in the format continent/location
come from IANA database. And in this database, each identifier represents a list of all the offsets that the respective region had (and has, and will have) during history.
Europe/Oslo
and Europe/Brussels
might be "the same" today, but their history of offsets are not identical - actually, that's why IANA assigned a different ID for each one.
Before 1980, Oslo didn't have Daylight Saving Time, while Brussels had. So, in the 70's, they weren't "the same" - in the sense that they didn't use the same rules regarding offsets (not only the offsets used, but also the exact instant when DST changes happen). Only after 1980, Oslo started using the same rules of Brussels, so now they can be considered "the same".
When checking if 2 or more timezones are "equal", you must set a reference date, or an interval of dates to be considered. Because different IANA identifiers means that, at some point in history, there'll be a difference.
And don't forget that timezones might change all the time, because they are defined by governments - usually without a technical reason. So you can't guarantee future dates as well: what if politicians of Brussels or Oslo decide to change their country's rules (such as "we won't have DST from now on" or "the standard offset will now be X, and during DST will be Y", or "we'll keep DST, but it starts and ends at different dates", etc)?
If you want to use the zone's short name, such as "CET" and "CEST" (for "Central European Time" and "Central European Summer Time"), you can use momentjs library with momentjs timezone, and check the zone's short name using the abbr
method.
But then you have another problem: many of those short names are ambiguous (IST is used by India, Israel and Ireland, CST is used in China, Cuba and USA's Central Time, etc), so just checking them are not enough (as pointed in the comments). So, you must also check the offset using the utcOffset
method.
But note that these methods need a timestamp as a reference, because they check in the timezone's history what was the name and the offset used in that moment. So you'll always need to use a reference date (or an interval of dates) to know if 2 timezones are "the same".
Actually, it doesn't matter which API you use, you must always use some reference date(s) to know if the timezones being compared have the same rules/name/offset/whatever criteria you use.
CET is a comprehensive name that covers lots of countries. Not only that, but each country adopted it in a different date. And, as I said before, this is always subject to change in the future (some countries might change to CET/CEST, or decide to not use CEST, or change to another offset different from CET, or even change the dates when CEST starts and/or ends; anyway, this is an always-changing list - just because 2 zones are "the same" today, it doesn't mean they were always like this in the past, and there's no guarantee that they'll stay the same in the future).
So, depending on the date you check, 2 timezones can be the same or not. There's no way to check "date-independent equality".
Upvotes: 6