Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listTimeZones() to filter the deprecated timezones #10

Open
nikravi opened this issue Apr 18, 2019 · 3 comments
Open

listTimeZones() to filter the deprecated timezones #10

nikravi opened this issue Apr 18, 2019 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@nikravi
Copy link

nikravi commented Apr 18, 2019

Is it possible to add a filter on the returned list, so that the deprecated timezones are not included?

@prantlf prantlf added the enhancement New feature or request label Jun 10, 2019
@prantlf prantlf self-assigned this Jun 10, 2019
@prantlf
Copy link
Owner

prantlf commented Jun 10, 2019

Actually, the array of time zone names returned by listTimeZones contains only valid, non-deprecated canonical time zones. Exported as zones from the data* modules.

Deprecated time zones are not exposed by any public function. They are exported as links from the data* modules. They are used only internally by findTimeZone.

Do you see any time zone returned by listTimeZones, which is not supposed to be there? (I released the version 2.0.2 today with the time zone database upgraded to 2019a. It might have such changes in comparison with the previously used 2018g.)

@prantlf prantlf added question Further information is requested and removed enhancement New feature or request labels Jun 10, 2019
@nilamsavani91
Copy link

Hello @prantlf,

I want to display timezone with their offset. Can you tell me how can I achieve that?

@prantlf
Copy link
Owner

prantlf commented Dec 4, 2022

@nilamsavani91, a time zone has no single offset. The offset will be known, as soon as you say what country and date you want to know the offset for. The reason is that the rules for daylight-saving changes are different in different countries and even for a single country they change in time.

For example, a time zone "Europe/Prague", on January 1, 2022 is the offset to add to the date for getting UTC -60 minutes:

❯ echo "import { findTimeZone, getZonedTime } from 'timezone-support'
const czechZone = findTimeZone('Europe/Prague')
const utcDate = new Date(Date.UTC(2022, 0, 1, 12, 0))
const czechTime = getZonedTime(utcDate, czechZone)
console.log('UTC: 2022-01-01 12:00')
console.log('Local:', czechTime)" | node --input-type=module
UTC: 2022-01-01 12:00
Local: {
  year: 2022,
  month: 1,
  day: 1,
  dayOfWeek: 6,
  hours: 13,
  minutes: 0,
  seconds: 0,
  milliseconds: 0,
  zone: { abbreviation: 'CET', offset: -60 }
}

And on June 1, 2022 is the offset to add to the date for getting UTC -120 minutes:

❯ echo "import { findTimeZone, getZonedTime } from 'timezone-support'
const czechZone = findTimeZone('Europe/Prague')
const utcDate = new Date(Date.UTC(2022, 5, 1, 12, 0))
const czechTime = getZonedTime(utcDate, czechZone)
console.log('UTC: 2022-06-01 12:00')
console.log('Local:', czechTime)" | node --input-type=module

UTC: 2022-06-01 12:00
Local: {
  year: 2022,
  month: 6,
  day: 1,
  dayOfWeek: 3,
  hours: 14,
  minutes: 0,
  seconds: 0,
  milliseconds: 0,
  zone: { abbreviation: 'CEST', offset: -120 }
}

Generally, if you want to get a time zone offset, get the time zone and the day and after you compute the time in the particular time zone by getZonedTime, read the offset from the zone.offset property:

import { findTimeZone, getZonedTime } from 'timezone-support'
const czechZone = findTimeZone(the name of your time zone)
const date = the day that you want to know the offset for
const time = getZonedTime(utcDate, czechZone)
const { offset } = time.zone

You will get the count of minutes, which you need to add to the local time to get the corresponding UTC time. If you want to get the offset, which you usually see in printed dates, multiply it with -1 and convert to the format hh:mm. (Printed time understands the offset as a time duration to add to an UTC time to get the corresponding local time.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants