001    package railo.commons.date;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    import java.util.TimeZone;
006    
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.exp.ExpressionException;
009    import railo.runtime.op.Caster;
010    import railo.runtime.type.List;
011    
012    public class TimeZoneUtil {
013    
014            private static final Map<String,TimeZone> IDS=new HashMap<String,TimeZone>();
015            
016            static {
017                    String[] ids=TimeZone.getAvailableIDs();
018                    for(int i=0;i<ids.length;i++){
019                            IDS.put(ids[i].toLowerCase(), TimeZone.getTimeZone(ids[i]));
020                    }
021                    TimeZone def = TimeZone.getDefault();
022                    if(def!=null)IDS.put(def.getID(),def);
023                    IDS.put("jvm", TimeZone.getDefault());
024                    IDS.put("default", TimeZone.getDefault());
025                    IDS.put("", TimeZone.getDefault());
026    
027                    // MS specific Timezone definions
028                    set("Dateline Standard Time",TimeZoneConstants.ETC_GMT_PLUS_12); // (GMT-12:00) International Date Line West
029                    set("Samoa Standard Time",TimeZoneConstants.PACIFIC_MIDWAY); //         (GMT-11:00) Midway Island, Samoa
030                    set("Hawaiian Standard Time",TimeZoneConstants.HST); // (GMT-10:00) Hawaii
031                    set("Alaskan Standard Time",TimeZoneConstants.AST); // (GMT-09:00) Alaska
032                    set("Pacific Standard Time",TimeZoneConstants.PST); // (GMT-08:00) Pacific Time (US and Canada); Tijuana
033                    set("Mountain Standard Time",TimeZoneConstants.MST); // (GMT-07:00) Mountain Time (US and Canada)
034                    set("Mexico Standard Time",TimeZoneConstants.MEXICO_GENERAL); // (GMT-06:00) Guadalajara, Mexico City, Monterrey
035                    set("Mexico Standard Time 2",TimeZoneConstants.AMERICA_CHIHUAHUA); // (GMT-07:00) Chihuahua, La Paz, Mazatlan
036                    set("U.S. Mountain Standard Time",TimeZoneConstants.MST); // (GMT-07:00) Arizona
037                    set("Central Standard Time",TimeZoneConstants.CST); // (GMT-06:00) Central Time (US and Canada
038                    set("Canada Central Standard Time",TimeZoneConstants.CANADA_CENTRAL); // (GMT-06:00) Saskatchewan
039                    set("Central America Standard Time",TimeZoneConstants.CST); // (GMT-06:00) Central America
040                    set("Eastern Standard Time",TimeZoneConstants.EST); // (GMT-05:00) Eastern Time (US and Canada)
041                    set("U.S. Eastern Standard Time",TimeZoneConstants.EST); // (GMT-05:00) Indiana (East)
042                    set("S.A. Pacific Standard Time",TimeZoneConstants.AMERICA_BOGOTA); // (GMT-05:00) Bogota, Lima, Quito
043                    set("Atlantic Standard Time",TimeZoneConstants.CANADA_ATLANTIC); // (GMT-04:00) Atlantic Time (Canada)
044                    set("S.A. Western Standard Time",TimeZoneConstants.AMERICA_ANTIGUA); // (GMT-04:00) Caracas, La Paz
045                    set("Pacific S.A. Standard Time",TimeZoneConstants.AMERICA_SANTIAGO); // (GMT-04:00) Santiago
046                    set("Newfoundland and Labrador Standard Time",TimeZoneConstants.CNT); // (GMT-03:30) Newfoundland and Labrador
047                    set("E. South America Standard Time",TimeZoneConstants.BET); // (GMT-03:00) Brasilia
048                    set("S.A. Eastern Standard Time",TimeZoneConstants.AMERICA_ARGENTINA_BUENOS_AIRES); // (GMT-03:00) Buenos Aires, Georgetown
049                    set("Greenland Standard Time",TimeZoneConstants.AMERICA_GODTHAB); // (GMT-03:00) Greenland
050                    set("Mid-Atlantic Standard Time",TimeZoneConstants.AMERICA_NORONHA); // (GMT-02:00) Mid-Atlantic
051                    set("Azores Standard Time",TimeZoneConstants.ATLANTIC_AZORES); // (GMT-01:00) Azores
052                    set("Cape Verde Standard Time",TimeZoneConstants.ATLANTIC_CAPE_VERDE); // (GMT-01:00) Cape Verde Islands
053                    set("Central Europe Standard Time",TimeZoneConstants.CET); // (GMT+01:00) Belgrade, Bratislava, Budapest, Ljubljana, Prague
054                    set("Central European Standard Time",TimeZoneConstants.CET); // (GMT+01:00) Sarajevo, Skopje, Warsaw, Zagreb
055                    set("Romance Standard Time",TimeZoneConstants.EUROPE_BRUSSELS); // (GMT+01:00) Brussels, Copenhagen, Madrid, Paris
056                    set("W. Europe Standard Time",TimeZoneConstants.CET); // (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
057                    set("W. Central Africa Standard Time",null); // (GMT+01:00) West Central Africa
058                    set("E. Europe Standard Time",TimeZoneConstants.ART); // (GMT+02:00) Bucharest
059                    set("Egypt Standard Time",TimeZoneConstants.EGYPT); // (GMT+02:00) Cairo
060                    set("FLE Standard Time",TimeZoneConstants.EET); // (GMT+02:00) Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius
061                    set("GTB Standard Time",TimeZoneConstants.EUROPE_ATHENS); // (GMT+02:00) Athens, Istanbul, Minsk
062                    set("Israel Standard Time",TimeZoneConstants.ASIA_JERUSALEM); // (GMT+02:00) Jerusalem
063                    set("South Africa Standard Time",TimeZoneConstants.AFRICA_JOHANNESBURG); // (GMT+02:00) Harare, Pretoria
064                    set("Russian Standard Time",TimeZoneConstants.EUROPE_MOSCOW); // (GMT+03:00) Moscow, St. Petersburg, Volgograd
065                    set("Arab Standard Time",TimeZoneConstants.ASIA_KUWAIT); // (GMT+03:00) Kuwait, Riyadh
066                    set("E. Africa Standard Time",TimeZoneConstants.AFRICA_NAIROBI); // (GMT+03:00) Nairobi
067                    set("Arabic Standard Time",TimeZoneConstants.ASIA_BAGHDAD); // (GMT+03:00) Baghdad
068                    set("Iran Standard Time",TimeZoneConstants.ASIA_TEHRAN); // (GMT+03:30) Tehran
069                    set("Arabian Standard Time",TimeZoneConstants.ASIA_MUSCAT); // (GMT+04:00) Abu Dhabi, Muscat
070                    set("Caucasus Standard Time",TimeZoneConstants.ASIA_YEREVAN); // (GMT+04:00) Baku, Tbilisi, Yerevan
071                    set("Transitional Islamic State of Afghanistan Standard Time",TimeZoneConstants.ASIA_KABUL); // (GMT+04:30) Kabul
072                    set("Ekaterinburg Standard Time",TimeZoneConstants.ASIA_YEKATERINBURG); // (GMT+05:00) Ekaterinburg
073                    set("West Asia Standard Time",TimeZoneConstants.ASIA_KARACHI); // (GMT+05:00) Islamabad, Karachi, Tashkent
074                    set("India Standard Time",TimeZoneConstants.IST); // (GMT+05:30) Chennai, Kolkata, Mumbai, New Delhi
075                    set("Nepal Standard Time",TimeZoneConstants.ASIA_KATMANDU); // (GMT+05:45) Kathmandu
076                    set("Central Asia Standard Time",TimeZoneConstants.ASIA_DHAKA); //(GMT+06:00) Astana, Dhaka 
077                    set("Sri Lanka Standard Time",TimeZoneConstants.ASIA_COLOMBO); // (GMT+06:00) Sri Jayawardenepura
078                    set("N. Central Asia Standard Time",TimeZoneConstants.ASIA_ALMATY); // (GMT+06:00) Almaty, Novosibirsk
079                    set("Myanmar Standard Time",TimeZoneConstants.ASIA_RANGOON); // (GMT+06:30) Yangon Rangoon
080                    set("S.E. Asia Standard Time",TimeZoneConstants.ASIA_BANGKOK); // (GMT+07:00) Bangkok, Hanoi, Jakarta
081                    set("North Asia Standard Time",TimeZoneConstants.ASIA_KRASNOYARSK); // (GMT+07:00) Krasnoyarsk
082                    set("China Standard Time",TimeZoneConstants.CTT); // (GMT+08:00) Beijing, Chongqing, Hong Kong SAR, Urumqi
083                    set("Singapore Standard Time",TimeZoneConstants.ASIA_SINGAPORE); // (GMT+08:00) Kuala Lumpur, Singapore
084                    set("Taipei Standard Time",TimeZoneConstants.ASIA_TAIPEI); // (GMT+08:00) Taipei
085                    set("W. Australia Standard Time",TimeZoneConstants.AUSTRALIA_PERTH); // (GMT+08:00) Perth
086                    set("North Asia East Standard Time",TimeZoneConstants.ASIA_IRKUTSK); // (GMT+08:00) Irkutsk, Ulaanbaatar
087                    set("Korea Standard Time",TimeZoneConstants.ASIA_SEOUL); // (GMT+09:00) Seoul
088                    set("Tokyo Standard Time",TimeZoneConstants.ASIA_TOKYO); // (GMT+09:00) Osaka, Sapporo, Tokyo
089                    set("Yakutsk Standard Time",TimeZoneConstants.ASIA_YAKUTSK); // (GMT+09:00) Yakutsk
090                    set("A.U.S. Central Standard Time",TimeZoneConstants.ACT); // (GMT+09:30) Darwin
091                    set("Cen. Australia Standard Time",TimeZoneConstants.ACT); // (GMT+09:30) Adelaide
092                    set("A.U.S. Eastern Standard Time",TimeZoneConstants.AET); // (GMT+10:00) Canberra, Melbourne, Sydney
093                    set("E. Australia Standard Time",TimeZoneConstants.AET); // (GMT+10:00) Brisbane
094                    set("Tasmania Standard Time",TimeZoneConstants.AUSTRALIA_TASMANIA); // (GMT+10:00) Hobart
095                    set("Vladivostok Standard Time",TimeZoneConstants.ASIA_VLADIVOSTOK); // (GMT+10:00) Vladivostok
096                    set("West Pacific Standard Time",TimeZoneConstants.PACIFIC_GUAM); // (GMT+10:00) Guam, Port Moresby
097                    set("Central Pacific Standard Time",TimeZoneConstants.ASIA_MAGADAN); // (GMT+11:00) Magadan, Solomon Islands, New Caledonia
098                    set("Fiji Islands Standard Time",TimeZoneConstants.PACIFIC_FIJI); // (GMT+12:00) Fiji Islands, Kamchatka, Marshall Islands
099                    set("New Zealand Standard Time",TimeZoneConstants.NZ); // (GMT+12:00) Auckland, Wellington
100                    set("Tonga Standard Time",TimeZoneConstants.PACIFIC_TONGATAPU); // (GMT+13:00) Nuku'alofa
101                    
102                    
103            }
104            
105            private static void set(String name, TimeZone tz) {
106                    if(tz==null) return;
107                    name=StringUtil.replace(name.trim().toLowerCase(), " ", "", false);
108                    IDS.put(name.toLowerCase(), tz);
109            }
110    
111            /**
112             * return the string format of the Timezone
113             * @param timezone
114             * @return
115             */
116            public static String toString(TimeZone timezone){
117                    return timezone.getID();
118            }
119    
120            private static String getSupportedTimeZonesAsString() {
121                    return List.arrayToList(TimeZone.getAvailableIDs(),", ");
122            }
123            
124            /**
125             * translate timezone string format to a timezone
126             * @param strTimezone
127             * @return
128             */
129            public static TimeZone toTimeZone(String strTimezone,TimeZone defaultValue){
130                    if(strTimezone==null) return defaultValue;
131                    strTimezone=StringUtil.replace(strTimezone.trim().toLowerCase(), " ", "", false);
132                    TimeZone tz = (TimeZone) IDS.get(strTimezone);
133                    if(tz!=null) return tz;
134                    
135                    //parse GMT followd by a number
136                    float gmtOffset=Float.NaN;
137                    if(strTimezone.startsWith("gmt")) gmtOffset=getGMTOffset(strTimezone.substring(3).trim(),Float.NaN);
138                    else if(strTimezone.startsWith("etc/gmt")) gmtOffset=getGMTOffset(strTimezone.substring(7).trim(),Float.NaN);
139                    else if(strTimezone.startsWith("utc")) gmtOffset=getGMTOffset(strTimezone.substring(3).trim(),Float.NaN);
140                    else if(strTimezone.startsWith("etc/utc")) gmtOffset=getGMTOffset(strTimezone.substring(7).trim(),Float.NaN);
141                    
142    
143                    
144                    if(!Float.isNaN(gmtOffset)) {
145                            strTimezone="etc/gmt"+(gmtOffset>=0?"+":"")+Caster.toString(gmtOffset);
146                            tz = (TimeZone) IDS.get(strTimezone);
147                            if(tz!=null) return tz;
148                            
149                    }
150                    
151                    
152                    
153                    
154                    return defaultValue;
155            }
156            
157            private static float getGMTOffset(String str, float defaultValue) {
158                    int index;
159                    String left=null,right=null;
160                    if((index=str.indexOf(':'))!=-1) {
161                            left = str.substring(0,index);
162                            right=str.substring(index+1);
163                    }
164                    else if(str.startsWith("-")) {
165                            if(str.length()>=4 && str.indexOf('.')==-1){
166                                    left = str.substring(0,str.length()-2);
167                                    right=str.substring(str.length()-2);
168                            }
169                    }
170                    else if(str.length()>=3 && str.indexOf('.')==-1) {
171                            left = str.substring(0,str.length()-2);
172                            right=str.substring(str.length()-2);
173                    }
174                    if(left!=null) {
175                            int l = Caster.toIntValue(left,Integer.MIN_VALUE);
176                            int r = Caster.toIntValue(right,Integer.MIN_VALUE);
177                            if(l==Integer.MIN_VALUE || r==Integer.MIN_VALUE || r>59) return defaultValue;
178                            return l+(r/60f);
179                    }
180                    
181                    
182                    float f=Caster.toFloatValue(str,Float.NaN);
183                    if(Float.isNaN(f)) return defaultValue;
184                    return f;
185            }
186    
187            public static TimeZone toTimeZone(String strTimezone) throws ExpressionException{
188                    TimeZone tz = toTimeZone(strTimezone, null);
189                    if(tz!=null) return tz;
190                    throw new ExpressionException("can't cast value ("+strTimezone+") to a TimeZone","supported TimeZones are:"+getSupportedTimeZonesAsString());
191            }
192    }