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.Caster; 016 import railo.runtime.op.date.DateCaster; 017 import railo.runtime.type.dt.DateTime; 018 import railo.runtime.type.dt.DateTimeImpl; 019 020 /** 021 * Implements the CFML Function dateformat 022 */ 023 public final class LSTimeFormat implements Function { 024 025 /** 026 * @param pc 027 * @param o 028 * @return 029 * @throws PageException 030 */ 031 public static String call(PageContext pc , Object o) throws PageException { 032 return _call(pc, o, "short", pc.getLocale(),pc.getTimeZone()); 033 } 034 035 public static String call(PageContext pc , Object o, String mask) throws PageException { 036 return _call(pc, o, mask, pc.getLocale(),pc.getTimeZone()); 037 } 038 public static String call(PageContext pc , Object o, String mask,String strLocale) throws PageException { 039 return _call(pc, o, mask, LocaleFactory.getLocale(strLocale),pc.getTimeZone()); 040 } 041 public static String call(PageContext pc , Object o, String mask,String strLocale,String strTimezone) throws PageException { 042 return _call(pc, o, mask, 043 strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale), 044 strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone)); 045 } 046 047 private static String _call(PageContext pc, Object o, String mask,Locale locale,TimeZone tz) throws PageException { 048 if(o instanceof String && StringUtil.isEmpty((String)o,true)) return ""; 049 return new railo.runtime.format.TimeFormat(locale).format(toTimeLS(locale, tz, o),mask,tz); 050 } 051 052 053 private static DateTime toTimeLS(Locale locale, TimeZone timeZone, Object object) throws PageException { 054 if(object instanceof DateTime) return (DateTime) object; 055 if(object instanceof CharSequence) { 056 String str=Caster.toString(object); 057 058 DateFormat[] formats=FormatUtil.getTimeFormats(locale,timeZone,true); 059 for(int i=0;i<formats.length;i++) { 060 try { 061 return new DateTimeImpl(formats[i].parse(str).getTime(),false); 062 } 063 catch (ParseException e) {} 064 } 065 066 } 067 return DateCaster.toDateAdvanced(object,timeZone); 068 } 069 070 071 }