-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Renamed the former type `TimeZone` to `TimeZoneOffset`. (This should not be breaking, since it is not exported in `index.ts` and only internal type.) * Change to existing code is the `TimeZone`-parameter for DateTime.localize which took what before was a `TimeZoneOffset`. However, this wasn't working. In reality it expected a IANA-timezone. (Else an error was thrown.) * Added a `TimeZone` type for IANA-timezone identifiers. (Not strictly ISO-standard, but related to ISO 8601) IANA-Timezone looks like this: * `"Europe/Stockholm"` * `"Europe/London"` * `"UTC"`
- Loading branch information
Showing
8 changed files
with
190 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as isolyGlobal from "." | ||
import { isoly } from "." | ||
import { TimeZone } from "./TimeZone" | ||
|
||
describe("TimeZone", () => { | ||
it("Imports", () => { | ||
expect(isolyGlobal.TimeZone.is("Europe/Stockholm")).toBe(true) | ||
expect(isoly.TimeZone.is("Europe/Stockholm")).toBe(true) | ||
expect(TimeZone.is("Europe/Stockholm")).toBe(true) | ||
}) | ||
it("Real timezones", () => { | ||
expect(TimeZone.is("Europe/Stockholm")).toBe(true) | ||
expect(TimeZone.is("Europe/London")).toBe(true) | ||
expect(TimeZone.is("Africa/Abidjan")).toBe(true) | ||
expect(TimeZone.is("Africa/Accra")).toBe(true) | ||
expect(TimeZone.is("Africa/Addis_Ababa")).toBe(true) | ||
expect(TimeZone.is("Africa/Algiers")).toBe(true) | ||
expect(TimeZone.is("Africa/Asmara")).toBe(true) | ||
expect(TimeZone.is("Africa/Bamako")).toBe(true) | ||
expect(TimeZone.is("Africa/Bangui")).toBe(true) | ||
expect(TimeZone.is("Africa/Banjul")).toBe(true) | ||
expect(TimeZone.is("Africa/Bissau")).toBe(true) | ||
expect(TimeZone.is("Africa/Blantyre")).toBe(true) | ||
expect(TimeZone.is("Africa/Brazzaville")).toBe(true) | ||
expect(TimeZone.is("Africa/Bujumbura")).toBe(true) | ||
expect(TimeZone.is("Africa/Cairo")).toBe(true) | ||
expect(TimeZone.is("Africa/Casablanca")).toBe(true) | ||
expect(TimeZone.is("Africa/Ceuta")).toBe(true) | ||
expect(TimeZone.is("Africa/Conakry")).toBe(true) | ||
expect(TimeZone.is("Africa/Dakar")).toBe(true) | ||
expect(TimeZone.is("Africa/Dar_es_Salaam")).toBe(true) | ||
expect(TimeZone.is("Africa/Djibouti")).toBe(true) | ||
expect(TimeZone.is("Africa/Douala")).toBe(true) | ||
expect(TimeZone.is("Africa/El_Aaiun")).toBe(true) | ||
expect(TimeZone.is("Africa/Freetown")).toBe(true) | ||
expect(TimeZone.is("Africa/Gaborone")).toBe(true) | ||
expect(TimeZone.is("Africa/Harare")).toBe(true) | ||
expect(TimeZone.is("Africa/Johannesburg")).toBe(true) | ||
expect(TimeZone.is("Africa/Juba")).toBe(true) | ||
expect(TimeZone.is("Africa/Kampala")).toBe(true) | ||
expect(TimeZone.is("Africa/Khartoum")).toBe(true) | ||
expect(TimeZone.is("Africa/Kigali")).toBe(true) | ||
expect(TimeZone.is("Africa/Kinshasa")).toBe(true) | ||
expect(TimeZone.is("Africa/Lagos")).toBe(true) | ||
expect(TimeZone.is("Africa/Libreville")).toBe(true) | ||
expect(TimeZone.is("Africa/Lome")).toBe(true) | ||
expect(TimeZone.is("Africa/Luanda")).toBe(true) | ||
expect(TimeZone.is("Africa/Lubumbashi")).toBe(true) | ||
expect(TimeZone.is("Africa/Lusaka")).toBe(true) | ||
expect(TimeZone.is("Africa/Malabo")).toBe(true) | ||
expect(TimeZone.is("Africa/Maputo")).toBe(true) | ||
expect(TimeZone.is("Africa/Maseru")).toBe(true) | ||
expect(TimeZone.is("Africa/Mbabane")).toBe(true) | ||
expect(TimeZone.is("Africa/Mogadishu")).toBe(true) | ||
expect(TimeZone.is("Africa/Monrovia")).toBe(true) | ||
expect(TimeZone.is("Africa/Nairobi")).toBe(true) | ||
expect(TimeZone.is("Africa/Ndjamena")).toBe(true) | ||
expect(TimeZone.is("Africa/Niamey")).toBe(true) | ||
expect(TimeZone.is("Africa/Nouakchott")).toBe(true) | ||
expect(TimeZone.is("Africa/Ouagadougou")).toBe(true) | ||
expect(TimeZone.is("Africa/Porto-Novo")).toBe(true) | ||
expect(TimeZone.is("Africa/Sao_Tome")).toBe(true) | ||
expect(TimeZone.is("Africa/Tripoli")).toBe(true) | ||
expect(TimeZone.is("Africa/Tunis")).toBe(true) | ||
expect(TimeZone.is("Africa/Windhoek")).toBe(true) | ||
expect(TimeZone.is("America/Adak")).toBe(true) | ||
expect(TimeZone.is("Pacific/Wallis")).toBe(true) | ||
expect(TimeZone.is("UTC")).toBe(true) | ||
expect(TimeZone.is("GMT")).toBe(true) | ||
}) | ||
it("Flawed timezones", () => { | ||
expect(TimeZone.is("Europe/Göteborg")).toBe(false) | ||
expect(TimeZone.is("+01:00")).toBe(false) | ||
expect(TimeZone.is(undefined)).toBe(false) | ||
expect(TimeZone.is("timezone")).toBe(false) | ||
expect(TimeZone.is("TZ")).toBe(false) | ||
expect(TimeZone.is("Z")).toBe(false) | ||
expect(TimeZone.is("")).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,13 @@ | ||
export type TimeZone = typeof TimeZone.values[number] | ||
|
||
/** IANA format. */ | ||
export type TimeZone = "Europe/Stockholm" | "Europe/London" | "UTC" | (string & Record<never, never>) // The Record<never...> makes autocomplete work in your IDE. | ||
export namespace TimeZone { | ||
export const values: string[] = [ | ||
"-12:00", | ||
"-11:00", | ||
"-10:00", | ||
"-09:30", | ||
"-09:00", | ||
"-08:00", | ||
"-07:00", | ||
"-06:00", | ||
"-05:00", | ||
"-04:00", | ||
"-03:30", | ||
"-03:00", | ||
"-02:00", | ||
"-01:00", | ||
"Z", | ||
"+01:00", | ||
"+02:00", | ||
"+03:00", | ||
"+03:30", | ||
"+04:00", | ||
"+04:30", | ||
"+05:00", | ||
"+05:30", | ||
"+05:45", | ||
"+06:00", | ||
"+06:30", | ||
"+07:00", | ||
"+08:00", | ||
"+08:45", | ||
"+09:00", | ||
"+09:30", | ||
"+10:00", | ||
"+10:30", | ||
"+11:00", | ||
"+12:00", | ||
"+12:45", | ||
"+13:00", | ||
"+14:00", | ||
] | ||
export function is(value: TimeZone | any): value is TimeZone { | ||
return typeof value == "string" && values.includes(value) | ||
let result: boolean | ||
try { | ||
result = typeof value == "string" && !!new Intl.DateTimeFormat("en-GB", { timeZone: value }) | ||
} catch (error) { | ||
result = false | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export type TimeZoneOffset = typeof TimeZoneOffset.values[number] | ||
|
||
export namespace TimeZoneOffset { | ||
export const values: string[] = [ | ||
"-12:00", | ||
"-11:00", | ||
"-10:00", | ||
"-09:30", | ||
"-09:00", | ||
"-08:00", | ||
"-07:00", | ||
"-06:00", | ||
"-05:00", | ||
"-04:00", | ||
"-03:30", | ||
"-03:00", | ||
"-02:00", | ||
"-01:00", | ||
"Z", | ||
"+01:00", | ||
"+02:00", | ||
"+03:00", | ||
"+03:30", | ||
"+04:00", | ||
"+04:30", | ||
"+05:00", | ||
"+05:30", | ||
"+05:45", | ||
"+06:00", | ||
"+06:30", | ||
"+07:00", | ||
"+08:00", | ||
"+08:45", | ||
"+09:00", | ||
"+09:30", | ||
"+10:00", | ||
"+10:30", | ||
"+11:00", | ||
"+12:00", | ||
"+12:45", | ||
"+13:00", | ||
"+14:00", | ||
] | ||
export function is(value: TimeZoneOffset | any): value is TimeZoneOffset { | ||
return typeof value == "string" && values.includes(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.