views:

328

answers:

7

Hi all,

I got UTC timestamps and want to display them for different timezones.

Maybe an example can clarify what I am trying to do:

Given the timestamp 1259431620000 those times should be displayed:

Atlanta 13:07

San Francisco 10:07

London 17:07

Sydney 04:07

The cities might change but I guess you get what I am looking for.

Is there a javascript library or a common solution that deals with the daylight saving?

I all ready have a list of all the time zones.

As the daylight saving is an easy and common problem I thought there might be a library or a script dealing with them.

Just in case you are interesting in the timezone table:

* GMT -11:00
      o Apia
      o Midway
      o Niue
      o Pago Pago
* GMT -10:00
      o Fakaofo
      o Hawaii Time
      o Johnston
      o Rarotonga
      o Tahiti
* GMT -09:00
      o (GMT-09:30) Marquesas
      o Alaska Time
      o Gambier
* GMT -08:00
      o Pacific Time
      o Pacific Time - Tijuana
      o Pacific Time - Vancouver
      o Pacific Time - Whitehorse
      o Pitcairn
* GMT -07:00
      o Mountain Time - Dawson Creek
      o Mountain Time (America/Denver)
      o Mountain Time - Edmonton
      o Mountain Time - Hermosillo
      o Mountain Time - Chihuahua, Mazatlan
      o Mountain Time - Arizona
      o Mountain Time - Yellowknife
* GMT -06:00
      o Belize
      o Central Time
      o Costa Rica
      o El Salvador
      o Guatemala
      o Managua
      o Central Time - Mexico City
      o Central Time - Regina
      o Central Time (America/Tegucigalpa)
      o Central Time - Winnipeg
      o Easter Island
      o Galapagos
* GMT -05:00
      o Bogota
      o Cayman
      o Grand Turk
      o Guayaquil
      o Havana
      o Eastern Time - Iqaluit
      o Jamaica
      o Lima
      o Eastern Time - Montreal
      o Nassau
      o Eastern Time
      o Panama
      o Port-au-Prince
      o Eastern Time - Toronto
* GMT -04:00
      o (GMT-04:30) Caracas
      o Anguilla
      o Antigua
      o Aruba
      o Asuncion
      o Barbados
      o Boa Vista
      o Campo Grande
      o Cuiaba
      o Curacao
      o Dominica
      o Grenada
      o Guadeloupe
      o Guyana
      o Atlantic Time - Halifax
      o La Paz
      o Manaus
      o Martinique
      o Montserrat
      o Port of Spain
      o Porto Velho
      o Puerto Rico
      o Rio Branco
      o Santiago
      o Santo Domingo
      o St. Kitts
      o St. Lucia
      o St. Thomas
      o St. Vincent
      o Thule
      o Tortola
      o Palmer
      o Bermuda
      o Stanley
* GMT -03:00
      o Newfoundland Time - St. Johns
      o Araguaina
      o Buenos Aires
      o Salvador
      o Belem
      o Cayenne
      o Fortaleza
      o Godthab
      o Maceio
      o Miquelon
      o Montevideo
      o Paramaribo
      o Recife
      o Sao Paulo
      o Rothera
* GMT -02:00
      o Noronha
      o South Georgia
* GMT -01:00
      o Scoresbysund
      o Azores
      o Cape Verde
      o Abidjan
* GMT 00:00
      o Accra
      o Bamako
      o Banjul
      o Bissau
      o Casablanca
      o Conakry
      o Dakar
      o El Aaiun
      o Freetown
      o Lome
      o Monrovia
      o Nouakchott
      o Ouagadougou
      o Sao Tome
      o Danmarkshavn
      o Canary Islands
      o Faeroe
      o Reykjavik
      o St Helena
      o GMT (no daylight saving)
      o Dublin
      o Lisbon
      o London
* GMT +01:00
      o Algiers
      o Bangui
      o Brazzaville
      o Ceuta
      o Douala
      o Kinshasa
      o Lagos
      o Libreville
      o Luanda
      o Malabo
      o Ndjamena
      o Niamey
      o Porto-Novo
      o Tunis
      o Windhoek
      o Amsterdam
      o Andorra
      o Central European Time
      o Berlin
      o Brussels
      o Budapest
      o Copenhagen
      o Gibraltar
      o Luxembourg
      o Madrid
      o Malta
      o Monaco
      o Oslo
      o Paris
      o Rome
      o Stockholm
      o Tirane
      o Vaduz
      o Vienna
      o Warsaw
      o Zurich
* GMT +02:00
      o Blantyre
      o Bujumbura
      o Cairo
      o Gaborone
      o Harare
      o Johannesburg
      o Kigali
      o Lubumbashi
      o Lusaka
      o Maputo
      o Maseru
      o Mbabane
      o Tripoli
      o Amman
      o Beirut
      o Damascus
      o Gaza
      o Jerusalem
      o Nicosia
      o Athens
      o Bucharest
      o Chisinau
      o Helsinki
      o Istanbul
      o Moscow-01 - Kaliningrad
      o Kiev
      o Minsk
      o Riga
      o Sofia
      o Tallinn
      o Vilnius
