Inconsistent timezone validation behavior
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.8k
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
The [Timezone docs](https://day.js.org/docs/en/timezone/timezone) offer two different ways of creating a DayJS instance with Timezone information. The two different styles have very different behaviors when given an "invalid" timezone ID.
For example, this gives you a DayJS instance that represents the current day rather than the date you pass in. It just quietly swallows the exception:
```
dayjs("12-12-2023", "bogus")
```
On the other hand, this gives you a RangeError:
```
dayjs("12-12-2023").tz("bogus")
```
I think the main issue is with that first style, because you have no idea there is a problem. It would be very handy if there were a way to test the validity of a timezone ID. The timezone may be coming from a backend API, so it may not be known ahead of time. I ran into an issue because the Google Timezone API that our backend uses provided a timezone ID of `Pacific/Vancouver`, which apparently is not a valid IANA timezone. It should have been `America/Vancouver`. This caused our web app to crash due to an unexpected RangeError.
**Expected behavior**
If the two styles of creating a DayJS instance are supposed to be equivalent then they should have identical behavior when given the same inputs. Also, there should ideally be some way to test the validity (according to DayJS) of a timezone ID before we use it.
**Information**
- Day.js Version: latest
- OS: any
- Browser: any
- Time zone: any invalid (non-IANA) timezone
Contributor guide
Research direction
Start with the Timezone documentation and reproduce both examples using the invalid timezone IDs shown in the issue. Compare how the two creation styles handle invalid input, then define a consistent behavior and a way to check timezone validity before use; done means the behavior is predictable and the documented examples agree.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- localization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100