Giter VIP home page Giter VIP logo

timezone-support's Introduction

Time Zone Support

Latest version Dependency status Coverage Codacy Badge

Lightweight time zone listing and date converting. Intended for adding time zone support to high-level date libraries, but also for direct application usage.

  • Tiny code base - 4.6 KB minified, 1.7 KB gzipped. Do not pack unnecessary weight in your application.
  • Packed time zone data - 924 KB minified, 33.6 KB gzipped. Single time zones are unpacked on demand.
  • Smaller bundles of code with limited data - 1900-2050 (206 kB minified, 25.4 kB gzipped), 1970-2038 (141 kB minified, 15.8 kB gzipped) and 2012-2022 (31.3 KB minified, 8.25 kB gzipped).
  • Generated from the official time zone database version 2022f. Canonical time zone names, aliases, UTC offsets, and daylight-saving time changes.
  • ESM, UMD and CJS module formats provided.
  • Minimal interface for time zone lookup and conversions. Parsing, formatting and manipulating dates is usually the task for a higher-level date library.

Attention: exported identifiers in vanilla browser modules changed in the version 2.0.0. See the migration guide for more information.

Table of Contents

Synopsis

const {
  listTimeZones, findTimeZone, getZonedTime, getUnixTime
} = require('timezone-support')

// List canonical time zone names: [ 'Africa/Abidjan', ... ]
const timeZones = listTimeZones()

// Find a particular time zone: { name: 'Europe/Berlin', ... }
const berlin = findTimeZone('Europe/Berlin')

// Convert a date to a specific time zone: { year, month, day, dayOfWeek,
// hours, minutes, seconds, milliseconds, epoch, zone: { abbreviation, offset } }
const nativeDate = new Date()
const berlinTime = getZonedTime(nativeDate, berlin)

// Convert a time from a specific time zone: native Date object
const berlinTime = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0 }
const nativeDate = new Date(getUnixTime(berlinTime, berlin))

Installation and Getting Started

This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 14.8 or newer.

$ npm i timezone-support
$ pnpm i timezone-support
$ yarn add timezone-support

Functions are exposed as named exports from the package modules, for example:

const { findTimeZone, getZonedTime } = require('timezone-support')

You can read more about the module loading in other environments, like with ES6 or in web browsers. Usage scenarios demonstrate applications of this library in typical real-world scenarios. Design concepts explain the approach to time zone handling taken by tni library and types of values used ion the interface. Generating custom time zone data will allow you to save the overall package size by limiting the supported year span. Finally, the API reference lists all functions with a description of their functionality.

You can see complete sample applications too, which can help you start with integration of this library.

Library Integrations

  • Day.js - timeZone plugin supplies parsing from and formatting to an arbitrary time zone
  • date-fns - date-fns-timezone provides functions for parsing from and formatting to an arbitrary time zone and time zone conversions for the native Date object.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2018-2022 Ferdinand Prantl

Licensed under the MIT license.

timezone-support's People

Contributors

econdepe avatar lucifurtun avatar nuxlli avatar pascalhonegger avatar prantlf avatar superhawk610 avatar vlascik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

timezone-support's Issues

Bug in getUnixTime for DST switch

There seems to be a bug in getUnixTime. The unix timestamp is incorrectly generated just after the change to PDT.

I've validated the result and compared the behavior to moment-timezone. See test case below

const moment = require('moment-timezone');
const { findTimeZone, getUnixTime } = require('timezone-support');

const unix1 = getUnixTime(
  { year: 2018, month: 3, day: 11, hours: 5 },
  findTimeZone('America/Vancouver')
) / 1000;
console.log(unix1);
// => 1520773200
// => 1520773200 converts to Sunday March 11, 2018 06:00:00 (am) in time zone America/Vancouver (PDT)
// Reference: https://www.epochconverter.com/timezones?q=1520773200&tz=America%2FVancouver

const unix2 = moment.tz('2018-03-11T05:00:00', 'America/Vancouver').unix();
console.log(unix2);
// => 1520769600
// => 1520769600 converts to Sunday March 11, 2018 05:00:00 (am) in time zone America/Vancouver (PDT)
// Reference: https://www.epochconverter.com/timezones?q=1520769600&tz=America%2FVancouver

Would be great if this could be fixed. It has caused me some headaches as I'm trying to improve performance by migrating from moment-timezone to timezone-support.

Thank you very much for your consideration!

Weird bundle size in production

Hi.
In readme it's said:

Tiny code base - 4.6 KB minified, 1.7 KB gzipped...

In my project I see different picture:
image

I use it via date-fns-timezone: ^0.1.4

DST

Hi!

Does this library takes DST adjustments into consideration?

Take this case for example:

At 00:00 of 1/3/2024 in some timezone T, the clocks will move forward one hour. If I ask what is the Date corresponding to 00:30 of 1/3/2024 at timezone T, will the library say that this is not defined?

