Tôi sẽ trả lời câu hỏi này cho Node.js bằng cách sử dụng TypeScript [vui lòng xóa các loại nếu bạn muốn sử dụng với kế hoạch JavaScript]. Đối với điều đó, chúng tôi sẽ cần 2 gói NPM.
- moment-timezone
- city-timezones
Tuyên bố miễn trừ trách nhiệm: Timezones rất phức tạp. Không phải tất cả các múi giờ đều cách nhau 1 giờ và một số cài đặt thời gian tiết kiệm ánh sáng ban ngày là kỳ lạ, ví dụ, chênh lệch 15 phút thay vì 60 phút tiêu chuẩn. Đây là một triển khai ngây thơ phù hợp với trường hợp sử dụng của tôi. Sử dụng một cách quyết định.Timezones are COMPLEX. Not all timezones are 1 hour apart and some daylight saving time settings are weird with, for example, 15 minutes difference instead of the standard 60 minutes. This is a naive implementation that suited my use case. Use with discretion.
Code:
import * as cityTimeZones from "city-timezones";
import * as moment from "moment-timezone";
/**
* Returns the UTC offset for the given timezone
* @param timezone Example: America/New_York
*/
export function getNormalizedUtcOffset[timezone: string]: number | null {
const momentTimezone = moment.tz[timezone];
if [!momentTimezone] {
return null;
}
let offset = momentTimezone.utcOffset[];
if [momentTimezone.isDST[]] {
// utcOffset will return the offset normalized by DST. If the location
// is in daylight saving time now, it will be adjusted for that. This is
// a NAIVE attempt to normalize that by going back 1 hour
offset -= 60;
}
return offset/60;
}
/**
* Returns the offset range for the given city or region
* @param location
*/
export function getUtcOffsetForLocation[location: string]: number[] | null {
const timezones = cityTimeZones.findFromCityStateProvince[location];
if [timezones && timezones.length] {
// timezones will contain an array of all timezones for all cities inside
// the given location. For example, if location is a country, this will contain
// all timezones of all cities inside the country.
// YOU SHOULD CACHE THE RESULT OF THIS FUNCTION.
const offsetSet = new Set[];
for [let timezone of timezones] {
const offset = getNormalizedUtcOffset[timezone.timezone];
if [offset !== null] {
offsetSet.add[offset];
}
}
return [...offsetSet].sort[[a, b] => a - b];
}
return null;
}
Bài kiểm tra đơn vị [với jest]
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
3.3.0 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản một năm trướcPublic • Published a year ago
- Readme
- Khám phá BetaBETA
- 0 phụ thuộc
- 88 người phụ thuộc
- 24 phiên bản
Các quốc gia và thời gian
Thư viện tối giản để làm việc với dữ liệu của các quốc gia và múi giờ. Cập nhật với cơ sở dữ liệu IANA Timezones.
Cách sử dụng
Nodejs
Cài đặt với NPM hoặc Sợi:
npm install --save countries-and-timezones
Trình duyệt
Thêm tập lệnh sau vào dự án của bạn [chỉ ~ 9kb]:
var data = ct.getCountry['MX']; console.log[data];
API
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
3
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một quốc gia được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
4.Chấp nhận một tham số với
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5.Thí dụ
const ct = require['countries-and-timezones']; const country = ct.getCountry['DE']; console.log[country]; /* Prints: { id: 'DE', name: 'Germany', timezones: [ 'Europe/Berlin', 'Europe/Zurich' ] } */
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.Thí dụ
const ct = require['countries-and-timezones']; const timezone = ct.getTimezone['America/Los_Angeles']; console.log[timezone]; /* Prints: { name: 'America/Los_Angeles', countries: [ 'US' ], utcOffset: -480, utcOffsetStr: '-08:00', dstOffset: -420, dstOffsetStr: '-07:00', aliasOf: null } */
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.Chấp nhận một tham số với
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5.Thí dụ
const ct = require['countries-and-timezones']; const countries = ct.getAllCountries[]; console.log[countries]; /* Prints: { AD: { id: 'AD', name: 'Andorra', timezones: [ 'Europe/Andorra' ] }, AE: { id: 'AE', name: 'United Arab Emirates', timezones: [ 'Asia/Dubai' ] }, AF: { id: 'AF', name: 'Afghanistan', timezones: [ 'Asia/Kabul' ] }, AG: { id: 'AG', name: 'Antigua and Barbuda', timezones: [ 'America/Puerto_Rico' ] }, ... } */
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.Chấp nhận một tham số với
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5.Thí dụ
const ct = require['countries-and-timezones']; const timezones = ct.getAllTimezones[]; console.log[timezones]; /* Prints: { "Africa/Abidjan": { "name": "Africa/Abidjan", "countries": [ "CI", "BF", "GH", "GM", "GN", "ML", "MR", "SH", "SL", "SN", "TG" ], "utcOffset": 0, "utcOffsetStr": "+00:00", "dstOffset": 0, "dstOffsetStr": "+00:00", "aliasOf": null }, "Africa/Algiers": { "name": "Africa/Algiers", "countries": [ "DZ" ], "utcOffset": 60, "utcOffsetStr": "+01:00", "dstOffset": 60, "dstOffsetStr": "+01:00", "aliasOf": null }, "Africa/Bissau": { "name": "Africa/Bissau", "countries": [ "GW" ], "utcOffset": 0, "utcOffsetStr": "+00:00", "dstOffset": 0, "dstOffsetStr": "+00:00", "aliasOf": null }, ... } */
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.Chấp nhận một tham số với
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5.Thí dụ
const ct = require['countries-and-timezones']; const timezones = ct.getTimezonesForCountry['MX']; console.log[timezones]; /* Prints: [ { "name": "America/Bahia_Banderas", "countries": [ "MX" ], "utcOffset": -360, "utcOffsetStr": "-06:00", "dstOffset": -300, "dstOffsetStr": "-05:00", "aliasOf": null }, { "name": "America/Cancun", "countries": [ "MX" ], "utcOffset": -300, "utcOffsetStr": "-05:00", "dstOffset": -300, "dstOffsetStr": "-05:00", "aliasOf": null }, { "name": "America/Chihuahua", "countries": [ "MX" ], "utcOffset": -420, "utcOffsetStr": "-07:00", "dstOffset": -360, "dstOffsetStr": "-06:00", "aliasOf": null }, ... } */
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.the first element is more relevant due to its geographical location.Chấp nhận một tham số với
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5.Thí dụ
const ct = require['countries-and-timezones']; const timezone = ct.getCountriesForTimezone['Europe/Zurich']; console.log[timezone]; /* Prints: [ { "id": "CH", "name": "Switzerland", "timezones": [ "Europe/Zurich" ] }, { "id": "DE", "name": "Germany", "timezones": [ "Europe/Berlin", "Europe/Zurich" ] }, { "id": "LI", "name": "Liechtenstein", "timezones": [ "Europe/Zurich" ] } ] */
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.Chấp nhận một tham số với
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5.Thí dụ
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
0 import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
6
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
Trả về một múi giờ được tham chiếu bởi
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7 của nó.
Trả về một đối tượng với dữ liệu của tất cả các múi giờ. | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
npm install --save countries-and-timezones
5
Trả về một danh sách các quốc gia sử dụng múi giờ cho import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7. Khi một múi giờ có nhiều quốc gia, yếu tố đầu tiên có liên quan hơn do vị trí địa lý của nó.
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
npm install --save countries-and-timezones8
Trả về một đối tượng với dữ liệu của tất cả các múi giờ. | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
npm install --save countries-and-timezones5 | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
npm install --save countries-and-timezones5 | Trả về một danh sách các quốc gia sử dụng múi giờ cho 7. Khi một múi giờ có nhiều quốc gia, yếu tố đầu tiên có liên quan hơn do vị trí địa lý của nó. | npm install --save countries-and-timezones8 |
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
1Trả về một quốc gia có liên quan nhất [do vị trí địa lý của nó] sử dụng múi giờ được cho import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7.
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5
npm install --save countries-and-timezones5 | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
npm install --save countries-and-timezones5 | Trả về một danh sách các quốc gia sử dụng múi giờ cho 7. Khi một múi giờ có nhiều quốc gia, yếu tố đầu tiên có liên quan hơn do vị trí địa lý của nó. | npm install --save countries-and-timezones8 |
Trả về một quốc gia có liên quan nhất [do vị trí địa lý của nó] sử dụng múi giờ được cho 7. | 5 | Các tùy chọn có sẵn cho các chức năng là:minutes between the timezone and UTC. |
Tham số | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
npm install --save countries-and-timezones5 | 5 | Các tùy chọn có sẵn cho các chức năng là:minutes between the timezone and UTC during daylight saving time [DST]. When const ct = require['countries-and-timezones']; const country = ct.getCountry['DE']; console.log[country]; /* Prints: { id: 'DE', name: 'Germany', timezones: [ 'Europe/Berlin', 'Europe/Zurich' ] } */1 and const ct = require['countries-and-timezones']; const country = ct.getCountry['DE']; console.log[country]; /* Prints: { id: 'DE', name: 'Germany', timezones: [ 'Europe/Berlin', 'Europe/Zurich' ] } */4 are the same, means that the timezone does not observe a daylight saving time. |
Tham số | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
npm install --save countries-and-timezones5 | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
Trả về một đối tượng với dữ liệu của tất cả các múi giờ. | npm install --save countries-and-timezones2 | Trả về một mảng với tất cả thời gian của một quốc gia được đưa ra 4. |
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
2 npm install --save countries-and-timezones
5
- Trả về một danh sách các quốc gia sử dụng múi giờ cho
7. Khi một múi giờ có nhiều quốc gia, yếu tố đầu tiên có liên quan hơn do vị trí địa lý của nó.import { getUtcOffsetForLocation } from "../timezone"; describe["timezone", [] => { describe["getUtcOffsetForLocation", [] => { it["should work for Lisbon", [] => { expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]]; }]; it["should work for Berlin", [] => { expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]]; }]; it["should work for Germany", [] => { expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]]; }]; it["should work for the United States", [] => { // when the region has multiple timezones, expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]]; }]; it["should return null for a non-existing region", [] => { // when the region has multiple timezones, expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null]; }]; }]; }];
-
npm install --save countries-and-timezones
8
Trả về một quốc gia có liên quan nhất [do vị trí địa lý của nó] sử dụng múi giờ được cho import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
7.
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
import { getUtcOffsetForLocation } from "../timezone";
describe["timezone", [] => {
describe["getUtcOffsetForLocation", [] => {
it["should work for Lisbon", [] => {
expect[getUtcOffsetForLocation["Lisbon"]].toEqual[[0]];
}];
it["should work for Berlin", [] => {
expect[getUtcOffsetForLocation["Berlin"]].toEqual[[1]];
}];
it["should work for Germany", [] => {
expect[getUtcOffsetForLocation["Germany"]].toEqual[[1]];
}];
it["should work for the United States", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["United States"]].toEqual[ [-10, -9, -8, -7, -6, -5, -4]];
}];
it["should return null for a non-existing region", [] => {
// when the region has multiple timezones,
expect[getUtcOffsetForLocation["Blablabla"]].toEqual[ null];
}];
}];
}];
5Các tùy chọn có sẵn cho các chức năng là:
Tham số
- Loại hình in the world. By full strings or autocompletion.
- Sự mô tả
var data = ct.getCountry['MX']; console.log[data];
2
Boolean
Cho biết nếu kết quả có nên bao gồm thời gian không dùng nữa hay không. Theo mặc định, không có thời gian không dùng nữa được bao gồm.