Socket
Socket
Sign inDemoInstall

date-fns-tz

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

date-fns-tz - npm Package Compare versions

Comparing version 1.1.7 to 1.2.0

_lib/tzPattern/index.js

19

_lib/tzParseTimezone/index.js

@@ -23,4 +23,9 @@ "use strict";

var token;
var absoluteOffset; // Z
var absoluteOffset; // Empty string
if (timezoneString === '') {
return 0;
} // Z
token = patterns.timezoneZ.exec(timezoneString);

@@ -71,3 +76,3 @@

return 0;
return NaN;
}

@@ -93,3 +98,3 @@

var o2 = calcOffset(new Date(utcGuess), timezoneString); // If so, offset didn't change and we're done
var o2 = calcOffset(new Date(utcGuess), timezoneString); // If so, offset didn't change, and we're done

@@ -107,3 +112,3 @@ if (offset === o2) {

return o2;
} // If it's different, we're in a hole time. The offset has changed, but the we don't adjust the time
} // If it's different, we're in a hole time. The offset has changed, but we don't adjust the time

@@ -115,7 +120,3 @@

function validateTimezone(hours, minutes) {
if (minutes != null && (minutes < 0 || minutes > 59)) {
return false;
}
return true;
return !(minutes != null && (minutes < 0 || minutes > 59));
}

@@ -122,0 +123,0 @@

@@ -18,2 +18,7 @@ import tzTokenizeDate from '../tzTokenizeDate/index.js'

// Empty string
if (timezoneString === '') {
return 0
}
// Z

@@ -66,3 +71,3 @@ token = patterns.timezoneZ.exec(timezoneString)

return 0
return NaN
}

@@ -104,3 +109,3 @@

// If so, offset didn't change and we're done
// If so, offset didn't change, and we're done
if (offset === o2) {

@@ -119,3 +124,3 @@ return offset

// If it's different, we're in a hole time. The offset has changed, but the we don't adjust the time
// If it's different, we're in a hole time. The offset has changed, but we don't adjust the time
return Math.max(o2, o3)

@@ -125,7 +130,3 @@ }

function validateTimezone(hours, minutes) {
if (minutes != null && (minutes < 0 || minutes > 59)) {
return false
}
return true
return !(minutes != null && (minutes < 0 || minutes > 59))
}

@@ -132,0 +133,0 @@

@@ -11,3 +11,3 @@ import tzIntlTimeZoneName from '../../_lib/tzIntlTimeZoneName'