* GMT +03:00
      o Addis Ababa
      o Asmera
      o Dar es Salaam
      o Djibouti
      o Kampala
      o Khartoum
      o Mogadishu
      o Nairobi
      o Syowa
      o Aden
      o Baghdad
      o Bahrain
      o Kuwait
      o Qatar
      o Riyadh
      o Moscow+00
      o Antananarivo
      o Comoro
      o Mayotte
      o (GMT+03:30) Tehran
* GMT +04:00
      o Baku
      o Dubai
      o Muscat
      o Tbilisi
      o Yerevan
      o Moscow+01 - Samara
      o Mahe
      o Mauritius
      o Reunion
      o (GMT+04:30) Kabul
* GMT +05:00
      o Aqtau
      o Aqtobe
      o Ashgabat
      o Dushanbe
      o Karachi
      o Tashkent
      o Moscow+02 - Yekaterinburg
      o Kerguelen
      o Maldives
      o (GMT+05:30) India Standard Time
      o (GMT+05:30) Colombo
      o (GMT+05:45) Katmandu
* GMT +06:00
      o Mawson
      o Vostok
      o Almaty
      o Bishkek
      o Dhaka
      o Moscow+03 - Omsk, Novosibirsk
      o Thimphu
      o Chagos
      o (GMT+06:30) Rangoon
      o (GMT+06:30) Cocos
* GMT +07:00
      o Davis
      o Bangkok
      o Hovd
      o Jakarta
      o Moscow+04 - Krasnoyarsk
      o Phnom Penh
      o Hanoi
      o Vientiane
      o Christmas
* GMT +08:00
      o Casey
      o Brunei
      o Choibalsan
      o Hong Kong
      o Moscow+05 - Irkutsk
      o Kuala Lumpur
      o Macau
      o Makassar
      o Manila
      o China Time - Beijing
      o Singapore
      o Taipei
      o Ulaanbaatar
      o Western Time - Perth
* GMT +09:00
      o Dili
      o Jayapura
      o Pyongyang
      o Seoul
      o Tokyo
      o Moscow+06 - Yakutsk
      o Palau
      o (GMT+09:30) Central Time - Adelaide
      o (GMT+09:30) Central Time - Darwin
* GMT +10:00
      o Dumont D'Urville
      o Moscow+07 - Yuzhno-Sakhalinsk
      o Eastern Time - Brisbane
      o Eastern Time - Hobart
      o Eastern Time - Melbourne, Sydney
      o Guam
      o Port Moresby
      o Saipan
      o Truk
* GMT +11:00
      o Moscow+08 - Magadan
      o Efate
      o Guadalcanal
      o Kosrae
      o Noumea
      o Ponape
      o (GMT+11:30) Norfolk
* GMT +12:00
      o Moscow+09 - Petropavlovsk-Kamchatskiy
      o Auckland
      o Fiji
      o Funafuti
      o Kwajalein
      o Majuro
      o Nauru
      o Tarawa
      o Wake
      o Wallis
* GMT +13:00
      o Enderbury
      o Tongatapu
      o Kiritimati

Thanks in advance

+1  A: 

It's a bit long-winded but this looks like it should probably work...

http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/

IanR
Thanks, however this is only the users daylight saving setting. See my updated question for further details.
Ghommey
+1  A: 

Since timezone is basically just adding some hours integer, translating from UTC time to localized time is trivial.

So I guess what you really need is a list of all cities/countries and their respective timezones... ? or am I misunderstanding?

What exactly do you need to accomplish?

Assaf Lavie
Some time zones include half hours such as CST in Australia and Nepal has a quarter hour in it at the moment although they are about to change it. Jon Skeet gave an excellent presentation covering exactly these difficulties today at Stackoverflow Dev Day London.
Dave Anderson
A list with all daylight saving rules would be all I need.
Ghommey
A: 

You could just mark the time as UTC and provide something like this;

2010-01-01 00:00 (UTC) get time locally

www.worldtimeserver.com don't seem to have a service you can consume to specify location so the user would need to select their own. You could subscribe to the Time Zone Database but that's $400 per year.

Dave Anderson
Well there has to be an easier way than paying 400$ per year ;)
Ghommey
A: 

It seems like what you're asking for is a list of daylight savings time rules. So, here is a list of countries that observe DST, and how they observe it:

http://www.timeanddate.com/time/dst2009.html

James Skidmore
+1  A: 

I should point out that DST time rules change constantly and in some countries they change on a yearly basis. I would try to solve this approach server-side but if that isn't possible I would consider using Ajax to call back to the server to get the current time or DST offset for the user. Maintaining your own list of timezones and zone rule changes is going to be a hassle, especially if you have to deploy it. But if you have a zone rule database actually implementing the library should be trivial.

Mr. Shiny and New
+1  A: 

A comprehensive source for timezone information is the Olson list. It is widely used in the UNIX world, and also available in e.g. Python in the form of pytz. It appears that someone has created some code that makes it available in JavaScript also, as mentioned in an older SO question.

This seems to be pretty much exactly what you're looking for ;)

Tuure Laurinolli
Thanks a lot! I did not see this SO question. This is exactly what I was looking for.
Ghommey
+1  A: 

Here's a very rich javascript library 'DateJs' - It also supports DayLight saving.

http://www.datejs.com/

this. __curious_geek
That looks really nice! `Date.getTimezoneOffset("PST", true);` solves my problem - however the documentation is very short.I'll use the solution Tuure Laurinolli posted as it allows you only to select all cities you need and it uses an open standard.
Ghommey