001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2007 Pentaho and others
008// All Rights Reserved.
009*/
010package mondrian.udf;
011
012import mondrian.olap.Evaluator;
013import mondrian.olap.Syntax;
014import mondrian.olap.type.StringType;
015import mondrian.olap.type.Type;
016import mondrian.spi.UserDefinedFunction;
017import mondrian.util.Format;
018
019import java.util.Date;
020import java.util.Locale;
021
022/**
023 * User-defined function <code>CurrentDateString<code>, which returns the
024 * current date value as a formatted string, based on a format string passed in
025 * as a parameter.  The format string conforms to the format string implemented
026 * by {@link Format}.
027 *
028 * @author Zelaine Fong
029 */
030public class CurrentDateStringUdf implements UserDefinedFunction {
031
032    public Object execute(Evaluator evaluator, Argument[] arguments) {
033        Object arg = arguments[0].evaluateScalar(evaluator);
034
035        final Locale locale = Locale.getDefault();
036        final Format format = new Format((String) arg, locale);
037        Date currDate = evaluator.getQueryStartTime();
038        return format.format(currDate);
039    }
040
041    public String getDescription() {
042        return "Returns the current date formatted as specified by the format "
043            + "parameter.";
044    }
045
046    public String getName() {
047        return "CurrentDateString";
048    }
049
050    public Type[] getParameterTypes() {
051        return new Type[] { new StringType() };
052    }
053
054    public String[] getReservedWords() {
055        return null;
056    }
057
058    public Type getReturnType(Type[] parameterTypes) {
059        return new StringType();
060    }
061
062    public Syntax getSyntax() {
063        return Syntax.Function;
064    }
065
066}
067
068// End CurrentDateStringUdf.java