Skip to content

Instantly share code, notes, and snippets.

@Yaffle
Created September 13, 2013 11:13
Show Gist options
  • Save Yaffle/6549379 to your computer and use it in GitHub Desktop.
Save Yaffle/6549379 to your computer and use it in GitHub Desktop.
timeZone.html
<script>
var timeZoneIds = [
"Africa/Abidjan",
"Africa/Accra",
"Africa/Addis_Ababa",
"Africa/Algiers",
"Africa/Asmera",
"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/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/Sao_Tome",
"Africa/Tripoli",
"Africa/Tunis",
"Africa/Windhoek",
"America/Adak",
"America/Anchorage",
"America/Anguilla",
"America/Antigua",
"America/Araguaina",
"America/Aruba",
"America/Asuncion",
"America/Bahia",
"America/Bahia_Banderas",
"America/Barbados",
"America/Belem",
"America/Belize",
"America/Boa_Vista",
"America/Bogota",
"America/Boise",
"America/Buenos_Aires",
"America/Cambridge_Bay",
"America/Campo_Grande",
"America/Cancun",
"America/Caracas",
"America/Catamarca",
"America/Cayenne",
"America/Cayman",
"America/Chicago",
"America/Chihuahua",
"America/Coral_Harbour",
"America/Cordoba",
"America/Costa_Rica",
"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/Fortaleza",
"America/Glace_Bay",
"America/Godthab",
"America/Goose_Bay",
"America/Grand_Turk",
"America/Grenada",
"America/Guadeloupe",
"America/Guatemala",
"America/Guayaquil",
"America/Guyana",
"America/Halifax",
"America/Havana",
"America/Hermosillo",
"America/Indianapolis",
"America/Inuvik",
"America/Iqaluit",
"America/Jamaica",
"America/Jujuy",
"America/Juneau",
"America/La_Paz",
"America/Lima",
"America/Los_Angeles",
"America/Louisville",
"America/Maceio",
"America/Managua",
"America/Manaus",
"America/Marigot",
"America/Martinique",
"America/Matamoros",
"America/Mazatlan",
"America/Mendoza",
"America/Menominee",
"America/Merida",
"America/Metlakatla",
"America/Mexico_City",
"America/Miquelon",
"America/Moncton",
"America/Monterrey",
"America/Montevideo",
"America/Montreal",
"America/Montserrat",
"America/Nassau",
"America/New_York",
"America/Nipigon",
"America/Nome",
"America/Noronha",
"America/Ojinaga",
"America/Panama",
"America/Pangnirtung",
"America/Paramaribo",
"America/Phoenix",
"America/Porto_Velho",
"America/Puerto_Rico",
"America/Rainy_River",
"America/Rankin_Inlet",
"America/Recife",
"America/Regina",
"America/Resolute",
"America/Rio_Branco",
"America/Santa_Isabel",
"America/Santarem",
"America/Santiago",
"America/Santo_Domingo",
"America/Sao_Paulo",
"America/Scoresbysund",
"America/Shiprock",
"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/Thunder_Bay",
"America/Tijuana",
"America/Toronto",
"America/Tortola",
"America/Vancouver",
"America/Whitehorse",
"America/Winnipeg",
"America/Yakutat",
"America/Yellowknife",
"Antarctica/Casey",
"Antarctica/Davis",
"Antarctica/Macquarie",
"Antarctica/Mawson",
"Antarctica/Palmer",
"Antarctica/Rothera",
"Antarctica/South_Pole",
"Antarctica/Syowa",
"Antarctica/Vostok",
"Arctic/Longyearbyen",
"Asia/Aden",
"Asia/Almaty",
"Asia/Amman",
"Asia/Anadyr",
"Asia/Aqtau",
"Asia/Aqtobe",
"Asia/Ashgabat",
"Asia/Baghdad",
"Asia/Bahrain",
"Asia/Baku",
"Asia/Bangkok",
"Asia/Beirut",
"Asia/Bishkek",
"Asia/Brunei",
"Asia/Calcutta",
"Asia/Choibalsan",
"Asia/Chongqing",
"Asia/Colombo",
"Asia/Damascus",
"Asia/Dhaka",
"Asia/Dili",
"Asia/Dubai",
"Asia/Dushanbe",
"Asia/Gaza",
"Asia/Harbin",
"Asia/Hebron",
"Asia/Hong_Kong",
"Asia/Hovd",
"Asia/Irkutsk",
"Asia/Jakarta",
"Asia/Jayapura",
"Asia/Jerusalem",
"Asia/Kabul",
"Asia/Kamchatka",
"Asia/Karachi",
"Asia/Kashgar",
"Asia/Katmandu",
"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/Qyzylorda",
"Asia/Rangoon",
"Asia/Riyadh",
"Asia/Saigon",
"Asia/Sakhalin",
"Asia/Samarkand",
"Asia/Seoul",
"Asia/Shanghai",
"Asia/Singapore",
"Asia/Taipei",
"Asia/Tashkent",
"Asia/Tbilisi",
"Asia/Tehran",
"Asia/Thimphu",
"Asia/Tokyo",
"Asia/Ulaanbaatar",
"Asia/Urumqi",
"Asia/Vientiane",
"Asia/Vladivostok",
"Asia/Yakutsk",
"Asia/Yekaterinburg",
"Asia/Yerevan",
"Atlantic/Azores",
"Atlantic/Bermuda",
"Atlantic/Canary",
"Atlantic/Cape_Verde",
"Atlantic/Faeroe",
"Atlantic/Madeira",
"Atlantic/Reykjavik",
"Atlantic/South_Georgia",
"Atlantic/St_Helena",
"Atlantic/Stanley",
"Australia/Adelaide",
"Australia/Brisbane",
"Australia/Broken_Hill",
"Australia/Currie",
"Australia/Darwin",
"Australia/Eucla",
"Australia/Hobart",
"Australia/Lindeman",
"Australia/Lord_Howe",
"Australia/Melbourne",
"Australia/Perth",
"Australia/Sydney",
"Etc/GMT",
"Etc/UTC",
"Europe/Amsterdam",
"Europe/Andorra",
"Europe/Athens",
"Europe/Belgrade",
"Europe/Berlin",
"Europe/Bratislava",
"Europe/Brussels",
"Europe/Bucharest",
"Europe/Budapest",
"Europe/Chisinau",
"Europe/Copenhagen",
"Europe/Dublin",
"Europe/Gibraltar",
"Europe/Guernsey",
"Europe/Helsinki",
"Europe/Istanbul",
"Europe/Jersey",
"Europe/Kaliningrad",
"Europe/Kiev",
"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/Simferopol",
"Europe/Skopje",
"Europe/Sofia",
"Europe/Stockholm",
"Europe/Tallinn",
"Europe/Tirane",
"Europe/Uzhgorod",
"Europe/Vaduz",
"Europe/Vatican",
"Europe/Vienna",
"Europe/Vilnius",
"Europe/Volgograd",
"Europe/Warsaw",
"Europe/Zagreb",
"Europe/Zaporozhye",
"Europe/Zurich",
"GMT",
"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/Chatham",
"Pacific/Easter",
"Pacific/Efate",
"Pacific/Enderbury",
"Pacific/Fakaofo",
"Pacific/Fiji",
"Pacific/Funafuti",
"Pacific/Galapagos",
"Pacific/Gambier",
"Pacific/Guadalcanal",
"Pacific/Guam",
"Pacific/Honolulu",
"Pacific/Johnston",
"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/Ponape",
"Pacific/Port_Moresby",
"Pacific/Rarotonga",
"Pacific/Saipan",
"Pacific/Tahiti",
"Pacific/Tarawa",
"Pacific/Tongatapu",
"Pacific/Truk",
"Pacific/Wake",
"Pacific/Wallis",
"UTC"
];
var timeZones = {
"0": "UTC",
"3600": "Africa/Tunis",
"7200": "Asia/Hebron",
"10800": "Indian/Mayotte",
"14400": "Indian/Reunion",
"16200": "Asia/Kabul",
"18000": "Indian/Maldives",
"19800": "Asia/Colombo",
"20700": "Asia/Katmandu",
"21600": "Indian/Chagos",
"23400": "Indian/Cocos",
"25200": "Indian/Christmas",
"28800": "Australia/Perth",
"31500": "Australia/Eucla",
"32400": "Pacific/Palau",
"34200": "Australia/Darwin",
"36000": "Pacific/Truk",
"39600": "Pacific/Ponape",
"41400": "Pacific/Norfolk",
"43200": "Pacific/Wallis",
"46800": "Pacific/Tongatapu",
"50400": "Pacific/Kiritimati",
"7200,1351386000000,3600": "Europe/Zurich",
"3600,1346547600000,7200": "Africa/Windhoek",
"-32400,1352026800000,-36000": "America/Adak",
"-28800,1352023200000,-32400": "America/Yakutat",
"-14400": "America/Tortola",
"-10800": "Antarctica/Rothera",
"-14400,1349049600000,-10800": "America/Asuncion",
"-10800,1350788400000,-7200": "America/Sao_Paulo",
"-18000,1351407600000,-21600": "America/Monterrey",
"-21600": "Pacific/Galapagos",
"-18000": "America/Panama",
"-21600,1352016000000,-25200": "America/Yellowknife",
"-14400,1350792000000,-10800": "America/Cuiaba",
"-16200": "America/Caracas",
"-18000,1352012400000,-21600": "America/Winnipeg",
"-21600,1351411200000,-25200": "America/Mazatlan",
"-25200,1352019600000,-28800": "America/Whitehorse",
"-25200": "America/Phoenix",
"-14400,1352008800000,-18000": "America/Toronto",
"-10800,1352005200000,-14400": "Atlantic/Bermuda",
"-7200,1351386000000,-10800": "America/Godthab",
"-14400,1351400400000,-18000": "America/Havana",
"-28800": "Pacific/Pitcairn",
"-7200,1352001600000,-10800": "America/Miquelon",
"-10800,1349049600000,-7200": "America/Montevideo",
"-7200": "Atlantic/South_Georgia",
"-25200,1351414800000,-28800": "America/Santa_Isabel",
"-14400,1350187200000,-10800": "Antarctica/Palmer",
"0,1351386000000,-3600": "Atlantic/Azores",
"-9000,1352003400000,-12600": "America/St_Johns",
"43200,1348927200000,46800": "Pacific/Auckland",
"10800,1351202400000,7200": "Asia/Amman",
"18000,1351382400000,14400": "Asia/Baku",
"10800,1351371600000,7200": "Asia/Beirut",
"10800,1351198800000,7200": "Asia/Damascus",
"10800,1348354800000,7200": "Asia/Jerusalem",
"10800,1351386000000,7200": "Europe/Zaporozhye",
"16200,1348169400000,12600": "Asia/Tehran",
"18000,1351375200000,14400": "Asia/Yerevan",
"3600,1351386000000,0": "Europe/London",
"-3600": "Atlantic/Cape_Verde",
"-14400,1346565600000,-10800": "Atlantic/Stanley",
"34200,1349541000000,37800": "Australia/Broken_Hill",
"36000,1349539200000,39600": "Australia/Sydney",
"37800,1349537400000,39600": "Australia/Lord_Howe",
"45900,1348927200000,49500": "Pacific/Chatham",
"-21600,1350187200000,-18000": "Pacific/Easter",
"-36000": "Pacific/Tahiti",
"-32400": "Pacific/Gambier",
"-34200": "Pacific/Marquesas",
"-39600": "Pacific/Pago_Pago"
};
var TimeZoneUtils = {
formatsCache: {},
getTimeZoneOffset: function (timeZoneId, timeStamp) {
if (timeZoneId === null) {
var date = new Date(timeStamp);
return date.getTimezoneOffset() * -60;
} else {
var formatsCache = TimeZoneUtils.formatsCache;
var x = formatsCache[timeZoneId];
if (x === undefined) {
var options = {hour12: false, day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", timeZone: timeZoneId};
x = new Intl.DateTimeFormat("en", options);
formatsCache[timeZoneId] = x;
}
var date = new Date(timeStamp);
var s = x.format(date);
var m = s.match(/^(\d\d)\s*(\d\d)\:(\d\d):(\d\d)$/);
if (!m) {
throw new Error(s);//!
}
var gmtTime = ((date.getUTCDate() * 24 + date.getUTCHours()) * 60 + date.getUTCMinutes()) * 60 + date.getUTCSeconds();
var localTime = ((Number(m[1]) * 24 + Number(m[2])) * 60 + Number(m[3])) * 60 + Number(m[4]);
var offset = localTime - gmtTime;
return offset;
}
},
getTimeZoneTransitions: function (timeZoneId) {
var to = 1356998400000;
var from = to - 0.5 * 366 * 86400 * 1000;
var step = 86400000 * 28;
var lastOffset = -1;
var transitions = "";
while ((from += step) < to) {
var offset = TimeZoneUtils.getTimeZoneOffset(timeZoneId, from);
if (lastOffset === -1) {
lastOffset = offset;
transitions += String(offset);
}
if (offset !== lastOffset) {
// binary search
var a = from - step;
var b = from;
while (b - a > 1) {
var m = a + Math.floor((b - a) / 2);
if (TimeZoneUtils.getTimeZoneOffset(timeZoneId, m) === lastOffset) {
a = m;
} else {
b = m;
}
}
transitions += "," + String(b) + "," + String(offset);
lastOffset = offset;
}
}
return transitions;
},
offsetToString: function (offset) {
var a = Math.abs(offset);
var hours = Math.floor(a / 3600);
var minutes = Math.floor((a % 60) / 60);
return "GMT" + (offset < 0 ? "-" : "+") + (hours < 10 ? "0" : "") + hours + (minutes < 10 ? "0" : "") + minutes;
},
getTimeZone: function () {
return timeZones[TimeZoneUtils.getTimeZoneTransitions(null)] || TimeZoneUtils.offsetToString(TimeZoneUtils.getTimeZoneOffset(null, new Date().getTime()));
},
getTimeZonesMap: function () {
var map = {};
var i = -1;
while (++i < timeZoneIds.length) {
var id = timeZoneIds[i];
try {
var s = TimeZoneUtils.getTimeZoneTransitions(id);
map[s] = id;
} catch (e) {
if (e instanceof RangeError) {
// Unsupported time zone
console.log(String(e.message));
} else {
throw e;
}
}
}
return map;
}
};
console.log(TimeZoneUtils.getTimeZone());
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment