From aefcc13ca8687433c9ed3abf09cefcfb5e189abe Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Fri, 21 Jun 2024 11:44:57 +0200 Subject: [PATCH] feat(date)!: separate timeZone method (#2947) --- docs/guide/upgrading.md | 9 + src/definitions/date.ts | 9 + src/definitions/location.ts | 4 +- src/locales/base/date/index.ts | 12 + src/locales/base/date/time_zone.ts | 425 ++++++++++++++++++ src/locales/base/index.ts | 2 + src/locales/base/location/time_zone.ts | 424 +---------------- src/modules/date/index.ts | 19 + src/modules/location/index.ts | 7 +- test/modules/__snapshots__/date.spec.ts.snap | 6 + .../__snapshots__/location.spec.ts.snap | 4 +- test/modules/date.spec.ts | 26 +- test/modules/location.spec.ts | 39 +- 13 files changed, 560 insertions(+), 426 deletions(-) create mode 100644 src/locales/base/date/index.ts create mode 100644 src/locales/base/date/time_zone.ts diff --git a/docs/guide/upgrading.md b/docs/guide/upgrading.md index b562cdb096e..9797a48c314 100644 --- a/docs/guide/upgrading.md +++ b/docs/guide/upgrading.md @@ -567,6 +567,15 @@ Now, this throws an error raising awareness of that bad value. This affects the `refDate` parameter of the `anytime()`, `birthdate()`, `past()`, `future()`, `recent()` and `soon()`, methods as well as the `from` and `to` parameters of `between()` and `betweens()`. +### Separate Timezone Methods + +The `timeZone` functionality has been divided to enhance specificity: + +- Use `faker.date.timeZone()` to generate a random global time zone. +- Use `faker.location.timeZone()` to obtain time zone specific to the current locale. + +We haven't updated all locale dependent time zone data yet, so if you encounter unexpected values, please create a new issue. + ### Prices Now Return More Price-Like Values The `faker.commerce.price()` method now produces values that also return fractional values. diff --git a/src/definitions/date.ts b/src/definitions/date.ts index 2265a0256cf..1a3d073bab9 100644 --- a/src/definitions/date.ts +++ b/src/definitions/date.ts @@ -13,6 +13,15 @@ export type DateDefinition = LocaleEntry<{ * The translations for weekdays (Sunday - Saturday). */ weekday: DateEntryDefinition; + + /** + * The names of the IANA time zones. Not tied to the current locale. + * + * Since this is the same for all locales, it is only defined in the `base` locale. + * + * @see [IANA Time Zone Database](https://www.iana.org/time-zones) + */ + time_zone: string[]; }>; /** diff --git a/src/definitions/location.ts b/src/definitions/location.ts index 1814a3accdc..5aee4e79e3f 100644 --- a/src/definitions/location.ts +++ b/src/definitions/location.ts @@ -137,7 +137,9 @@ export type LocationDefinition = LocaleEntry<{ secondary_address: string[]; /** - * A list of timezones names. + * A list of time zones names relevant to this locale. + * + * @see [IANA Time Zone Database](https://www.iana.org/time-zones) */ time_zone: string[]; }>; diff --git a/src/locales/base/date/index.ts b/src/locales/base/date/index.ts new file mode 100644 index 00000000000..72136d30ae9 --- /dev/null +++ b/src/locales/base/date/index.ts @@ -0,0 +1,12 @@ +/* + * This file is automatically generated. + * Run 'pnpm run generate:locales' to update. + */ +import type { DateDefinition } from '../../..'; +import time_zone from './time_zone'; + +const date: DateDefinition = { + time_zone, +}; + +export default date; diff --git a/src/locales/base/date/time_zone.ts b/src/locales/base/date/time_zone.ts new file mode 100644 index 00000000000..7f7f73784c7 --- /dev/null +++ b/src/locales/base/date/time_zone.ts @@ -0,0 +1,425 @@ +// https://www.iana.org/time-zones - 2024a - 2024-02-01 +// This file is generated using: +// const { timeZonesNames } = await import('https://esm.sh/@vvo/tzdb'); +// JSON.stringify(timeZonesNames, null, 2); +export default [ + 'Africa/Abidjan', + 'Africa/Accra', + 'Africa/Addis_Ababa', + 'Africa/Algiers', + 'Africa/Asmara', + 'Africa/Bamako', + 'Africa/Bangui', + 'Africa/Banjul', + 'Africa/Bissau', + 'Africa/Blantyre', + 'Africa/Brazzaville', + 'Africa/Bujumbura', + 'Africa/Cairo', + 'Africa/Casablanca', + 'Africa/Ceuta', + 'Africa/Conakry', + 'Africa/Dakar', + 'Africa/Dar_es_Salaam', + 'Africa/Djibouti', + 'Africa/Douala', + 'Africa/El_Aaiun', + 'Africa/Freetown', + 'Africa/Gaborone', + 'Africa/Harare', + 'Africa/Johannesburg', + 'Africa/Juba', + 'Africa/Kampala', + 'Africa/Khartoum', + 'Africa/Kigali', + 'Africa/Kinshasa', + 'Africa/Lagos', + 'Africa/Libreville', + 'Africa/Lome', + 'Africa/Luanda', + 'Africa/Lubumbashi', + 'Africa/Lusaka', + 'Africa/Malabo', + 'Africa/Maputo', + 'Africa/Maseru', + 'Africa/Mbabane', + 'Africa/Mogadishu', + 'Africa/Monrovia', + 'Africa/Nairobi', + 'Africa/Ndjamena', + 'Africa/Niamey', + 'Africa/Nouakchott', + 'Africa/Ouagadougou', + 'Africa/Porto-Novo', + 'Africa/Sao_Tome', + 'Africa/Tripoli', + 'Africa/Tunis', + 'Africa/Windhoek', + 'America/Adak', + 'America/Anchorage', + 'America/Anguilla', + 'America/Antigua', + 'America/Araguaina', + 'America/Argentina/Buenos_Aires', + 'America/Argentina/Catamarca', + 'America/Argentina/Cordoba', + 'America/Argentina/Jujuy', + 'America/Argentina/La_Rioja', + 'America/Argentina/Mendoza', + 'America/Argentina/Rio_Gallegos', + 'America/Argentina/Salta', + 'America/Argentina/San_Juan', + 'America/Argentina/San_Luis', + 'America/Argentina/Tucuman', + 'America/Argentina/Ushuaia', + 'America/Aruba', + 'America/Asuncion', + 'America/Atikokan', + 'America/Bahia', + 'America/Bahia_Banderas', + 'America/Barbados', + 'America/Belem', + 'America/Belize', + 'America/Blanc-Sablon', + 'America/Boa_Vista', + 'America/Bogota', + 'America/Boise', + 'America/Cambridge_Bay', + 'America/Campo_Grande', + 'America/Cancun', + 'America/Caracas', + 'America/Cayenne', + 'America/Cayman', + 'America/Chicago', + 'America/Chihuahua', + 'America/Ciudad_Juarez', + 'America/Costa_Rica', + 'America/Creston', + 'America/Cuiaba', + 'America/Curacao', + 'America/Danmarkshavn', + 'America/Dawson', + 'America/Dawson_Creek', + 'America/Denver', + 'America/Detroit', + 'America/Dominica', + 'America/Edmonton', + 'America/Eirunepe', + 'America/El_Salvador', + 'America/Fort_Nelson', + 'America/Fortaleza', + 'America/Glace_Bay', + 'America/Goose_Bay', + 'America/Grand_Turk', + 'America/Grenada', + 'America/Guadeloupe', + 'America/Guatemala', + 'America/Guayaquil', + 'America/Guyana', + 'America/Halifax', + 'America/Havana', + 'America/Hermosillo', + 'America/Indiana/Indianapolis', + 'America/Indiana/Knox', + 'America/Indiana/Marengo', + 'America/Indiana/Petersburg', + 'America/Indiana/Tell_City', + 'America/Indiana/Vevay', + 'America/Indiana/Vincennes', + 'America/Indiana/Winamac', + 'America/Inuvik', + 'America/Iqaluit', + 'America/Jamaica', + 'America/Juneau', + 'America/Kentucky/Louisville', + 'America/Kentucky/Monticello', + 'America/Kralendijk', + 'America/La_Paz', + 'America/Lima', + 'America/Los_Angeles', + 'America/Lower_Princes', + 'America/Maceio', + 'America/Managua', + 'America/Manaus', + 'America/Marigot', + 'America/Martinique', + 'America/Matamoros', + 'America/Mazatlan', + 'America/Menominee', + 'America/Merida', + 'America/Metlakatla', + 'America/Mexico_City', + 'America/Miquelon', + 'America/Moncton', + 'America/Monterrey', + 'America/Montevideo', + 'America/Montserrat', + 'America/Nassau', + 'America/New_York', + 'America/Nome', + 'America/Noronha', + 'America/North_Dakota/Beulah', + 'America/North_Dakota/Center', + 'America/North_Dakota/New_Salem', + 'America/Nuuk', + 'America/Ojinaga', + 'America/Panama', + 'America/Paramaribo', + 'America/Phoenix', + 'America/Port-au-Prince', + 'America/Port_of_Spain', + 'America/Porto_Velho', + 'America/Puerto_Rico', + 'America/Punta_Arenas', + 'America/Rankin_Inlet', + 'America/Recife', + 'America/Regina', + 'America/Resolute', + 'America/Rio_Branco', + 'America/Santarem', + 'America/Santiago', + 'America/Santo_Domingo', + 'America/Sao_Paulo', + 'America/Scoresbysund', + 'America/Sitka', + 'America/St_Barthelemy', + 'America/St_Johns', + 'America/St_Kitts', + 'America/St_Lucia', + 'America/St_Thomas', + 'America/St_Vincent', + 'America/Swift_Current', + 'America/Tegucigalpa', + 'America/Thule', + 'America/Tijuana', + 'America/Toronto', + 'America/Tortola', + 'America/Vancouver', + 'America/Whitehorse', + 'America/Winnipeg', + 'America/Yakutat', + 'America/Yellowknife', + 'Antarctica/Casey', + 'Antarctica/Davis', + 'Antarctica/DumontDUrville', + 'Antarctica/Macquarie', + 'Antarctica/Mawson', + 'Antarctica/McMurdo', + 'Antarctica/Palmer', + 'Antarctica/Rothera', + 'Antarctica/Syowa', + 'Antarctica/Troll', + 'Antarctica/Vostok', + 'Arctic/Longyearbyen', + 'Asia/Aden', + 'Asia/Almaty', + 'Asia/Amman', + 'Asia/Anadyr', + 'Asia/Aqtau', + 'Asia/Aqtobe', + 'Asia/Ashgabat', + 'Asia/Atyrau', + 'Asia/Baghdad', + 'Asia/Bahrain', + 'Asia/Baku', + 'Asia/Bangkok', + 'Asia/Barnaul', + 'Asia/Beirut', + 'Asia/Bishkek', + 'Asia/Brunei', + 'Asia/Chita', + 'Asia/Choibalsan', + 'Asia/Colombo', + 'Asia/Damascus', + 'Asia/Dhaka', + 'Asia/Dili', + 'Asia/Dubai', + 'Asia/Dushanbe', + 'Asia/Famagusta', + 'Asia/Gaza', + 'Asia/Hebron', + 'Asia/Ho_Chi_Minh', + 'Asia/Hong_Kong', + 'Asia/Hovd', + 'Asia/Irkutsk', + 'Asia/Jakarta', + 'Asia/Jayapura', + 'Asia/Jerusalem', + 'Asia/Kabul', + 'Asia/Kamchatka', + 'Asia/Karachi', + 'Asia/Kathmandu', + 'Asia/Khandyga', + 'Asia/Kolkata', + 'Asia/Krasnoyarsk', + 'Asia/Kuala_Lumpur', + 'Asia/Kuching', + 'Asia/Kuwait', + 'Asia/Macau', + 'Asia/Magadan', + 'Asia/Makassar', + 'Asia/Manila', + 'Asia/Muscat', + 'Asia/Nicosia', + 'Asia/Novokuznetsk', + 'Asia/Novosibirsk', + 'Asia/Omsk', + 'Asia/Oral', + 'Asia/Phnom_Penh', + 'Asia/Pontianak', + 'Asia/Pyongyang', + 'Asia/Qatar', + 'Asia/Qostanay', + 'Asia/Qyzylorda', + 'Asia/Riyadh', + 'Asia/Sakhalin', + 'Asia/Samarkand', + 'Asia/Seoul', + 'Asia/Shanghai', + 'Asia/Singapore', + 'Asia/Srednekolymsk', + 'Asia/Taipei', + 'Asia/Tashkent', + 'Asia/Tbilisi', + 'Asia/Tehran', + 'Asia/Thimphu', + 'Asia/Tokyo', + 'Asia/Tomsk', + 'Asia/Ulaanbaatar', + 'Asia/Urumqi', + 'Asia/Ust-Nera', + 'Asia/Vientiane', + 'Asia/Vladivostok', + 'Asia/Yakutsk', + 'Asia/Yangon', + 'Asia/Yekaterinburg', + 'Asia/Yerevan', + 'Atlantic/Azores', + 'Atlantic/Bermuda', + 'Atlantic/Canary', + 'Atlantic/Cape_Verde', + 'Atlantic/Faroe', + 'Atlantic/Madeira', + 'Atlantic/Reykjavik', + 'Atlantic/South_Georgia', + 'Atlantic/St_Helena', + 'Atlantic/Stanley', + 'Australia/Adelaide', + 'Australia/Brisbane', + 'Australia/Broken_Hill', + 'Australia/Darwin', + 'Australia/Eucla', + 'Australia/Hobart', + 'Australia/Lindeman', + 'Australia/Lord_Howe', + 'Australia/Melbourne', + 'Australia/Perth', + 'Australia/Sydney', + 'Europe/Amsterdam', + 'Europe/Andorra', + 'Europe/Astrakhan', + 'Europe/Athens', + 'Europe/Belgrade', + 'Europe/Berlin', + 'Europe/Bratislava', + 'Europe/Brussels', + 'Europe/Bucharest', + 'Europe/Budapest', + 'Europe/Busingen', + 'Europe/Chisinau', + 'Europe/Copenhagen', + 'Europe/Dublin', + 'Europe/Gibraltar', + 'Europe/Guernsey', + 'Europe/Helsinki', + 'Europe/Isle_of_Man', + 'Europe/Istanbul', + 'Europe/Jersey', + 'Europe/Kaliningrad', + 'Europe/Kirov', + 'Europe/Kyiv', + 'Europe/Lisbon', + 'Europe/Ljubljana', + 'Europe/London', + 'Europe/Luxembourg', + 'Europe/Madrid', + 'Europe/Malta', + 'Europe/Mariehamn', + 'Europe/Minsk', + 'Europe/Monaco', + 'Europe/Moscow', + 'Europe/Oslo', + 'Europe/Paris', + 'Europe/Podgorica', + 'Europe/Prague', + 'Europe/Riga', + 'Europe/Rome', + 'Europe/Samara', + 'Europe/San_Marino', + 'Europe/Sarajevo', + 'Europe/Saratov', + 'Europe/Simferopol', + 'Europe/Skopje', + 'Europe/Sofia', + 'Europe/Stockholm', + 'Europe/Tallinn', + 'Europe/Tirane', + 'Europe/Ulyanovsk', + 'Europe/Vaduz', + 'Europe/Vatican', + 'Europe/Vienna', + 'Europe/Vilnius', + 'Europe/Volgograd', + 'Europe/Warsaw', + 'Europe/Zagreb', + 'Europe/Zurich', + 'Indian/Antananarivo', + 'Indian/Chagos', + 'Indian/Christmas', + 'Indian/Cocos', + 'Indian/Comoro', + 'Indian/Kerguelen', + 'Indian/Mahe', + 'Indian/Maldives', + 'Indian/Mauritius', + 'Indian/Mayotte', + 'Indian/Reunion', + 'Pacific/Apia', + 'Pacific/Auckland', + 'Pacific/Bougainville', + 'Pacific/Chatham', + 'Pacific/Chuuk', + 'Pacific/Easter', + 'Pacific/Efate', + 'Pacific/Fakaofo', + 'Pacific/Fiji', + 'Pacific/Funafuti', + 'Pacific/Galapagos', + 'Pacific/Gambier', + 'Pacific/Guadalcanal', + 'Pacific/Guam', + 'Pacific/Honolulu', + 'Pacific/Kanton', + 'Pacific/Kiritimati', + 'Pacific/Kosrae', + 'Pacific/Kwajalein', + 'Pacific/Majuro', + 'Pacific/Marquesas', + 'Pacific/Midway', + 'Pacific/Nauru', + 'Pacific/Niue', + 'Pacific/Norfolk', + 'Pacific/Noumea', + 'Pacific/Pago_Pago', + 'Pacific/Palau', + 'Pacific/Pitcairn', + 'Pacific/Pohnpei', + 'Pacific/Port_Moresby', + 'Pacific/Rarotonga', + 'Pacific/Saipan', + 'Pacific/Tahiti', + 'Pacific/Tarawa', + 'Pacific/Tongatapu', + 'Pacific/Wake', + 'Pacific/Wallis', +]; diff --git a/src/locales/base/index.ts b/src/locales/base/index.ts index 66e8a765c83..0a031fbbb50 100644 --- a/src/locales/base/index.ts +++ b/src/locales/base/index.ts @@ -5,6 +5,7 @@ import type { LocaleDefinition } from '../..'; import color from './color'; import database from './database'; +import date from './date'; import hacker from './hacker'; import internet from './internet'; import location from './location'; @@ -14,6 +15,7 @@ import system from './system'; const base: LocaleDefinition = { color, database, + date, hacker, internet, location, diff --git a/src/locales/base/location/time_zone.ts b/src/locales/base/location/time_zone.ts index 58bc6b2d8a2..c5e60398f92 100644 --- a/src/locales/base/location/time_zone.ts +++ b/src/locales/base/location/time_zone.ts @@ -1,420 +1,4 @@ -export default [ - 'Africa/Abidjan', - 'Africa/Accra', - 'Africa/Addis_Ababa', - 'Africa/Algiers', - 'Africa/Asmara', - 'Africa/Bamako', - 'Africa/Bangui', - 'Africa/Banjul', - 'Africa/Bissau', - 'Africa/Blantyre', - 'Africa/Brazzaville', - 'Africa/Bujumbura', - 'Africa/Cairo', - 'Africa/Casablanca', - 'Africa/Ceuta', - 'Africa/Conakry', - 'Africa/Dakar', - 'Africa/Dar_es_Salaam', - 'Africa/Djibouti', - 'Africa/Douala', - 'Africa/El_Aaiun', - 'Africa/Freetown', - 'Africa/Gaborone', - 'Africa/Harare', - 'Africa/Johannesburg', - 'Africa/Juba', - 'Africa/Kampala', - 'Africa/Khartoum', - 'Africa/Kigali', - 'Africa/Kinshasa', - 'Africa/Lagos', - 'Africa/Libreville', - 'Africa/Lome', - 'Africa/Luanda', - 'Africa/Lubumbashi', - 'Africa/Lusaka', - 'Africa/Malabo', - 'Africa/Maputo', - 'Africa/Maseru', - 'Africa/Mbabane', - 'Africa/Mogadishu', - 'Africa/Monrovia', - 'Africa/Nairobi', - 'Africa/Ndjamena', - 'Africa/Niamey', - 'Africa/Nouakchott', - 'Africa/Ouagadougou', - 'Africa/Porto-Novo', - 'Africa/Sao_Tome', - 'Africa/Tripoli', - 'Africa/Tunis', - 'Africa/Windhoek', - 'America/Adak', - 'America/Anchorage', - 'America/Anguilla', - 'America/Antigua', - 'America/Araguaina', - 'America/Argentina/Buenos_Aires', - 'America/Argentina/Catamarca', - 'America/Argentina/Cordoba', - 'America/Argentina/Jujuy', - 'America/Argentina/La_Rioja', - 'America/Argentina/Mendoza', - 'America/Argentina/Rio_Gallegos', - 'America/Argentina/Salta', - 'America/Argentina/San_Juan', - 'America/Argentina/San_Luis', - 'America/Argentina/Tucuman', - 'America/Argentina/Ushuaia', - 'America/Aruba', - 'America/Asuncion', - 'America/Atikokan', - 'America/Bahia', - 'America/Bahia_Banderas', - 'America/Barbados', - 'America/Belem', - 'America/Belize', - 'America/Blanc-Sablon', - 'America/Boa_Vista', - 'America/Bogota', - 'America/Boise', - 'America/Cambridge_Bay', - 'America/Campo_Grande', - 'America/Cancun', - 'America/Caracas', - 'America/Cayenne', - 'America/Cayman', - 'America/Chicago', - 'America/Chihuahua', - 'America/Costa_Rica', - 'America/Creston', - 'America/Cuiaba', - 'America/Curacao', - 'America/Danmarkshavn', - 'America/Dawson', - 'America/Dawson_Creek', - 'America/Denver', - 'America/Detroit', - 'America/Dominica', - 'America/Edmonton', - 'America/Eirunepe', - 'America/El_Salvador', - 'America/Fort_Nelson', - 'America/Fortaleza', - 'America/Glace_Bay', - 'America/Goose_Bay', - 'America/Grand_Turk', - 'America/Grenada', - 'America/Guadeloupe', - 'America/Guatemala', - 'America/Guayaquil', - 'America/Guyana', - 'America/Halifax', - 'America/Havana', - 'America/Hermosillo', - 'America/Indiana/Indianapolis', - 'America/Indiana/Knox', - 'America/Indiana/Marengo', - 'America/Indiana/Petersburg', - 'America/Indiana/Tell_City', - 'America/Indiana/Vevay', - 'America/Indiana/Vincennes', - 'America/Indiana/Winamac', - 'America/Inuvik', - 'America/Iqaluit', - 'America/Jamaica', - 'America/Juneau', - 'America/Kentucky/Louisville', - 'America/Kentucky/Monticello', - 'America/Kralendijk', - 'America/La_Paz', - 'America/Lima', - 'America/Los_Angeles', - 'America/Lower_Princes', - 'America/Maceio', - 'America/Managua', - 'America/Manaus', - 'America/Marigot', - 'America/Martinique', - 'America/Matamoros', - 'America/Mazatlan', - 'America/Menominee', - 'America/Merida', - 'America/Metlakatla', - 'America/Mexico_City', - 'America/Miquelon', - 'America/Moncton', - 'America/Monterrey', - 'America/Montevideo', - 'America/Montserrat', - 'America/Nassau', - 'America/New_York', - 'America/Nome', - 'America/Noronha', - 'America/North_Dakota/Beulah', - 'America/North_Dakota/Center', - 'America/North_Dakota/New_Salem', - 'America/Nuuk', - 'America/Ojinaga', - 'America/Panama', - 'America/Paramaribo', - 'America/Phoenix', - 'America/Port-au-Prince', - 'America/Port_of_Spain', - 'America/Porto_Velho', - 'America/Puerto_Rico', - 'America/Punta_Arenas', - 'America/Rankin_Inlet', - 'America/Recife', - 'America/Regina', - 'America/Resolute', - 'America/Rio_Branco', - 'America/Santarem', - 'America/Santiago', - 'America/Santo_Domingo', - 'America/Sao_Paulo', - 'America/Scoresbysund', - 'America/Sitka', - 'America/St_Barthelemy', - 'America/St_Johns', - 'America/St_Kitts', - 'America/St_Lucia', - 'America/St_Thomas', - 'America/St_Vincent', - 'America/Swift_Current', - 'America/Tegucigalpa', - 'America/Thule', - 'America/Tijuana', - 'America/Toronto', - 'America/Tortola', - 'America/Vancouver', - 'America/Whitehorse', - 'America/Winnipeg', - 'America/Yakutat', - 'America/Yellowknife', - 'Antarctica/Casey', - 'Antarctica/Davis', - 'Antarctica/DumontDUrville', - 'Antarctica/Macquarie', - 'Antarctica/Mawson', - 'Antarctica/McMurdo', - 'Antarctica/Palmer', - 'Antarctica/Rothera', - 'Antarctica/Syowa', - 'Antarctica/Troll', - 'Antarctica/Vostok', - 'Arctic/Longyearbyen', - 'Asia/Aden', - 'Asia/Almaty', - 'Asia/Amman', - 'Asia/Anadyr', - 'Asia/Aqtau', - 'Asia/Aqtobe', - 'Asia/Ashgabat', - 'Asia/Atyrau', - 'Asia/Baghdad', - 'Asia/Bahrain', - 'Asia/Baku', - 'Asia/Bangkok', - 'Asia/Barnaul', - 'Asia/Beirut', - 'Asia/Bishkek', - 'Asia/Brunei', - 'Asia/Chita', - 'Asia/Choibalsan', - 'Asia/Colombo', - 'Asia/Damascus', - 'Asia/Dhaka', - 'Asia/Dili', - 'Asia/Dubai', - 'Asia/Dushanbe', - 'Asia/Famagusta', - 'Asia/Gaza', - 'Asia/Hebron', - 'Asia/Ho_Chi_Minh', - 'Asia/Hong_Kong', - 'Asia/Hovd', - 'Asia/Irkutsk', - 'Asia/Jakarta', - 'Asia/Jayapura', - 'Asia/Jerusalem', - 'Asia/Kabul', - 'Asia/Kamchatka', - 'Asia/Karachi', - 'Asia/Kathmandu', - 'Asia/Khandyga', - 'Asia/Kolkata', - 'Asia/Krasnoyarsk', - 'Asia/Kuala_Lumpur', - 'Asia/Kuching', - 'Asia/Kuwait', - 'Asia/Macau', - 'Asia/Magadan', - 'Asia/Makassar', - 'Asia/Manila', - 'Asia/Muscat', - 'Asia/Nicosia', - 'Asia/Novokuznetsk', - 'Asia/Novosibirsk', - 'Asia/Omsk', - 'Asia/Oral', - 'Asia/Phnom_Penh', - 'Asia/Pontianak', - 'Asia/Pyongyang', - 'Asia/Qatar', - 'Asia/Qostanay', - 'Asia/Qyzylorda', - 'Asia/Riyadh', - 'Asia/Sakhalin', - 'Asia/Samarkand', - 'Asia/Seoul', - 'Asia/Shanghai', - 'Asia/Singapore', - 'Asia/Srednekolymsk', - 'Asia/Taipei', - 'Asia/Tashkent', - 'Asia/Tbilisi', - 'Asia/Tehran', - 'Asia/Thimphu', - 'Asia/Tokyo', - 'Asia/Tomsk', - 'Asia/Ulaanbaatar', - 'Asia/Urumqi', - 'Asia/Ust-Nera', - 'Asia/Vientiane', - 'Asia/Vladivostok', - 'Asia/Yakutsk', - 'Asia/Yangon', - 'Asia/Yekaterinburg', - 'Asia/Yerevan', - 'Atlantic/Azores', - 'Atlantic/Bermuda', - 'Atlantic/Canary', - 'Atlantic/Cape_Verde', - 'Atlantic/Faroe', - 'Atlantic/Madeira', - 'Atlantic/Reykjavik', - 'Atlantic/South_Georgia', - 'Atlantic/St_Helena', - 'Atlantic/Stanley', - 'Australia/Adelaide', - 'Australia/Brisbane', - 'Australia/Broken_Hill', - 'Australia/Darwin', - 'Australia/Eucla', - 'Australia/Hobart', - 'Australia/Lindeman', - 'Australia/Lord_Howe', - 'Australia/Melbourne', - 'Australia/Perth', - 'Australia/Sydney', - 'Europe/Amsterdam', - 'Europe/Andorra', - 'Europe/Astrakhan', - 'Europe/Athens', - 'Europe/Belgrade', - 'Europe/Berlin', - 'Europe/Bratislava', - 'Europe/Brussels', - 'Europe/Bucharest', - 'Europe/Budapest', - 'Europe/Busingen', - 'Europe/Chisinau', - 'Europe/Copenhagen', - 'Europe/Dublin', - 'Europe/Gibraltar', - 'Europe/Guernsey', - 'Europe/Helsinki', - 'Europe/Isle_of_Man', - 'Europe/Istanbul', - 'Europe/Jersey', - 'Europe/Kaliningrad', - 'Europe/Kirov', - 'Europe/Kyiv', - 'Europe/Lisbon', - 'Europe/Ljubljana', - 'Europe/London', - 'Europe/Luxembourg', - 'Europe/Madrid', - 'Europe/Malta', - 'Europe/Mariehamn', - 'Europe/Minsk', - 'Europe/Monaco', - 'Europe/Moscow', - 'Europe/Oslo', - 'Europe/Paris', - 'Europe/Podgorica', - 'Europe/Prague', - 'Europe/Riga', - 'Europe/Rome', - 'Europe/Samara', - 'Europe/San_Marino', - 'Europe/Sarajevo', - 'Europe/Saratov', - 'Europe/Simferopol', - 'Europe/Skopje', - 'Europe/Sofia', - 'Europe/Stockholm', - 'Europe/Tallinn', - 'Europe/Tirane', - 'Europe/Ulyanovsk', - 'Europe/Vaduz', - 'Europe/Vatican', - 'Europe/Vienna', - 'Europe/Vilnius', - 'Europe/Volgograd', - 'Europe/Warsaw', - 'Europe/Zagreb', - 'Europe/Zurich', - 'Indian/Antananarivo', - 'Indian/Chagos', - 'Indian/Christmas', - 'Indian/Cocos', - 'Indian/Comoro', - 'Indian/Kerguelen', - 'Indian/Mahe', - 'Indian/Maldives', - 'Indian/Mauritius', - 'Indian/Mayotte', - 'Indian/Reunion', - 'Pacific/Apia', - 'Pacific/Auckland', - 'Pacific/Bougainville', - 'Pacific/Chatham', - 'Pacific/Chuuk', - 'Pacific/Easter', - 'Pacific/Efate', - 'Pacific/Fakaofo', - 'Pacific/Fiji', - 'Pacific/Funafuti', - 'Pacific/Galapagos', - 'Pacific/Gambier', - 'Pacific/Guadalcanal', - 'Pacific/Guam', - 'Pacific/Honolulu', - 'Pacific/Kanton', - 'Pacific/Kiritimati', - 'Pacific/Kosrae', - 'Pacific/Kwajalein', - 'Pacific/Majuro', - 'Pacific/Marquesas', - 'Pacific/Midway', - 'Pacific/Nauru', - 'Pacific/Niue', - 'Pacific/Norfolk', - 'Pacific/Noumea', - 'Pacific/Pago_Pago', - 'Pacific/Palau', - 'Pacific/Pitcairn', - 'Pacific/Pohnpei', - 'Pacific/Port_Moresby', - 'Pacific/Rarotonga', - 'Pacific/Saipan', - 'Pacific/Tahiti', - 'Pacific/Tarawa', - 'Pacific/Tongatapu', - 'Pacific/Wake', - 'Pacific/Wallis', -]; +import timeZone from '../date/time_zone'; + +// The base locale uses any possible time zone. +export default timeZone; diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 10196d6e039..f2eac1cf80e 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -685,4 +685,23 @@ export class DateModule extends SimpleDateModule { assertLocaleData(values, 'date.weekday', type); return this.faker.helpers.arrayElement(values); } + + /** + * Returns a random IANA time zone name. + * + * The returned time zone is not tied to the current locale. + * + * @see [IANA Time Zone Database](https://www.iana.org/time-zones) + * @see faker.location.timeZone(): For generating a timezone based on the current locale. + * + * @example + * faker.location.timeZone() // 'Pacific/Guam' + * + * @since 9.0.0 + */ + timeZone(): string { + return this.faker.helpers.arrayElement( + this.faker.definitions.date.time_zone + ); + } } diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index 8fbbbe304ba..d55e0c8ae6a 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -596,7 +596,12 @@ export class LocationModule extends ModuleBase { } /** - * Returns a random time zone. + * Returns a random IANA time zone relevant to this locale. + * + * The returned time zone is tied to the current locale. + * + * @see [IANA Time Zone Database](https://www.iana.org/time-zones) + * @see faker.date.timeZone(): For generating a random time zone from all available time zones. * * @example * faker.location.timeZone() // 'Pacific/Guam' diff --git a/test/modules/__snapshots__/date.spec.ts.snap b/test/modules/__snapshots__/date.spec.ts.snap index 236c367b158..ee5faa6d7fc 100644 --- a/test/modules/__snapshots__/date.spec.ts.snap +++ b/test/modules/__snapshots__/date.spec.ts.snap @@ -115,6 +115,8 @@ exports[`date > 42 > soon > with only string refDate 1`] = `2021-02-22T02:08:36. exports[`date > 42 > soon > with value 1`] = `2021-02-25T11:02:38.999Z`; +exports[`date > 42 > timeZone 1`] = `"America/North_Dakota/Center"`; + exports[`date > 42 > weekday > noArgs 1`] = `"Saturday"`; exports[`date > 42 > weekday > with abbreviated = true 1`] = `"Sat"`; @@ -239,6 +241,8 @@ exports[`date > 1211 > soon > with only string refDate 1`] = `2021-02-22T15:26:1 exports[`date > 1211 > soon > with value 1`] = `2021-03-02T23:59:57.196Z`; +exports[`date > 1211 > timeZone 1`] = `"Pacific/Fiji"`; + exports[`date > 1211 > weekday > noArgs 1`] = `"Wednesday"`; exports[`date > 1211 > weekday > with abbreviated = true 1`] = `"Wed"`; @@ -361,6 +365,8 @@ exports[`date > 1337 > soon > with only string refDate 1`] = `2021-02-21T23:26:3 exports[`date > 1337 > soon > with value 1`] = `2021-02-24T08:02:25.768Z`; +exports[`date > 1337 > timeZone 1`] = `"America/Guadeloupe"`; + exports[`date > 1337 > weekday > noArgs 1`] = `"Monday"`; exports[`date > 1337 > weekday > with abbreviated = true 1`] = `"Mon"`; diff --git a/test/modules/__snapshots__/location.spec.ts.snap b/test/modules/__snapshots__/location.spec.ts.snap index 5d4b1d31e72..b53acbda355 100644 --- a/test/modules/__snapshots__/location.spec.ts.snap +++ b/test/modules/__snapshots__/location.spec.ts.snap @@ -128,7 +128,7 @@ exports[`location > 42 > streetAddress > with boolean 1`] = `"9751 Anderson Thro exports[`location > 42 > streetAddress > with useFullAddress options 1`] = `"9751 Anderson Throughway Suite 709"`; -exports[`location > 42 > timeZone 1`] = `"America/North_Dakota/New_Salem"`; +exports[`location > 42 > timeZone 1`] = `"America/North_Dakota/Center"`; exports[`location > 42 > zipCode > noArgs 1`] = `"97511"`; @@ -400,7 +400,7 @@ exports[`location > 1337 > streetAddress > with boolean 1`] = `"22435 Westley Ri exports[`location > 1337 > streetAddress > with useFullAddress options 1`] = `"22435 Westley Ridges Apt. 461"`; -exports[`location > 1337 > timeZone 1`] = `"America/Guatemala"`; +exports[`location > 1337 > timeZone 1`] = `"America/Guadeloupe"`; exports[`location > 1337 > zipCode > noArgs 1`] = `"12435"`; diff --git a/test/modules/date.spec.ts b/test/modules/date.spec.ts index 811bd81bc99..e30da8d9580 100644 --- a/test/modules/date.spec.ts +++ b/test/modules/date.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { FakerError, faker, fakerAZ } from '../../src'; +import { FakerError, allLocales, faker, fakerAZ } from '../../src'; import { seededTests } from '../support/seeded-runs'; import { times } from './../support/times'; @@ -59,6 +59,8 @@ describe('date', () => { }); }); + t.it('timeZone'); + t.describe('between', (t) => { t.it('with string dates', { from: '2021-02-21T17:09:15.711Z', @@ -653,9 +655,31 @@ describe('date', () => { ); }); }); + + describe('timeZone', () => { + it('should return a random timezone', () => { + const actual = faker.date.timeZone(); + expect(faker.definitions.date.time_zone).toContain(actual); + }); + }); } ); + describe('definitions', () => { + describe('timeZone', () => { + it('should have timezones in the base locale', () => { + expect(allLocales.base.date?.time_zone).toSatisfy(Array.isArray); + expect(allLocales.base.date?.time_zone?.length).toBeGreaterThan(0); + }); + + it.each( + Object.entries(allLocales).filter(([locale]) => locale !== 'base') + )('should not have any timezones in %s', (_, data) => { + expect(data.date?.time_zone).toBeUndefined(); + }); + }); + }); + describe('refDateSource', () => { afterEach(() => { faker.setDefaultRefDate(); diff --git a/test/modules/location.spec.ts b/test/modules/location.spec.ts index dc82757d29d..1291efd31fd 100644 --- a/test/modules/location.spec.ts +++ b/test/modules/location.spec.ts @@ -1,5 +1,11 @@ import { describe, expect, it } from 'vitest'; -import { FakerError, faker, fakerEN_CA, fakerEN_US } from '../../src'; +import { + FakerError, + allLocales, + faker, + fakerEN_CA, + fakerEN_US, +} from '../../src'; import { seededTests } from '../support/seeded-runs'; import { times } from './../support/times'; @@ -390,6 +396,37 @@ describe('location', () => { } ); }); + + describe('timeZone', () => { + it('should return a random timezone', () => { + const actual = faker.location.timeZone(); + expect(faker.definitions.location.time_zone).toContain(actual); + }); + }); } ); }); + +describe('definitions', () => { + describe('timeZone', () => { + it.each(Object.entries(allLocales))( + 'locale data for %s should be a subset of the base locale', + (locale, data) => { + if (locale === 'base') { + expect(data.location?.time_zone).toSatisfy(Array.isArray); + expect(data.location?.time_zone?.length).toBeGreaterThan(0); + expect(data.location?.time_zone).toEqual( + allLocales.base.date?.time_zone + ); + } else if (data.location?.time_zone != null) { + expect(data.location.time_zone).toSatisfy(Array.isArray); + expect(data.location.time_zone.length).toBeGreaterThan(0); + // expected and actual are flipped here + expect(allLocales.base.date?.time_zone).toEqual( + expect.arrayContaining(data.location.time_zone) + ); + } + } + ); + }); +});