001 /** 002 * Implements the Cold Fusion Function lsdateformat 003 */ 004 package railo.runtime.functions.international; 005 006 import java.util.Locale; 007 import java.util.TimeZone; 008 009 import railo.commons.date.TimeZoneUtil; 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 019 public final class LSDateFormat implements Function { 020 021 private static final long serialVersionUID = 4720003854756942610L; 022 023 //private static Calendar calendar; 024 public static String call(PageContext pc , Object object) throws PageException { 025 return _call(pc, object, "medium", pc.getLocale(),pc.getTimeZone()); 026 } 027 public static synchronized String call(PageContext pc , Object object, String mask) throws PageException { 028 return _call(pc, object, mask, pc.getLocale(),pc.getTimeZone()); 029 } 030 public static synchronized String call(PageContext pc , Object object, String mask,String strLocale) throws PageException { 031 return _call(pc, object, mask, LocaleFactory.getLocale(strLocale),pc.getTimeZone()); 032 } 033 public static synchronized String call(PageContext pc , Object object, String mask,String strLocale,String strTimezone) throws PageException { 034 return _call(pc, object, mask, 035 strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale), 036 strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone)); 037 } 038 039 040 private static synchronized String _call(PageContext pc , Object object, String mask,Locale locale,TimeZone tz) throws PageException { 041 if(StringUtil.isEmpty(object)) return ""; 042 043 return new railo.runtime.format.DateFormat(locale). 044 format(toDateLS(pc ,locale,tz, object),mask,tz); 045 } 046 047 private static DateTime toDateLS(PageContext pc ,Locale locale, TimeZone timeZone, Object object) throws PageException { 048 //print.out("oh:"+object); 049 DateTime res = Caster.toDateTime(locale,Caster.toString(object),timeZone,null,locale.equals(Locale.US)); 050 if(res!=null)return res; 051 return DateCaster.toDateAdvanced(object,timeZone); 052 053 /*try{ 054 return DateCaster.toDateAdvanced(object,timeZone); 055 } 056 catch(PageException pe){ 057 if(object instanceof String) { 058 String str=(String) object; 059 DateFormat[] formats=FormatUtil.getDateFormats(locale,true); 060 for(int i=0;i<formats.length;i++) { 061 try { 062 long t = formats[i].parse(str).getTime(); 063 return new DateTimeImpl(fixYear(timeZone,t),false); 064 } 065 catch (ParseException e) { 066 // 067 } 068 } 069 } 070 throw pe; 071 }*/ 072 } 073 074 /*private static long fixYear(TimeZone timezone,long time) { 075 if (calendar == null) 076 calendar=JREDateTimeUtil.newInstance(); 077 synchronized (calendar) { 078 calendar.clear(); 079 calendar.setTimeZone(timezone); 080 calendar.setTimeInMillis(time); 081 int year = calendar.get(Calendar.YEAR); 082 if(year<100) { 083 if(year<21)year=year+=2000; 084 else year=year+=1900; 085 calendar.set(Calendar.YEAR,year); 086 return calendar.getTimeInMillis(); 087 } 088 } 089 return time; 090 }*/ 091 }