001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019/** 020 * Implements the CFML Function lsparsedatetime 021 */ 022package lucee.runtime.functions.international; 023 024import java.text.ParseException; 025import java.text.SimpleDateFormat; 026import java.util.Date; 027import java.util.Locale; 028import java.util.TimeZone; 029 030import lucee.commons.date.TimeZoneUtil; 031import lucee.commons.lang.StringUtil; 032import lucee.runtime.PageContext; 033import lucee.runtime.engine.ThreadLocalPageContext; 034import lucee.runtime.exp.PageException; 035import lucee.runtime.ext.function.Function; 036import lucee.runtime.i18n.LocaleFactory; 037import lucee.runtime.op.Caster; 038import lucee.runtime.op.date.DateCaster; 039import lucee.runtime.type.dt.DateTimeImpl; 040 041public final class LSParseDateTime implements Function { 042 043 private static final long serialVersionUID = 7808039073301229473L; 044 045 public static lucee.runtime.type.dt.DateTime call(PageContext pc , Object oDate) throws PageException { 046 return _call(pc, oDate, pc.getLocale(),pc.getTimeZone(),null); 047 } 048 049 public static lucee.runtime.type.dt.DateTime call(PageContext pc , Object oDate,String strLocale) throws PageException { 050 return _call(pc, oDate, LocaleFactory.getLocale(strLocale),pc.getTimeZone(),null); 051 } 052 053 public static lucee.runtime.type.dt.DateTime call(PageContext pc , Object oDate,String strLocale,String strTimezoneOrFormat) throws PageException { 054 if(strTimezoneOrFormat==null) { 055 return _call(pc, oDate, LocaleFactory.getLocale(strLocale),pc.getTimeZone(),null); 056 } 057 Locale locale = strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale); 058 TimeZone tz = TimeZoneUtil.toTimeZone(strTimezoneOrFormat,null); 059 if(tz!=null) 060 return _call(pc, oDate, locale,tz,null); 061 return _call(pc, oDate, locale,pc.getTimeZone(),strTimezoneOrFormat); 062 } 063 064 065 public static lucee.runtime.type.dt.DateTime call(PageContext pc , Object oDate,String strLocale,String strTimezone, String strFormat) throws PageException { 066 return _call(pc, oDate, 067 strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale), 068 strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone),strFormat); 069 } 070 071 private static lucee.runtime.type.dt.DateTime _call(PageContext pc , Object oDate,Locale locale,TimeZone tz, String format) throws PageException { 072 if(oDate instanceof Date) return Caster.toDate(oDate, tz); 073 074 String strDate = Caster.toString(oDate); 075 076 // regular parse date time 077 if(StringUtil.isEmpty(format,true)) 078 return DateCaster.toDateTime(locale,strDate,tz,locale.equals(Locale.US)); 079 080 081 // with java based format 082 tz=ThreadLocalPageContext.getTimeZone(tz); 083 if(locale==null)locale=pc.getLocale(); 084 SimpleDateFormat df = new SimpleDateFormat(format, locale); 085 df.setTimeZone(tz); 086 try { 087 return new DateTimeImpl(df.parse(strDate)); 088 } catch (ParseException e) { 089 throw Caster.toPageException(e); 090 } 091 } 092}