Skip to content

Instantly share code, notes, and snippets.

@leodevbro
Created December 14, 2024 20:02
Show Gist options
  • Save leodevbro/a40651ce418ada8f33c4ab03c2c4d787 to your computer and use it in GitHub Desktop.
Save leodevbro/a40651ce418ada8f33c4ab03c2c4d787 to your computer and use it in GitHub Desktop.
List all strange timezones for your locale
const showStrangeTimezonesForMyLocale = (yearFrom: number, yearTo: number) => {
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"
);
return results;
};
showStrangeTimezonesForMyLocale(1, 2024);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment