I'm looking for a function that receives a time, source timezone and a destination and returns time in the destination timezone
timezones are in the following format: 'utc', 'est', 'edt', 'gmt', ...
function convertTime(time,fromZone,toZone){
// do conversion
return convertedTime;
}
Sorry, but it is not possible to convert between time zones in the way you describe, for several reasons:
Many time zone abbreviations are ambiguous. For example, CST
could be Central Standard Time, Cuba Standard Time, or China Standard Time. IST
could be India Standard Time, Israel Standard Time, or Irish Summer Time. There are many other examples.
Not everyone agrees on what the abbreviations for time zones should be. For example, in Hawaii, both HST
and HAST
are commonly used.
Not all time zone abbreviations are in English. For example, HNE
and HAE
are French time zones abbreviations used for the Eastern time zone in Canada.
Not all time zones have abbreviations in the way we think about them. For example, there are two time zones in Kazakhstan. In the western part of the country they are 5 hours ahead of UTC. In the eastern part of the country they are 6 hours ahead of UTC. In the past, English speakers have assigned made-up abbreviations such as AQTT
or ORAT
to the west, and ALMT
to the east, after the names of some major cities, but none of those are actually used there nor ever have been.
Assuming EST
and EDT
are Eastern Standard Time and Eastern Daylight Time as observed in the US and Canada, you would have to predetermine whether to pass EST
or EDT
to your function depending on which was in effect at the time given. For example, if you passed a time on today, 2013-03-13 and used EST
, you would be in error since EDT
is currently in effect. This would apply for both fromZone
and toZone
, and each would have to be determined independently.
Say you tried to write logic to address the previous point. You would still have problems where two different time zone abbreviations apply to different parts of the same time zone at the same time. For example, right now MDT
applies to most of the US Mountain time zone - except most (but not all) of Arizona where MST
applies.
All of the above factors and more are considered by using IANA tz database identifiers, such as America/New_York
, instead of abbreviations.
I also suggest reviewing the following resources: