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 monthasstring
021 */
022package lucee.runtime.functions.string;
023
024import java.text.DateFormatSymbols;
025import java.util.Calendar;
026import java.util.Date;
027import java.util.Locale;
028
029import lucee.commons.date.JREDateTimeUtil;
030import lucee.runtime.PageContext;
031import lucee.runtime.exp.ExpressionException;
032import lucee.runtime.ext.function.Function;
033import lucee.runtime.i18n.LocaleFactory;
034
035public final class MonthAsString implements Function {
036        
037        private static final int MONTH=1000*60*60*24*32;
038        private static Date[] dates=new Date[12];
039        static {
040                Calendar cal=JREDateTimeUtil.getThreadCalendar();
041                cal.setTimeInMillis(0);
042                dates[0]=cal.getTime();
043                for(int i=1;i<12;i++) {
044                        cal.add(Calendar.MONTH,1);
045                        dates[i]=cal.getTime();
046                }
047        }
048
049        public static String call(PageContext pc , double month) throws ExpressionException {
050                return call(month, pc.getLocale());
051        }
052        public static String call(PageContext pc , double month, String strLocale) throws ExpressionException {
053                return call(month, strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale));
054        }
055        
056        private static String call(double month, Locale locale) throws ExpressionException {
057                int m=(int)month;
058                if(m>=1 && m<=12) {
059                        return new DateFormatSymbols(locale).getMonths()[m-1];
060                }
061                throw new ExpressionException("invalid month definition in function monthAsString, must be between 1 and 12 now ["+month+"]");
062                
063        }
064}