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 gettickcount
021 */
022package lucee.runtime.functions.other;
023
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.FunctionException;
027import lucee.runtime.ext.function.Function;
028
029public final class GetTickCount implements Function {
030
031        private static final long serialVersionUID = 678332662578928144L;
032
033        public static double UNIT_NANO=1;
034        public static double UNIT_MILLI=2;
035        public static double UNIT_MICRO=4;
036        public static double UNIT_SECOND=8;
037        
038        public static double call(PageContext pc) {
039                return System.currentTimeMillis();
040        }
041        public static double call(PageContext pc,String unit) throws FunctionException {
042                unit=unit.trim();
043                if (!StringUtil.isEmpty( unit )) {
044            char c = unit.charAt( 0 );
045
046            if ( c == 'n' || c == 'N' )
047                return System.nanoTime();
048            else if ( c == 'm' || c == 'M' ) {
049                if("micro".equalsIgnoreCase(unit)) return System.nanoTime()/1000;
050                        return System.currentTimeMillis();
051            }
052            else if ( c == 's' || c == 'S' )
053                return System.currentTimeMillis()/1000;
054        }
055
056                throw new FunctionException(pc, "GetTickCount", 1, "unit", "invalid value ["+unit+"], valid values are (nano, micro, milli, second)");
057        }
058
059        
060        // this function is only called when the evaluator validates the unit defintion on compilation time
061        public static double call(PageContext pc,double unit) {
062                if(UNIT_NANO==unit) return System.nanoTime();
063                if(UNIT_MICRO==unit) return System.nanoTime()/1000;
064                if(UNIT_MILLI==unit) return System.currentTimeMillis();
065                return System.currentTimeMillis()/1000;
066        }
067}