UNPKG

@vvo/tzdb

Version:

Always up to date list of time zones, with grouping and major cities

1 lines 7.11 kB
{"version":3,"sources":["../../lib/utils/timeZone.js"],"names":["ianaRegex","typeToPos","year","month","day","hour","minute","second","isValidIanaSpecifier","s","match","hackyOffset","dtf","date","formatted","format","replace","parsed","exec","fMonth","fDay","fYear","fHour","fMinute","fSecond","partsOffset","formatToParts","filled","i","length","type","value","pos","parseInt","makeDTF","zone","Intl","DateTimeFormat","hourCycle","timeZone","objToLocalTS","obj","d","Date","UTC","millisecond","setUTCFullYear","getUTCFullYear","getZoneOffset","timeZoneName","now","_","asUTC","asTS","over"],"mappings":";;;;;;AAAA;AACA;AAEA;AAEA;AAEA;AAEA,MAAMA,SAAS,GACb,sEADF;AAGA,MAAMC,SAAS,GAAG;AAChBC,EAAAA,IAAI,EAAE,CADU;AAEhBC,EAAAA,KAAK,EAAE,CAFS;AAGhBC,EAAAA,GAAG,EAAE,CAHW;AAIhBC,EAAAA,IAAI,EAAE,CAJU;AAKhBC,EAAAA,MAAM,EAAE,CALQ;AAMhBC,EAAAA,MAAM,EAAE;AANQ,CAAlB;;AASA,SAASC,oBAAT,CAA8BC,CAA9B,EAAiC;AAC/B,SAAO,CAAC,EAAEA,CAAC,IAAIA,CAAC,CAACC,KAAF,CAAQV,SAAR,CAAP,CAAR;AACD;;AAED,SAASW,WAAT,CAAqBC,GAArB,EAA0BC,IAA1B,EAAgC;AAC9B,QAAMC,SAAS,GAAGF,GAAG,CAACG,MAAJ,CAAWF,IAAX,EAAiBG,OAAjB,CAAyB,SAAzB,EAAoC,EAApC,CAAlB;AACA,QAAMC,MAAM,GAAG,0CAA0CC,IAA1C,CAA+CJ,SAA/C,CAAf;AACA,QAAM,GAAGK,MAAH,EAAWC,IAAX,EAAiBC,KAAjB,EAAwBC,KAAxB,EAA+BC,OAA/B,EAAwCC,OAAxC,IAAmDP,MAAzD;AACA,SAAO,CAACI,KAAD,EAAQF,MAAR,EAAgBC,IAAhB,EAAsBE,KAAtB,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EAAAA,IAAI,IAAIC,IAAI,IAAI,CAAR,GAAYA,IAAZ,GAAmB,OAAOA,IAAlC;AACA,SAAO,CAACF,KAAK,GAAGC,IAAT,KAAkB,KAAK,IAAvB,CAAP;AACD","sourcesContent":["// Most of the code here is from Luxon\n// Copyright 2019 JS Foundation and other contributors\n\n// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\n// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nconst ianaRegex =\n /^[A-Za-z_+-]{1,256}(:?\\/[A-Za-z_+-]{1,256}(\\/[A-Za-z_+-]{1,256})?)?$/;\n\nconst typeToPos = {\n year: 0,\n month: 1,\n day: 2,\n hour: 3,\n minute: 4,\n second: 5,\n};\n\nfunction isValidIanaSpecifier(s) {\n return !!(s && s.match(ianaRegex));\n}\n\nfunction hackyOffset(dtf, date) {\n const formatted = dtf.format(date).replace(/\\u200E/g, \"\");\n const parsed = /(\\d+)\\/(\\d+)\\/(\\d+),? (\\d+):(\\d+):(\\d+)/.exec(formatted);\n const [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed;\n return [fYear, fMonth, fDay, fHour, fMinute, fSecond];\n}\n\nfunction partsOffset(dtf, date) {\n const formatted = dtf.formatToParts(date);\n const filled = [];\n for (let i = 0; i < formatted.length; i++) {\n const { type, value } = formatted[i];\n const pos = typeToPos[type];\n\n if (typeof pos !== \"undefined\") {\n filled[pos] = parseInt(value, 10);\n }\n }\n return filled;\n}\n\nfunction makeDTF(zone) {\n return new Intl.DateTimeFormat(\"en-US\", {\n hourCycle: \"h23\",\n timeZone: zone,\n year: \"numeric\",\n month: \"2-digit\",\n day: \"2-digit\",\n hour: \"2-digit\",\n minute: \"2-digit\",\n second: \"2-digit\",\n });\n}\n\n// covert a calendar object to a local timestamp (epoch, but with the offset baked in)\nfunction objToLocalTS(obj) {\n let d = Date.UTC(\n obj.year,\n obj.month - 1,\n obj.day,\n obj.hour,\n obj.minute,\n obj.second,\n obj.millisecond,\n );\n\n // for legacy reasons, years between 0 and 99 are interpreted as 19XX; revert that\n if (obj.year < 100 && obj.year >= 0) {\n d = new Date(d);\n d.setUTCFullYear(d.getUTCFullYear() - 1900);\n }\n return +d;\n}\n\nexport function getZoneOffset(timeZoneName) {\n if (!isValidIanaSpecifier(timeZoneName)) {\n return false;\n }\n\n const date = new Date(Date.now());\n\n let dtf;\n\n try {\n dtf = makeDTF(timeZoneName);\n } catch (_) {\n return false;\n }\n\n const [year, month, day, hour, minute, second] = dtf.formatToParts\n ? partsOffset(dtf, date)\n : hackyOffset(dtf, date);\n\n const asUTC = objToLocalTS({\n year,\n month,\n day,\n hour,\n minute,\n second,\n millisecond: 0,\n });\n\n let asTS = +date;\n const over = asTS % 1000;\n asTS -= over >= 0 ? over : 1000 + over;\n return (asUTC - asTS) / (60 * 1000);\n}\n"],"file":"timeZone.js"}