var timezoneOffset = options.timeZone
? tzParseTimezone(options.timeZone, originalDate) / MILLISECONDS_IN_MINUTE
? tzParseTimezone(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()

@@ -45,3 +45,3 @@

var timezoneOffset = options.timeZone
? tzParseTimezone(options.timeZone, originalDate) / MILLISECONDS_IN_MINUTE
? tzParseTimezone(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()

@@ -75,3 +75,3 @@

var timezoneOffset = options.timeZone
? tzParseTimezone(options.timeZone, originalDate) / MILLISECONDS_IN_MINUTE
? tzParseTimezone(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE
: originalDate.getTimezoneOffset()

@@ -78,0 +78,0 @@

// This file is generated automatically by `scripts/build/indices.js`. Please, don't change it.
export { default as format } from './format/index.js'
export { default as formatInTimeZone } from './formatInTimeZone/index.js'
export { default as formatInTimeZoneWithOptions } from './formatInTimeZoneWithOptions/index.js'
export { default as formatWithOptions } from './formatWithOptions/index.js'

@@ -5,0 +7,0 @@ export { default as getTimezoneOffset } from './getTimezoneOffset/index.js'

// This file is generated automatically by `scripts/build/indices.js`. Please, don't change it.
export { default as format } from './format/index.js'
export { default as formatInTimeZone } from './formatInTimeZone/index.js'
export { default as getTimezoneOffset } from './getTimezoneOffset/index.js'

@@ -5,0 +6,0 @@ export { default as toDate } from './toDate/index.js'

import toInteger from 'date-fns/esm/_lib/toInteger/index.js'
import getTimezoneOffsetInMilliseconds from 'date-fns/esm/_lib/getTimezoneOffsetInMilliseconds/index.js'
import tzParseTimezone from '../_lib/tzParseTimezone'
import tzPattern from '../_lib/tzPattern'

@@ -10,5 +11,5 @@ var MILLISECONDS_IN_HOUR = 3600000

var patterns = {
dateTimeDelimeter: /[T ]/,
dateTimePattern: /^([0-9W+-]+)(T| )(.*)/,
datePattern: /^([0-9W+-]+)(.*)/,
plainTime: /:/,
timeZoneDelimeter: /[Z ]/i,

@@ -40,4 +41,4 @@ // year tokens

// timezone tokens (to identify the presence of a tz)
timezone: /([Z+-].*| UTC|(?:[a-zA-Z]+\/[a-zA-Z_]+(?:\/[a-zA-Z_]+)?))$/,
// time zone tokens (to identify the presence of a tz)
timeZone: tzPattern,
}

@@ -150,4 +151,4 @@

if (dateStrings.timezone || options.timeZone) {
offset = tzParseTimezone(dateStrings.timezone || options.timeZone, new Date(timestamp + time))
if (dateStrings.timeZone || options.timeZone) {
offset = tzParseTimezone(dateStrings.timeZone || options.timeZone, new Date(timestamp + time))
if (isNaN(offset)) {

@@ -157,3 +158,3 @@ return new Date(NaN)

} else {
// get offset accurate to hour in timezones that change offset
// get offset accurate to hour in time zones that change offset
offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time))

@@ -171,23 +172,24 @@ offset = getTimezoneOffsetInMilliseconds(new Date(timestamp + time + offset))

var dateStrings = {}
var array = dateString.split(patterns.dateTimeDelimeter)
var parts = patterns.dateTimePattern.exec(dateString)
var timeString
if (patterns.plainTime.test(array[0])) {
dateStrings.date = null
timeString = array[0]
if (!parts) {
parts = patterns.datePattern.exec(dateString)
if (parts) {
dateStrings.date = parts[1]
timeString = parts[2]
} else {
dateStrings.date = null
timeString = dateString
}
} else {
dateStrings.date = array[0]
timeString = array[1]
dateStrings.timezone = array[2]
if (patterns.timeZoneDelimeter.test(dateStrings.date)) {
dateStrings.date = dateString.split(patterns.timeZoneDelimeter)[0]
timeString = dateString.substr(dateStrings.date.length, dateString.length)
}
dateStrings.date = parts[1]
timeString = parts[3]
}
if (timeString) {
var token = patterns.timezone.exec(timeString)
var token = patterns.timeZone.exec(timeString)
if (token) {
dateStrings.time = timeString.replace(token[1], '')
dateStrings.timezone = token[1]
dateStrings.timeZone = token[1].trim()
} else {

@@ -194,0 +196,0 @@ dateStrings.time = timeString

@@ -31,7 +31,7 @@ import tzParseTimezone from '../_lib/tzParseTimezone'

var offsetMilliseconds = tzParseTimezone(timeZone, date, true) || 0
var offsetMilliseconds = tzParseTimezone(timeZone, date, true)
var d = new Date(date.getTime() - offsetMilliseconds)
var zonedTime = new Date(
return new Date(
d.getUTCFullYear(),

@@ -45,4 +45,2 @@ d.getUTCMonth(),

)
return zonedTime
}
import cloneObject from 'date-fns/esm/_lib/cloneObject'
import format from 'date-fns/esm/format'
import toDate from '../toDate'
import tzPattern from '../_lib/tzPattern'
import tzParseTimezone from '../_lib/tzParseTimezone'

@@ -30,8 +31,23 @@ /**

export default function zonedTimeToUtc(date, timeZone, options) {
if (date instanceof Date) {
date = format(date, "yyyy-MM-dd'T'HH:mm:ss.SSS")
if (typeof date === 'string' && !date.match(tzPattern)) {
var extendedOptions = cloneObject(options)
extendedOptions.timeZone = timeZone
return toDate(date, extendedOptions)
}
var extendedOptions = cloneObject(options)
extendedOptions.timeZone = timeZone
return toDate(date, extendedOptions)
var d = toDate(date, options)
var utc = Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours(),
d.getMinutes(),
d.getSeconds(),
d.getMilliseconds()
)
var offsetMilliseconds = tzParseTimezone(timeZone, new Date(utc))
return new Date(utc + offsetMilliseconds)
}

@@ -19,3 +19,3 @@ "use strict";

var originalDate = options._originalDate || date;
var timezoneOffset = options.timeZone ? (0, _tzParseTimezone.default)(options.timeZone, originalDate) / MILLISECONDS_IN_MINUTE : originalDate.getTimezoneOffset();
var timezoneOffset = options.timeZone ? (0, _tzParseTimezone.default)(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE : originalDate.getTimezoneOffset();

@@ -52,3 +52,3 @@ if (timezoneOffset === 0) {

var originalDate = options._originalDate || date;
var timezoneOffset = options.timeZone ? (0, _tzParseTimezone.default)(options.timeZone, originalDate) / MILLISECONDS_IN_MINUTE : originalDate.getTimezoneOffset();
var timezoneOffset = options.timeZone ? (0, _tzParseTimezone.default)(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE : originalDate.getTimezoneOffset();

@@ -81,3 +81,3 @@ switch (token) {

var originalDate = options._originalDate || date;
var timezoneOffset = options.timeZone ? (0, _tzParseTimezone.default)(options.timeZone, originalDate) / MILLISECONDS_IN_MINUTE : originalDate.getTimezoneOffset();
var timezoneOffset = options.timeZone ? (0, _tzParseTimezone.default)(options.timeZone, originalDate, true) / MILLISECONDS_IN_MINUTE : originalDate.getTimezoneOffset();

@@ -84,0 +84,0 @@ switch (token) {

@@ -6,2 +6,4 @@ "use strict";

format: require('./format/index.js'),
formatInTimeZone: require('./formatInTimeZone/index.js'),
formatInTimeZoneWithOptions: require('./formatInTimeZoneWithOptions/index.js'),
formatWithOptions: require('./formatWithOptions/index.js'),

@@ -8,0 +10,0 @@ getTimezoneOffset: require('./getTimezoneOffset/index.js'),

@@ -6,2 +6,3 @@ "use strict";

format: require('./format/index.js'),
formatInTimeZone: require('./formatInTimeZone/index.js'),
getTimezoneOffset: require('./getTimezoneOffset/index.js'),

@@ -8,0 +9,0 @@ toDate: require('./toDate/index.js'),

{
"name": "date-fns-tz",
"version": "1.1.7",
"version": "1.2.0",
"sideEffects": false,

@@ -49,3 +49,3 @@ "description": "Time zone support for date-fns v2 with the Intl API",

"peerDependencies": {
"date-fns": ">=2.0.0-alpha.13"
"date-fns": ">=2.0.0"
},

@@ -52,0 +52,0 @@ "devDependencies": {

# date-fns-tz
Time zone support for [date-fns](https://date-fns.org/) v2.0.0 using the
[Intl API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl). By using
the browser API no time zone data needs to be included in code bundles. Modern browsers all support the
[Intl API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl).
By using the browser API no time zone data needs to be included in code bundles. Modern browsers
and Node.js all support the
[necessary features](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat#Browser_compatibility),
and for those that don't a [polyfill](https://github.com/yahoo/date-time-format-timezone) can be used.
If you do not wish to use a polyfill the time zone option can still be used, but only with
time zone offsets such as '-0200' or '+04:00' and not IANA time zone names.
If you do not wish to use a polyfill the time zones can still be specified as offsets
such as '-0200' or '+04:00', but not IANA time zone names.
**Note:** `date-fns` is a peer dependency of this library.
If you find this library useful, why not
<a href="https://www.buymeacoffee.com/marnusw" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
## Table of Contents
- [Overview](#overview)
- [Time Zone Helpers](#time-zone-helpers)
- [Date and time zone formatting](#date-and-time-zone-formatting)
- [`formatInTimeZone`](#formataszonedtime) - Formats a date in the provided time zone,
regardless of the system time zone
- [Time zone offset helpers](#time-zone-offset-helpers)
- [`zonedTimeToUtc`](#zonedtimetoutc) - Given a date and any time zone, returns a `Date` with the equivalent UTC time
- [`utcToZonedTime`](#utctozonedtime) - Get a date/time representing local time in a given time zone from the UTC date
- [`getTimezoneOffset`](#gettimezoneoffset) - Gets the offset in milliseconds between the time zone and UTC time
- [Time Zone Formatting](#time-zone-formatting)
- [`format`](#format) - Extends `date-fns/format` with full time zone support
- [`toDate`](#todate) - Can be used to create a zoned Date from a string containing an offset or IANA time zone
- [Usage with Node.js](#usage-with-nodejs)
- [Low-level formatting helpers](#low-level-formatting-helpers)
- [`format`](#format) - Extends `date-fns/format` with support for all time zone tokens,
including `z..zzzz`
- [`toDate`](#todate) - Can be used to parse a `Date` from a date string representing time in
any time zone

@@ -28,4 +39,5 @@ ## Overview

are displayed in a user's local time in the browser. The difficulty comes when working with another
time zone's local time, other than the current system's, like on a Node server or when showing the time
of an event in a specific time zone, like an event in LA at 8pm PST regardless of where a user resides.
time zone's local time, one other than the current system's, like on a Node server or when showing
the time of an event in a specific time zone, like an event in LA at 8pm PST regardless of where
a user resides.

@@ -37,23 +49,68 @@ In this case there are two relevant pieces of information:

Libraries like Moment and Luxon, which provide their own date time classes, manage these timestamp and time
zone values internally. Since `date-fns` always returns a plain JS Date, which implicitly has the current
system's time zone, helper functions are provided for handling common time zone related use cases.
Libraries like Moment and Luxon, which provide their own date-time classes, manage these
timestamp and time zone values internally. Since `date-fns` always returns a plain JS Date,
which implicitly has the current system's time zone, helper functions are provided for handling
common time zone related use cases.
## Time Zone Helpers
## Date and time zone formatting
To discuss the usage of the time zone helpers let's assume we're writing a system where administrators set
up events which will start at a specific time in the venue's local time, and this local time should be
shown when accessing the site from anywhere in the world.
### `formatInTimeZone`
This function takes a `Date` instance in the system's local time or an ISO8601 string, and
an IANA time zone name or offset string. It then formats this date in the target time zone
regardless of the system's local time zone.
It supports the same format tokens as `date-fns/format`, and adds full support for:
- The `z..zzz` Unicode tokens: _short specific non-location format_, e.g. `EST`
- The `zzzz` Unicode token: _long specific non-location format_, e.g. `Eastern Standard Time`
Unlike `date-fns/format`, the `z..zzzz`, `x..xxxxx`, `X..XXXXX` and `O..OOO` tokens will all
print the formatted value of the provided time zone rather than the system time zone.
An invalid date or time zone input will result in an `Invalid Date` passed to `date-fns/format`,
which will throw a `RangeError`.
For most use cases this is the only function from this library you will need.
```javascript
import { formatInTimeZone } from 'date-fns-tz'
const date = new Date('2014-10-25T10:46:20Z')
formatInTimeZone(date, 'America/New_York', 'yyyy-MM-dd HH:mm:ssXXX') // 2014-10-25 06:46:20-04:00
formatInTimeZone(date, 'America/New_York', 'yyyy-MM-dd HH:mm:ss zzz') // 2014-10-25 06:46:20 EST
formatInTimeZone(date, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzz') // 2014-10-25 10:46:20 GMT+2
// The time zone name is generated by the Intl API which works best when a locale is also provided
import enGB from 'date-fns/locale/en-GB'
formatInTimeZone(parisDate, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzz', { locale: enGB })
// 2014-10-25 10:46:20 CEST
formatInTimeZone(parisDate, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzzz', { locale: enGB })
// 2014-10-25 10:46:20 Central European Summer Time
```
## Time zone offset helpers
These functions are useful when you are not formatting a date yourself, but passing it to
third-party code such as a date picker library alongside an input for selecting a time zone.
To discuss the usage of the time zone helpers let's assume we're writing a system where
administrators set up events which will start at a specific time in the venue's local time, and
this local time should be shown when accessing the site from anywhere in the world.
### `zonedTimeToUtc`
**Given a date and any time zone, returns a `Date` with the equivalent UTC time**
Given a date and any time zone, returns a `Date` with the equivalent UTC time.
An invalid date string or time zone will result in an `Invalid Date`.
```js
```ts
zonedTimeToUtc(date: Date|Number|String, timeZone: String): Date
```
Say a user is asked to input the date/time and time zone of an event. A date/time picker will typically
return a Date instance with the chosen date, in the user's local time zone, and a select input might
provide the actual IANA time zone name.
Say a user is asked to input the date/time and time zone of an event. A date/time picker will
typically return a Date instance with the chosen date, in the user's local time zone, and a
select input might provide the actual IANA time zone name.

@@ -75,3 +132,4 @@ In order to work with this info effectively it is necessary to find the equivalent UTC time:

**Get a date/time in the local time of any time zone from UTC time**
Returns a `Date` which will format as the local time of any time zone from a specific UTC time.
An invalid date string or time zone will result in an `Invalid Date`.

@@ -82,5 +140,5 @@ ```js

Say the server provided a UTC date/time and a time zone which should be used as initial values for the above form.
The date/time picker will take a Date input which will be in the user's local time zone, but the date value
must be that of the target time zone.
Say the server provided a UTC date/time and a time zone which should be used as initial values
for the above form. The date/time picker will take a Date input which will be in the user's
local time zone, but the date value must be that of the target time zone.

@@ -100,3 +158,3 @@ ```javascript

**Gets the offset in milliseconds between the time zone and UTC time**
Returns the offset in milliseconds between the time zone and UTC time.

@@ -114,2 +172,4 @@ ```js

For invalid time zones, `NaN` is returned.
```javascript

@@ -128,7 +188,8 @@ import { getTimezoneOffset } from 'date-fns-tz'

## Time Zone Formatting
## Low-level formatting helpers
### `format`
The `format` function exported from this library extends `date-fns/format` with full time zone support for:
The `format` function exported from this library is used under the hood by `formatInTimeZone`
and extends `date-fns/format` with full time zone support for:

@@ -138,16 +199,23 @@ - The `z..zzz` Unicode tokens: _short specific non-location format_

When using those tokens with `date-fns/format` it falls back to GMT timezones, and always uses the local
system timezone. For example `zzz` in New York would return `GMT-4` instead of the desired `EST`, whereas
this extended `format` function will return the latter.
When using those tokens with `date-fns/format` it falls back to the GMT time zone format, and
always uses the current system's local time zone. For example `zzz` in New York will always return
`GMT-4` instead of the desired `EST`, and `zzz` in Paris `GMT+2` instead of `CEST`, making the
time zone tokens somewhat irrelevant. This extended `format` function returns the proper
specific non-location format, e.g. `EST` or `Eastern Standard Time`, and that of the target time
zone (if provided, see below) rather than the system time zone.
To format a date to a string showing time for a specific time zone, which can be different from the system
time zone, the `format` function can be combined with `utcToZonedTime` as shown in the example below. _To
clarify, the `format` function will never change the underlying date, it must be changed to a zoned time
before passing it to `format`._
Since a JavaScript `Date` instance cannot convey the time zone information to the `format` function
it is necessary to pass the `timeZone` value as an option on the third argument of `format`.
Since a zoned time `Date` instance cannot convey the time zone information to the `format` function it is
necessary to pass the same `timeZone` value as an option on the third argument of `format`. When using this
option the `z..zzzz`, `x..xxxxx`, `X..XXXXX` and `O..OOO` tokens will all print the provided time zone rather
than the system time zone.
Similar to `date-fns/format`, when an invalid date or time zone is used a `RangeError` is thrown.
To format a date showing time for a specific time zone other than the system time zone, the
`format` function can be combined with `utcToZonedTime`. This is what `formatInTimeZone` does
internally. _To clarify, the `format` function will never change the underlying date, it must be
changed to a zoned time before passing it to `format`._
In most cases there is no need to use `format` rather than `formatInTimeZone`. The only time
this makes sense is when `utcToZonedTime` has been applied to a date once, and you want to
format it multiple times to different outputs.
```javascript

@@ -157,7 +225,5 @@ import { format, utcToZonedTime } from 'date-fns-tz'

const date = new Date('2014-10-25T10:46:20Z')
const nyTimeZone = 'America/New_York'
const parisTimeZone = 'Europe/Paris'
const nyDate = utcToZonedTime(date, nyTimeZone)
const parisDate = utcToZonedTime(date, parisTimeZone)
const nyDate = utcToZonedTime(date, 'America/New_York')
const parisDate = utcToZonedTime(date, 'Europe/Paris')

@@ -185,5 +251,7 @@ format(nyDate, 'yyyy-MM-dd HH:mm:ssXXX', { timeZone: 'America/New_York' }) // 2014-10-25 06:46:20-04:00

The `toDate` function can be used to create a zoned Date from a string containing an offset or IANA
time zone, or by providing the `timeZone` option.
The `toDate` function can be used to parse a `Date` from a string containing a date and time
representing time in any time zone by providing an IANA time zone name on the `timeZone` option.
An invalid date string or time zone will result in an `Invalid Date`.
```javascript

@@ -193,6 +261,7 @@ import { toDate, format } from 'date-fns-tz'

// Offsets in the date string work as usual and take precedence
const parisDate = toDate('2014-10-25T13:46:20+02:00')
format(parisDate, 'yyyy-MM-dd HH:mm:ssZ', { timeZone: 'Europe/Paris' }) // 2014-10-25 13:46:20+02:00
const parsedDate = toDate('2014-10-25T13:46:20+04:00')
const parisDate = utcToZonedTime(parsedDate, 'Europe/Paris')
format(parisDate, 'yyyy-MM-dd HH:mm:ssxxx', { timeZone: 'Europe/Paris' }) // 2014-10-25 11:46:20+02:00
// Since toDate simply clones a Date instance timeZone option is effectively ignored in this case
// Since toDate simply clones a Date instance, the timeZone option is effectively ignored in this case
const date = new Date('2014-10-25T13:46:20Z')

@@ -203,19 +272,13 @@ const clonedDate = toDate(date, { timeZone: 'Europe/Paris' })

// When there is no offset in the date string the timeZone property is used
const bangkokDate = toDate('2014-10-25T13:46:20', { timeZone: 'Asia/Bangkok' })
format(bangkokDate, 'yyyy-MM-dd HH:mm:ssZ', { timeZone: 'Asia/Bangkok' }) // 2014-10-25 13:46:20+07:00
const nyDate = toDate('2014-10-25T13:46:20 America/New_York')
format(nyDate, 'yyyy-MM-dd HH:mm:ssZ', { timeZone: 'America/New_York' }) // 2014-10-25 13:46:20-04:00
const parsedDate = toDate('2014-10-25T13:46:20', { timeZone: 'Asia/Bangkok' })
const bangkokDate = utcToZonedTime(parsedDate, 'Asia/Bangkok')
format(bangkokDate, 'yyyy-MM-dd HH:mm:ssxxx', { timeZone: 'Asia/Bangkok' }) // 2014-10-25 13:46:20+07:00
```
**Note:** Since the Intl API does not provide a way to parse long or short time zone names the `parse`
function cannot be supported using this approach.
## Usage with Node.js
Node.js supports the `Intl` API. From v13 Node.js ships with full ICU data included in the binary, however
the current LTS version 12.14 is still built with the `small-icu` flag and only contains ICU data for the
`en-US` locale. To use this library with Node.js 12 and any locale other than `en-US` it should be run
with
[ICU data provided at runtime](https://nodejs.org/docs/latest-v12.x/api/intl.html#intl_providing_icu_data_at_runtime).
Node.js supports the `Intl` API and ships with full ICU data included in the binary from v13,
i.e. this library will just work.
Node.js v12, which reaches end of life on 30 April 2022, requires running with
[full ICU data provided at runtime](https://nodejs.org/docs/latest-v12.x/api/intl.html#intl_providing_icu_data_at_runtime).

@@ -222,0 +285,0 @@ ## Credit

@@ -14,2 +14,4 @@ "use strict";

var _tzPattern = _interopRequireDefault(require("../_lib/tzPattern"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -21,5 +23,5 @@

var patterns = {
dateTimeDelimeter: /[T ]/,
dateTimePattern: /^([0-9W+-]+)(T| )(.*)/,
datePattern: /^([0-9W+-]+)(.*)/,
plainTime: /:/,
timeZoneDelimeter: /[Z ]/i,
// year tokens

@@ -45,4 +47,4 @@ YY: /^(\d{2})$/,

HHMMSS: /^(\d{2}):?(\d{2}):?(\d{2}([.,]\d*)?)$/,
// timezone tokens (to identify the presence of a tz)
timezone: /([Z+-].*| UTC|(?:[a-zA-Z]+\/[a-zA-Z_]+(?:\/[a-zA-Z_]+)?))$/
// time zone tokens (to identify the presence of a tz)
timeZone: _tzPattern.default
};

@@ -140,4 +142,4 @@ /**

if (dateStrings.timezone || options.timeZone) {
offset = (0, _tzParseTimezone.default)(dateStrings.timezone || options.timeZone, new Date(timestamp + time));
if (dateStrings.timeZone || options.timeZone) {
offset = (0, _tzParseTimezone.default)(dateStrings.timeZone || options.timeZone, new Date(timestamp + time));

@@ -148,3 +150,3 @@ if (isNaN(offset)) {

} else {
// get offset accurate to hour in timezones that change offset
// get offset accurate to hour in time zones that change offset
offset = (0, _index2.default)(new Date(timestamp + time));

@@ -162,25 +164,26 @@ offset = (0, _index2.default)(new Date(timestamp + time + offset));

var dateStrings = {};
var array = dateString.split(patterns.dateTimeDelimeter);
var parts = patterns.dateTimePattern.exec(dateString);
var timeString;
if (patterns.plainTime.test(array[0])) {
dateStrings.date = null;
timeString = array[0];
} else {
dateStrings.date = array[0];
timeString = array[1];
dateStrings.timezone = array[2];
if (!parts) {
parts = patterns.datePattern.exec(dateString);
if (patterns.timeZoneDelimeter.test(dateStrings.date)) {
dateStrings.date = dateString.split(patterns.timeZoneDelimeter)[0];
timeString = dateString.substr(dateStrings.date.length, dateString.length);
if (parts) {
dateStrings.date = parts[1];
timeString = parts[2];
} else {
dateStrings.date = null;
timeString = dateString;
}
} else {
dateStrings.date = parts[1];
timeString = parts[3];
}
if (timeString) {
var token = patterns.timezone.exec(timeString);
var token = patterns.timeZone.exec(timeString);
if (token) {
dateStrings.time = timeString.replace(token[1], '');
dateStrings.timezone = token[1];
dateStrings.timeZone = token[1].trim();
} else {

@@ -187,0 +190,0 @@ dateStrings.time = timeString;

@@ -52,2 +52,5 @@ // This file is generated automatically by `scripts/build/typings.js`. Please, don't change it.

function formatInTimeZone(date: Date | number, timeZone: string, options?: OptionsWithTZ): string
namespace formatInTimeZone {}
function getTimezoneOffset(timeZone: string, date?: Date | number): number

@@ -79,2 +82,7 @@ namespace getTimezoneOffset {}

declare module 'date-fns-tz/formatInTimeZone' {
import { formatInTimeZone } from 'date-fns-tz'
export = formatInTimeZone
}
declare module 'date-fns-tz/getTimezoneOffset' {

@@ -105,2 +113,7 @@ import { getTimezoneOffset } from 'date-fns-tz'

declare module 'date-fns-tz/formatInTimeZone/index' {
import { formatInTimeZone } from 'date-fns-tz'
export = formatInTimeZone
}
declare module 'date-fns-tz/getTimezoneOffset/index' {

@@ -131,2 +144,7 @@ import { getTimezoneOffset } from 'date-fns-tz'

declare module 'date-fns-tz/formatInTimeZone/index.js' {
import { formatInTimeZone } from 'date-fns-tz'
export = formatInTimeZone
}
declare module 'date-fns-tz/getTimezoneOffset/index.js' {

@@ -160,2 +178,8 @@ import { getTimezoneOffset } from 'date-fns-tz'

const formatInTimeZone: CurriedFn2<string, Date | number, string>
namespace formatInTimeZone {}
const formatInTimeZoneWithOptions: CurriedFn3<OptionsWithTZ, string, Date | number, string>
namespace formatInTimeZoneWithOptions {}
const formatWithOptions: CurriedFn3<OptionsWithTZ, string, Date | string | number, string>

@@ -191,2 +215,12 @@ namespace formatWithOptions {}

declare module 'date-fns-tz/fp/formatInTimeZone' {
import { formatInTimeZone } from 'date-fns-tz/fp'
export = formatInTimeZone
}
declare module 'date-fns-tz/fp/formatInTimeZoneWithOptions' {
import { formatInTimeZoneWithOptions } from 'date-fns-tz/fp'
export = formatInTimeZoneWithOptions
}
declare module 'date-fns-tz/fp/formatWithOptions' {

@@ -237,2 +271,12 @@ import { formatWithOptions } from 'date-fns-tz/fp'

declare module 'date-fns-tz/fp/formatInTimeZone/index' {
import { formatInTimeZone } from 'date-fns-tz/fp'
export = formatInTimeZone
}
declare module 'date-fns-tz/fp/formatInTimeZoneWithOptions/index' {
import { formatInTimeZoneWithOptions } from 'date-fns-tz/fp'
export = formatInTimeZoneWithOptions
}
declare module 'date-fns-tz/fp/formatWithOptions/index' {

@@ -283,2 +327,12 @@ import { formatWithOptions } from 'date-fns-tz/fp'

declare module 'date-fns-tz/fp/formatInTimeZone/index.js' {
import { formatInTimeZone } from 'date-fns-tz/fp'
export = formatInTimeZone
}
declare module 'date-fns-tz/fp/formatInTimeZoneWithOptions/index.js' {
import { formatInTimeZoneWithOptions } from 'date-fns-tz/fp'
export = formatInTimeZoneWithOptions
}
declare module 'date-fns-tz/fp/formatWithOptions/index.js' {

@@ -332,2 +386,5 @@ import { formatWithOptions } from 'date-fns-tz/fp'

function formatInTimeZone(date: Date | number, timeZone: string, options?: OptionsWithTZ): string
namespace formatInTimeZone {}
function getTimezoneOffset(timeZone: string, date?: Date | number): number

@@ -359,2 +416,7 @@ namespace getTimezoneOffset {}

declare module 'date-fns-tz/esm/formatInTimeZone' {
import { formatInTimeZone } from 'date-fns-tz/esm'
export default formatInTimeZone
}
declare module 'date-fns-tz/esm/getTimezoneOffset' {

@@ -385,2 +447,7 @@ import { getTimezoneOffset } from 'date-fns-tz/esm'

declare module 'date-fns-tz/esm/formatInTimeZone/index' {
import { formatInTimeZone } from 'date-fns-tz/esm'
export default formatInTimeZone
}
declare module 'date-fns-tz/esm/getTimezoneOffset/index' {

@@ -411,2 +478,7 @@ import { getTimezoneOffset } from 'date-fns-tz/esm'

declare module 'date-fns-tz/esm/formatInTimeZone/index.js' {
import { formatInTimeZone } from 'date-fns-tz/esm'
export default formatInTimeZone
}
declare module 'date-fns-tz/esm/getTimezoneOffset/index.js' {

@@ -440,2 +512,8 @@ import { getTimezoneOffset } from 'date-fns-tz/esm'

const formatInTimeZone: CurriedFn2<string, Date | number, string>
namespace formatInTimeZone {}
const formatInTimeZoneWithOptions: CurriedFn3<OptionsWithTZ, string, Date | number, string>
namespace formatInTimeZoneWithOptions {}
const formatWithOptions: CurriedFn3<OptionsWithTZ, string, Date | string | number, string>

@@ -471,2 +549,12 @@ namespace formatWithOptions {}

declare module 'date-fns-tz/esm/fp/formatInTimeZone' {
import { formatInTimeZone } from 'date-fns-tz/esm/fp'
export default formatInTimeZone
}
declare module 'date-fns-tz/esm/fp/formatInTimeZoneWithOptions' {
import { formatInTimeZoneWithOptions } from 'date-fns-tz/esm/fp'
export default formatInTimeZoneWithOptions
}
declare module 'date-fns-tz/esm/fp/formatWithOptions' {

@@ -517,2 +605,12 @@ import { formatWithOptions } from 'date-fns-tz/esm/fp'

declare module 'date-fns-tz/esm/fp/formatInTimeZone/index' {
import { formatInTimeZone } from 'date-fns-tz/esm/fp'
export default formatInTimeZone
}
declare module 'date-fns-tz/esm/fp/formatInTimeZoneWithOptions/index' {
import { formatInTimeZoneWithOptions } from 'date-fns-tz/esm/fp'
export default formatInTimeZoneWithOptions
}
declare module 'date-fns-tz/esm/fp/formatWithOptions/index' {

@@ -563,2 +661,12 @@ import { formatWithOptions } from 'date-fns-tz/esm/fp'

declare module 'date-fns-tz/esm/fp/formatInTimeZone/index.js' {
import { formatInTimeZone } from 'date-fns-tz/esm/fp'
export default formatInTimeZone
}
declare module 'date-fns-tz/esm/fp/formatInTimeZoneWithOptions/index.js' {
import { formatInTimeZoneWithOptions } from 'date-fns-tz/esm/fp'
export default formatInTimeZoneWithOptions
}
declare module 'date-fns-tz/esm/fp/formatWithOptions/index.js' {

@@ -565,0 +673,0 @@ import { formatWithOptions } from 'date-fns-tz/esm/fp'

@@ -40,8 +40,7 @@ "use strict";

var date = (0, _toDate.default)(dirtyDate, options);
var offsetMilliseconds = (0, _tzParseTimezone.default)(timeZone, date, true) || 0;
var offsetMilliseconds = (0, _tzParseTimezone.default)(timeZone, date, true);
var d = new Date(date.getTime() - offsetMilliseconds);
var zonedTime = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
return zonedTime;
return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), d.getUTCMilliseconds());
}
module.exports = exports.default;

@@ -10,6 +10,8 @@ "use strict";

var _format = _interopRequireDefault(require("date-fns/format"));
var _toDate = _interopRequireDefault(require("../toDate"));
var _tzPattern = _interopRequireDefault(require("../_lib/tzPattern"));
var _tzParseTimezone = _interopRequireDefault(require("../_lib/tzParseTimezone"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -42,11 +44,14 @@

function zonedTimeToUtc(date, timeZone, options) {
if (date instanceof Date) {
date = (0, _format.default)(date, "yyyy-MM-dd'T'HH:mm:ss.SSS");
if (typeof date === 'string' && !date.match(_tzPattern.default)) {
var extendedOptions = (0, _cloneObject.default)(options);
extendedOptions.timeZone = timeZone;
return (0, _toDate.default)(date, extendedOptions);
}
var extendedOptions = (0, _cloneObject.default)(options);
extendedOptions.timeZone = timeZone;
return (0, _toDate.default)(date, extendedOptions);
var d = (0, _toDate.default)(date, options);
var utc = Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
var offsetMilliseconds = (0, _tzParseTimezone.default)(timeZone, new Date(utc));
return new Date(utc + offsetMilliseconds);
}
module.exports = exports.default;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc