timezone-support icon indicating copy to clipboard operation
timezone-support copied to clipboard

listTimeZones() to filter the deprecated timezones

Open nikravi opened this issue 5 years ago • 3 comments

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

nikravi avatar Apr 18 '19 20:04 nikravi

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 avatar Jun 10 '19 23:06 prantlf

Hello @prantlf,

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

nilamsavani91 avatar Nov 06 '19 20:11 nilamsavani91

@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.)

prantlf avatar Dec 04 '22 20:12 prantlf