Skip to content

Commit

Permalink
MONDRIAN - added new CurrentDateString UDF
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//open/mondrian/": change = 7116]
  • Loading branch information
Zelaine Fong committed Jul 5, 2006
1 parent 8257e51 commit c8b154e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
@@ -1,3 +1,4 @@
mondrian.udf.ValUdf
mondrian.udf.InverseNormalUdf
mondrian.udf.LastNonEmptyUdf
mondrian.udf.CurrentDateStringUdf
69 changes: 69 additions & 0 deletions src/main/mondrian/udf/CurrentDateStringUdf.java
@@ -0,0 +1,69 @@
/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2006-2006 Julian Hyde and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.udf;

import mondrian.olap.Evaluator;
import mondrian.olap.Syntax;
import mondrian.olap.type.StringType;
import mondrian.olap.type.Type;
import mondrian.spi.UserDefinedFunction;
import mondrian.util.*;

import java.util.*;

/**
* User-defined function CurrentDateString, which returns the current date
* value as a formatted string, based on a format string passed in as a
* parameter. The format string conforms to the format string implemented
* by {@link Format}.
*
* @author Zelaine Fong
*/
public class CurrentDateStringUdf implements UserDefinedFunction {

public Object execute(Evaluator evaluator, Argument[] arguments) {
Object arg = arguments[0].evaluateScalar(evaluator);

if (!(arg instanceof String)) {
return null;
}
final Locale locale = Locale.getDefault();
final Format format = new Format((((String) arg).toString()), locale);
Date currDate = new Date();
return format.format(currDate);
}

public String getDescription() {
return "Returns current date as a formatted string";
}

public String getName() {
return "CurrentDateString";
}

public Type[] getParameterTypes() {
return new Type[] { new StringType() };
}

public String[] getReservedWords() {
return null;
}

public Type getReturnType(Type[] parameterTypes) {
return new StringType();
}

public Syntax getSyntax() {
return Syntax.Function;
}

}

// End CurrentDateStringUdf.java
11 changes: 11 additions & 0 deletions testsrc/main/mondrian/test/UdfTest.java
Expand Up @@ -14,6 +14,7 @@
import mondrian.olap.type.Type;
import mondrian.spi.UserDefinedFunction;

import java.util.*;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -218,6 +219,16 @@ public void testException() {
Pattern.compile(".*Invalid value for inverse normal distribution: 1.4435.*"),
cell.getValue().toString());
}

public void testCurrentDateString()
{
String actual = executeExpr("CurrentDateString(\"Ddd mmm dd yyyy\")");
Date currDate = new Date();
String dateString = currDate.toString();
String expected =
dateString.substring(0, 11) + dateString.substring(24);
assertEquals(expected, actual);
}

/**
* Dynamic schema which adds two user-defined functions to any given
Expand Down
9 changes: 9 additions & 0 deletions testsrc/main/mondrian/xmla/XmlaBasicTest.ref.xml
Expand Up @@ -4084,6 +4084,15 @@
<INTERFACE_NAME/>
<CAPTION>Crossjoin</CAPTION>
</row>
<row>
<FUNCTION_NAME>CurrentDateString</FUNCTION_NAME>
<DESCRIPTION>Returns current date as a formatted string</DESCRIPTION>
<PARAMETER_LIST>(none)</PARAMETER_LIST>
<RETURN_TYPE>1</RETURN_TYPE>
<ORIGIN>1</ORIGIN>
<INTERFACE_NAME/>
<CAPTION>CurrentDateString</CAPTION>
</row>
<row>
<FUNCTION_NAME>CurrentMember</FUNCTION_NAME>
<DESCRIPTION>Returns the current member along a
Expand Down

0 comments on commit c8b154e

Please sign in to comment.