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 dayofweekasstring
021 */
022package lucee.runtime.functions.string;
023
024import java.util.Date;
025import java.util.Locale;
026
027import lucee.commons.date.TimeZoneConstants;
028import lucee.commons.i18n.DateFormatPool;
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.ExpressionException;
031import lucee.runtime.exp.FunctionException;
032import lucee.runtime.ext.function.Function;
033import lucee.runtime.i18n.LocaleFactory;
034
035public final class DayOfWeekAsString implements Function {
036
037        private static final long serialVersionUID = 4067032942689404733L;
038        private static final int DAY=1000*60*60*24;
039
040        private static Date[] dates=new Date[]{
041                        new Date(0+(3*DAY)),
042                        new Date(0+(4*DAY)),
043                        new Date(0+(5*DAY)),
044                        new Date(0+(6*DAY)),
045                        new Date(0),
046                        new Date(0+(1*DAY)),
047                        new Date(0+(2*DAY))
048        };
049        
050        public static String call(PageContext pc , double dow) throws ExpressionException {
051                return call(pc,dow, pc.getLocale(),true);
052        }
053        public static String call(PageContext pc , double dow, String strLocale) throws ExpressionException {
054                return call(pc,dow, strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),true);
055        }
056        protected static String call(PageContext pc , double dow, Locale locale,boolean _long) throws ExpressionException {
057                
058                int dayOfWeek=(int)dow;
059                if(dayOfWeek>=1 && dayOfWeek<=7) {
060                    return DateFormatPool.format(locale,TimeZoneConstants.GMT0,_long?"EEEE":"EEE",dates[dayOfWeek-1]);
061                }
062                throw new FunctionException(
063                                pc,
064                                _long?"DayOfWeekAsString":"DayOfWeekShortAsString",
065                                1,"dayOfWeek",
066                                "must be between 1 and 7 now ["+dayOfWeek+"]");
067                //throw new ExpressionException("invalid dayOfWeek definition in function DayOfWeekAsString, must be between 1 and 7 now ["+dayOfWeek+"]");
068        }
069}