There is another case, which is when the clocks move backward one hour, in which the library should say that the conversion is ambiguous.

This is what happen with e.g. this C++ date library

What will happen in these cases with this library?

Thanks!

Include "Common" Time Zone Names

Would you accept a PR that includes the well-known time zone name and abbreviation in each zone as listed here: https://www.timeanddate.com/time/zones/?

convertToTimeZone(parsedDate, { timeZone: timeZone.name })

It would allow a caller to use the following format: convertToTimeZone(new Date(), { timeZone: 'Eastern Standard Time' })

Feature: subset of timezone data

moment-timezone has a 9KB, 2012-2022 subset version of the data, instead of full 25KB one. Having something similar here would be really useful.

How to generate new data?

Since the project seems to be abandoned, a fork would be needed to use the latest data. Creating this issue to compile information regarding data generation. Timezone info is stored in /util/data folder in json format, but it isn't clear to me how is it generated from the official IANA dataset. Any idea?

IE error occured.

Doesn't work in IE.
When i'm using findTimeZone i got an error because imported module not in ES5 format.

Could you check please ?

parseZonedTime() not working (not even in the documentation and example provided)

When trying to use the example below from the documentation in a create-react-app.
Issues:
-The example provided in the documentation is not working
-There is a parsing error?
**Provided is a picture of the error, and a code sandbox example **

const { parseZonedTime } = require('timezone-support/dist/parse-format')
const input = '2.9.2018 10:00 CEST+02:00'
const format = 'D.M YYYY H:mm:ss [z]Z'
const time = parseZonedTime(input, format)

Screen Shot 2019-06-03 at 11 15 11 PM

Please refer to the following code sandbox to take a look at the error.

https://codesandbox.io/s/notworking-mdsnh

attachEpoch uses defineProperty which makes it slow

I found this problem when profiling our application code. attachEpoch comes up quite clearly as at the top suspect for slow down. Interestingly when I looked at setTimeZone the zone property is added via assignment and it's only the epoch property that is added via defineProperty. Is it possible to change attachEpoch to add epoch via assignment?

Adding property through assignment is 2 orders of magnitude faster (see https://jsperf.com/object-defineproperty-vs-assignment/3)

Is this project abandoned?

There seems to be no changes since 1.5 years ago, no replies on issues or pull requests. I reached out to all contributors on email and didn't get any replies.

Interfaces are not exported alongside functions

type DateInput
interface TimeZoneInfo
interface TimeZoneOffset
interface Time
interface SetTimeZoneOptions

are all not exported alongside the functions like findTimeZone and setTimeZone

This leads to that you have to create your own interfaces that mirror them (problematic on version update when the interface changes).
Would love to create a PR to export the types and interfaces as well.

Let me know what you think

TS Typings on 1.6.0

in package.json

  "main": "dist/index.js",
  "typings": "dist/index.d.ts",

however typings seem to have moved to src/index.d.ts ? Or weren't published to dist/ for some reason

What is the best way to import `formatZonedTime`?

I'm in the process of optimizing the JavaScript bundle size for my company's application. The 'timezone-support' package seems to be significantly contributing to the size. In my codebase, there's a particular file where the following lines need refactoring:

const { getZonedTime, findTimeZone } = require('timezone-support');
const { formatZonedTime } = require('timezone-support/dist/parse-format');

Given that 'timezone-support' is now updated to version 3 or above, I'm looking for the most efficient method to import formatZonedTime without having to load the complete 1MB+ distribution of 'timezone-support'.

CST timezone

Most timezones don't seem to work other than (what I figured out after a short test) GMT, UTC, or EST but the timezone CST doesn't work and responds with Error: Unknown time zone "CST".. This occurs when trying to use the findTimeZone method. Is there any explanations as to why this is happening?

coveralls repo_token in repo

Hi, just browsing here randomly and noticed you have a private token visible in your public repo, in .coveralls.yml

I don't know if this is by (lazy) design, but it's bad security practice, so just letting you know ๐Ÿ˜„

Typings not picked when using ESM

When using "module": "node16" TypeScript is unable to find the typings for the package.

Could not find a declaration file for module 'timezone-support'. 'foo/node_modules/timezone-support/dist/index.mjs' implicitly has an 'any' type.

Since the file imported is dist/index.mjs, TypeScript looks for a dist/index.d.mts file which doesn't exist.

Smaller bundle for 2023-2033

Hello and Happy 2023 ๐Ÿฅณ

I noticed that there are smaller bundles made so that we don't pack too much data when using timezone-support.
The smallest one is 2012-2022. Do you plan to release a new one since 2022 is gone. (Could be 2023-2033 or something else) ?

I could create a PR, but before, I'd like to validate that it would make sense for timezone-support

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.