Created
December 14, 2024 20:05
-
-
Save leodevbro/471ab473bd4bbaf9010ee5f263f37e71 to your computer and use it in GitHub Desktop.
List all strange timezones for your locale (JS)
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
const showStrangeTimezonesForMyLocale = (yearFrom, yearTo) => { | |
if (yearFrom > yearTo) { | |
throw new Error("yearFrom must not be greater than yearTo"); | |
} | |
const correctTimezone = new Date().getTimezoneOffset(); | |
const fullSearchCount = yearTo - yearFrom + 1; | |
console.log("Correct timezone is:", correctTimezone, "(offset minutes)"); | |
console.log( | |
"searching years\nfrom:", | |
yearFrom, | |
",", | |
"to:", | |
yearTo, | |
",", | |
"count:", | |
fullSearchCount, | |
"\n\n" | |
); | |
const results = | |
// : { | |
// rangeInclLeftInclRight: [number, number]; | |
// count: number; | |
// timezone: number; | |
// }[] | |
[]; | |
const recentRange = { | |
start: yearFrom, | |
tz: new Date(Date.UTC(yearFrom, 4)).getTimezoneOffset(), | |
}; | |
for (let year = yearFrom + 1; year <= yearTo; year += 1) { | |
const currTz = new Date(Date.UTC(year, 4)).getTimezoneOffset(); | |
if (currTz !== recentRange.tz) { | |
if (recentRange.tz !== correctTimezone) { | |
results.push({ | |
rangeInclLeftInclRight: [recentRange.start, year - 1], | |
count: year - recentRange.start, | |
timezone: recentRange.tz, | |
}); | |
} | |
recentRange.start = year; | |
recentRange.tz = currTz; | |
} | |
} | |
if (recentRange.tz !== correctTimezone) { | |
results.push({ | |
rangeInclLeftInclRight: [recentRange.start, yearTo], | |
count: yearTo - recentRange.start + 1, | |
timezone: recentRange.tz, | |
}); | |
} | |
results.forEach((x) => { | |
console.log( | |
`from: ${x.rangeInclLeftInclRight[0]}, to: ${x.rangeInclLeftInclRight[1]} --> count: ${x.count}, strange timezone: ${x.timezone}` | |
); | |
}); | |
const fullCountOfStrange = results.reduce( | |
(accu, curr) => accu + curr.count, | |
0 | |
); | |
console.log( | |
"\nFull count of years with strange timezone:", | |
fullCountOfStrange, | |
`(${((100 * fullCountOfStrange) / fullSearchCount).toFixed( | |
2 | |
)}% of ${fullSearchCount})`, | |
"\n" | |
); | |
}; | |
showStrangeTimezonesForMyLocale(1, 2024); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment