001    /**
002     * Implements the CFML Function gettickcount
003     */
004    package railo.runtime.functions.other;
005    
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.PageContext;
008    import railo.runtime.exp.FunctionException;
009    import railo.runtime.ext.function.Function;
010    
011    public final class GetTickCount implements Function {
012    
013            private static final long serialVersionUID = 678332662578928144L;
014    
015            public static double UNIT_NANO=1;
016            public static double UNIT_MILLI=2;
017            public static double UNIT_MICRO=4;
018            public static double UNIT_SECOND=8;
019            
020            public static double call(PageContext pc) {
021                    return System.currentTimeMillis();
022            }
023            public static double call(PageContext pc,String unit) throws FunctionException {
024                    unit=unit.trim();
025                    if (!StringUtil.isEmpty( unit )) {
026                char c = unit.charAt( 0 );
027    
028                if ( c == 'n' || c == 'N' )
029                    return System.nanoTime();
030                else if ( c == 'm' || c == 'M' ) {
031                    if("micro".equalsIgnoreCase(unit)) return System.nanoTime()/1000;
032                            return System.currentTimeMillis();
033                }
034                else if ( c == 's' || c == 'S' )
035                    return System.currentTimeMillis()/1000;
036            }
037    
038                    throw new FunctionException(pc, "GetTickCount", 1, "unit", "invalid value ["+unit+"], valid values are (nano, micro, milli, second)");
039            }
040    
041            
042            // this function is only called when the evaluator validates the unit defintion on compilation time
043            public static double call(PageContext pc,double unit) {
044                    if(UNIT_NANO==unit) return System.nanoTime();
045                    if(UNIT_MICRO==unit) return System.nanoTime()/1000;
046                    if(UNIT_MILLI==unit) return System.currentTimeMillis();
047                    return System.currentTimeMillis()/1000;
048            }
049    }