001 package railo.runtime.functions.international; 002 003 import java.text.DateFormat; 004 import java.text.ParseException; 005 import java.util.Locale; 006 import java.util.TimeZone; 007 008 import railo.commons.date.TimeZoneUtil; 009 import railo.commons.i18n.FormatUtil; 010 import railo.commons.lang.StringUtil; 011 import railo.runtime.PageContext; 012 import railo.runtime.exp.PageException; 013 import railo.runtime.ext.function.Function; 014 import railo.runtime.i18n.LocaleFactory; 015 import railo.runtime.op.date.DateCaster; 016 import railo.runtime.type.dt.DateTime; 017 import railo.runtime.type.dt.DateTimeImpl; 018 019 /** 020 * Implements the Cold Fusion Function dateformat 021 */ 022 public final class LSTimeFormat implements Function { 023 024 /** 025 * @param pc 026 * @param o 027 * @return 028 * @throws PageException 029 */ 030 public static String call(PageContext pc , Object o) throws PageException { 031 return _call(pc, o, "short", pc.getLocale(),pc.getTimeZone()); 032 } 033 034 public static String call(PageContext pc , Object o, String mask) throws PageException { 035 return _call(pc, o, mask, pc.getLocale(),pc.getTimeZone()); 036 } 037 public static String call(PageContext pc , Object o, String mask,String strLocale) throws PageException { 038 return _call(pc, o, mask, LocaleFactory.getLocale(strLocale),pc.getTimeZone()); 039 } 040 public static String call(PageContext pc , Object o, String mask,String strLocale,String strTimezone) throws PageException { 041 return _call(pc, o, mask, 042 strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale), 043 strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone)); 044 } 045 046 private static String _call(PageContext pc, Object o, String mask,Locale locale,TimeZone tz) throws PageException { 047 if(o instanceof String && StringUtil.isEmpty((String)o,true)) return ""; 048 return new railo.runtime.format.TimeFormat(locale).format(toTimeLS(locale, tz, o),mask,tz); 049 //return new railo.runtime.format.TimeFormat(locale).format(DateCaster.toDateAdvanced(o,pc.getTimeZone()),mask); 050 } 051 052 053 private static DateTime toTimeLS(Locale locale, TimeZone timeZone, Object object) throws PageException { 054 if(object instanceof String) { 055 String str=(String) object; 056 057 DateFormat[] formats=FormatUtil.getTimeFormats(locale,timeZone,true); 058 for(int i=0;i<formats.length;i++) { 059 try { 060 return new DateTimeImpl(formats[i].parse(str).getTime(),false); 061 } 062 catch (ParseException e) { 063 // 064 } 065 } 066 067 } 068 return DateCaster.toDateAdvanced(object,timeZone); 069 } 070 071 072 }