New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@vvo/tzdb

Package Overview
Dependencies
Maintainers
1
Versions
208
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vvo/tzdb - npm Package Compare versions

Comparing version 4.4.0 to 5.0.0

dist/getTimeZones.js

22

dist/formatTimeZone.js

@@ -11,26 +11,20 @@ "use strict";

function format({
name: timeZoneName,
alternativeName: alternativeTimeZoneName,
alternativeName,
mainCities,
rawOffsetInMinutes
rawOffsetInMinutes,
currentTimeOffsetInMinutes
}, {
useCurrentOffset = false
} = {}) {
const currentDate = _luxon.DateTime.fromObject({
locale: "en-US",
zone: timeZoneName
});
const offsetInHours = useCurrentOffset ? currentDate.toFormat("ZZ") // We could use something like https://github.com/mobz/get-timezone-offset to get the time zone offset so that we do not require luxon, but this seems like an abandonned project so for now we will rely on luxon
: getRawOffsetString(rawOffsetInMinutes);
return `${offsetInHours.padStart(6, "+")} ${alternativeTimeZoneName} - ${mainCities.join(", ")}`;
const offsetInHours = useCurrentOffset ? getOffsetString(currentTimeOffsetInMinutes) : getOffsetString(rawOffsetInMinutes);
return `${offsetInHours.padStart(6, "+")} ${alternativeName} - ${mainCities.join(", ")}`;
}
function getRawOffsetString(rawOffsetInMinutes) {
function getOffsetString(offsetInMinutes) {
const durationInHoursMinutes = _luxon.Duration.fromObject({
minutes: Math.abs(rawOffsetInMinutes)
minutes: Math.abs(offsetInMinutes)
}).toFormat("hh:mm");
return `${rawOffsetInMinutes >= 0 ? "+" : "-"}${durationInHoursMinutes}`;
return `${offsetInMinutes >= 0 ? "+" : "-"}${durationInHoursMinutes}`;
}
//# sourceMappingURL=formatTimeZone.js.map

@@ -6,28 +6,28 @@ "use strict";

});
Object.defineProperty(exports, "formatTimeZone", {
Object.defineProperty(exports, "rawTimeZones", {
enumerable: true,
get: function () {
return _formatTimeZone.default;
return _rawTimeZones.default;
}
});
Object.defineProperty(exports, "simplifiedTimeZones", {
Object.defineProperty(exports, "timeZonesNames", {
enumerable: true,
get: function () {
return _simplifiedTimeZones.default;
return _timeZonesNames.default;
}
});
Object.defineProperty(exports, "timeZonesNames", {
Object.defineProperty(exports, "getTimeZones", {
enumerable: true,
get: function () {
return _timeZonesNames.default;
return _getTimeZones.default;
}
});
var _formatTimeZone = _interopRequireDefault(require("./formatTimeZone.js"));
var _rawTimeZones = _interopRequireDefault(require("../raw-time-zones.json"));
var _simplifiedTimeZones = _interopRequireDefault(require("../simplified-time-zones.json"));
var _timeZonesNames = _interopRequireDefault(require("../time-zones-names.json"));
var _getTimeZones = _interopRequireDefault(require("./getTimeZones.js"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=index.js.map

@@ -1,20 +0,15 @@

import { DateTime, Duration } from "luxon";
import { Duration } from "luxon";
export default function format(
{
name: timeZoneName,
alternativeName: alternativeTimeZoneName,
alternativeName,
mainCities,
rawOffsetInMinutes,
currentTimeOffsetInMinutes,
},
{ useCurrentOffset = false } = {},
) {
const currentDate = DateTime.fromObject({
locale: "en-US",
zone: timeZoneName,
});
const offsetInHours = useCurrentOffset
? currentDate.toFormat("ZZ") // We could use something like https://github.com/mobz/get-timezone-offset to get the time zone offset so that we do not require luxon, but this seems like an abandonned project so for now we will rely on luxon
: getRawOffsetString(rawOffsetInMinutes);
? getOffsetString(currentTimeOffsetInMinutes)
: getOffsetString(rawOffsetInMinutes);

@@ -24,11 +19,11 @@ return `${offsetInHours.padStart(

"+",
)} ${alternativeTimeZoneName} - ${mainCities.join(", ")}`;
)} ${alternativeName} - ${mainCities.join(", ")}`;
}
function getRawOffsetString(rawOffsetInMinutes) {
function getOffsetString(offsetInMinutes) {
const durationInHoursMinutes = Duration.fromObject({
minutes: Math.abs(rawOffsetInMinutes),
minutes: Math.abs(offsetInMinutes),
}).toFormat("hh:mm");
return `${rawOffsetInMinutes >= 0 ? "+" : "-"}${durationInHoursMinutes}`;
return `${offsetInMinutes >= 0 ? "+" : "-"}${durationInHoursMinutes}`;
}

@@ -1,3 +0,3 @@

export { default as formatTimeZone } from "./formatTimeZone.js";
export { default as simplifiedTimeZones } from "../simplified-time-zones.json";
export { default as rawTimeZones } from "../raw-time-zones.json";
export { default as timeZonesNames } from "../time-zones-names.json";
export { default as getTimeZones } from "./getTimeZones.js";
{
"name": "@vvo/tzdb",
"version": "4.4.0",
"version": "5.0.0",
"description": "Simplified, grouped and always up to date list of time zones, with major cities",

@@ -11,2 +11,3 @@ "repository": {

"author": "Vincent Voyer <vincent@codeagain.com>",
"sideEffects": false,
"main": "dist/",

@@ -17,3 +18,3 @@ "files": [

"time-zones-names.json",
"simplified-time-zones.json",
"raw-time-zones.json",
"LICENSE",

@@ -74,3 +75,4 @@ "README.md"

"dependencies": {
"luxon": "1.24.1"
"luxon": "1.24.1",
"sort-on": "^4.1.0"
},

@@ -77,0 +79,0 @@ "devDependencies": {

@@ -7,2 +7,3 @@ # tzdb [![GitHub license](https://img.shields.io/github/license/vvo/tzdb?style=flat)](https://github.com/vvo/tzdb/blob/master/LICENSE) [![Tests](https://github.com/vvo/tzdb/workflows/CI/badge.svg)](https://github.com/vvo/tzdb/actions) [![npm version](https://badge.fury.io/js/%40vvo%2Ftzdb.svg)](https://www.npmjs.com/package/@vvo/tzdb)

- all existing raw IANA time zones names
- "raw" offsets along with current time offsets

@@ -13,43 +14,39 @@ The data and npm packages are **automatically updated** whenever there are changes to https://www.geonames.org/ which is generated from IANA databases.

## Available data
## NPM package
### [simplified-time-zones.json](./simplified-time-zones.json)
Installation:
This is most probably what you're looking for if you're trying to build a time zones selector in your application.
```bash
npm add @vvo/tzdb
```
Example data:
Usage:
```js
import { getTimeZones, rawTimeZones, timeZonesNames } from "@vvo/tzdb";
```
## API
### getTimeZones()
```js
const timeZones = getTimeZones();
```
This method returns an array of time zones objects:
```js
[
// ...
{
name: "America/Anchorage",
alternativeName: "Alaska Time",
countryName: "United States",
mainCities: ["Anchorage", "Juneau"],
formatted: "-09:00 Alaska Time - Anchorage, Juneau",
group: [
"America/Anchorage",
"America/Juneau",
"America/Metlakatla",
"America/Nome",
"America/Sitka",
"America/Yakutat",
],
},
{
name: "Pacific/Gambier",
alternativeName: "Gambier Time",
countryName: "French Polynesia",
mainCities: ["Gambier"],
formatted: "-09:00 Gambier Time - Gambier",
group: ["Pacific/Gambier"],
},
{
name: "America/Los_Angeles",
alternativeName: "Pacific Time",
group: ["America/Los_Angeles"],
countryName: "United States",
mainCities: ["Los Angeles", "San Diego"],
formatted: "-08:00 Pacific Time - Los Angeles, San Diego",
group: ["America/Los_Angeles"],
rawOffsetInMinutes: -480, // "raw" time zone offset, when there's no DST in place
rawFormat: "-08:00 Pacific Time - Los Angeles, San Diego",
currentTimeOffsetInMinutes: -420, // "current" time zone offset, this is why getTimeZones() is a method and not just an object: it can only work at runtime
currentTimeFormat: "-07:00 Pacific Time - Los Angeles, San Diego",
},

@@ -60,17 +57,29 @@ // ...

As you can see, we provide the time zone name and a pre-formatted version using more common offset names.
When relevant, time zones are grouped. The rules for grouping are:
Notes:
- if the time zones are in the same country
- if the DST or summer time offsets
- if the non DST, non summer time offsets are the same
- then we group the time zones
- the "main" time zone name (`name` attribute), is always the one from the most populated city
- Grouping: when two different time zones names are in the same country, same offset and dst rules then we merge them and select the time zone name from the biggest city
- We provide two cities when grouping happens, ranked by population
- We provide offset names ("Pacific Time") without dst and remove "Standard" and "Daylight"
- The offsets (-08:00) are always the "raw offsets", the ones when there's no summer time or daylight saving time in place for the time zone. We decided not to include the current date offset. Because then you would have to upgrade this library in your application as soon as it would be updated with the new "current offset". But we provide a function, `formatTimeZone` to compute the same formatted structure, with the current date offset, at runtime instead of buildtime
- This can be used to build a good enough (Google calendar like) select box of time zones, but it's your responsibility to handle dst and real offsets using [luxon](https://moment.github.io/luxon/) for example
Here's a grouping example:
### [time-zones-names.json](./time-zones-names.json)
```js
{
name: 'America/Dawson_Creek',
alternativeName: 'Mountain Time',
group: [ 'America/Creston', 'America/Dawson_Creek', 'America/Fort_Nelson' ],
countryName: 'Canada',
mainCities: [ 'Fort St. John', 'Creston' ],
rawOffsetInMinutes: -420,
rawFormat: '-07:00 Mountain Time - Fort St. John, Creston',
currentTimeOffsetInMinutes: -420,
currentTimeFormat: '-07:00 Mountain Time - Fort St. John, Creston'
}
```
This is the raw list of all IANA time zones ranked by alphabetical order.
### rawTimeZones
Example data:
This is an array of time zone objects without the current time information:

@@ -80,2 +89,26 @@ ```js

// ...
{
name: "America/Los_Angeles",
alternativeName: "Pacific Time",
group: ["America/Los_Angeles"],
countryName: "United States",
mainCities: ["Los Angeles", "San Diego"],
rawOffsetInMinutes: -480,
rawFormat: "-08:00 Pacific Time - Los Angeles, San Diego",
},
// ...
];
```
### timeZonesNames
This is an array of time zone names:
```js
[
// ...
"America/Juneau",
"America/Kentucky/Louisville",
"America/Kentucky/Monticello",
"America/Kralendijk",
"America/La_Paz",

@@ -90,2 +123,3 @@ "America/Lima",

"America/Martinique",
"America/Matamoros",
// ...

@@ -95,53 +129,17 @@ ];

## NPM package
## Notes
Installation:
- We provide two cities when grouping happens, ranked by population
- We provide alternative names ("Pacific Time" for "America/Los_Angeles") and remove "Standard", "Daylight" or "Summer" from them
- If you're using this to build a time zone selector and saving to a database then:
- make sure to save the `name` attribute (`America/Los_Angeles`) in your database
- when displaying the select with a default value from your database, either select the time zone name that matches, or if the time zone name is part of the group. Example:
```bash
npm add @vvo/tzdb
```
Usage:
```js
import { simplifiedTimeZones, timeZonesNames, formatTimeZone } from "@vvo/tzdb";
const value = timeZones.find((timeZone) => {
return (
dbData.timeZone === timeZone.name ||
timeZone.group.includes(dbData.timeZone)
);
});
```
## API
### simplifiedTimeZones
### timeZonesNames
### formatTimeZone(simplifiedTimeZone, { useCurrentOffset = false })
You can use this function when you want to get a formatted time zone but with the current date offset. This is useful if you always want to show the real, current time zone offset.
Example usage:
```js
import { simplifiedTimeZones, formatTimeZone } from "@vvo/tzdb";
console.log(
formatTimeZone(simplifiedTimeZones[10], { useCurrentOffset: true }),
);
// output when in DST:
// -07:00 Pacific Time - Los Angeles, San Diego
```
This function uses luxon internally, so it would be better for your build size to also use luxon in your application whenever you need to manipulate dates.
PS: If you'd like to contribute to this library, we could make it so that formatTimeZone is not luxon dependant, for example taking the code here: https://github.com/mobz/get-timezone-offset. Open an issue!
## [BETA] Algolia
You can store cities information on a search engine like [Algolia](http://algolia.com/). There's a `yarn generate` command you can use if you clone this repository to create your own Algolia index. The expected environment variables are:
```
ALGOLIA_APPLICATION_ID=... ALGOLIA_ADMIN_API_KEY=... ALGOLIA_INDEX_NAME=... yarn build
```
Here's a demo of the index:
![Demo](./demo.gif)

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