[PATCH] Accept number as argument in dc:date-format()

Wald Commits scm-commit at wald.intevation.org
Thu Oct 30 14:18:11 CET 2014


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1414675069 -3600
# Node ID 081a57c8054037a247bd3366cf200b485f7faf55
# Parent  c6db5f8cafbb8426fcaffb1a0d410eb8a95123c9
Accept number as argument in dc:date-format().

diff -r c6db5f8cafbb -r 081a57c80540 artifacts/doc/datacage_ref_de.rst
--- a/artifacts/doc/datacage_ref_de.rst	Thu Oct 30 14:10:41 2014 +0100
+++ b/artifacts/doc/datacage_ref_de.rst	Thu Oct 30 14:17:49 2014 +0100
@@ -707,7 +707,9 @@
 
 ``dc:date-format(Format, Datum)`` liefert für ein Datum einen String, der
 nach den Formatierungsregeln aus Format formatiert wird. Für die Formatierungen
-gelten die in SimpleFormat_ beschriebenen Regeln.
+gelten die in SimpleFormat_ beschriebenen Regeln. ``Datum`` kann dabei ein
+Datums-Objekt oder eine Zahl sein. Letztere wird als Millisekunden nach dem
+1.1.1970 00:00:00 GMT interpretiert.
 
 .. _SimpleFormat: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
 
diff -r c6db5f8cafbb -r 081a57c80540 artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Thu Oct 30 14:10:41 2014 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java	Thu Oct 30 14:17:49 2014 +0100
@@ -429,23 +429,33 @@
         Object date    = args.get(1);
 
         try {
-            // Oracle does not return a date object but an oracle.sql.TIMESTAMP
-            Method meth = date.getClass().getMethod("dateValue", new Class[] {});
-            date = meth.invoke(date, new Object [] {});
-        } catch (IllegalArgumentException e) {
-        } catch (IllegalAccessException e) {
-        } catch (InvocationTargetException e) {
-        } catch (NoSuchMethodException e) {
-        }
-        if (pattern instanceof String && date instanceof Date) {
+            // TODO: Take locale into account.
+            SimpleDateFormat format = new SimpleDateFormat((String)pattern);
+
+            if (date instanceof Number) {
+                return format.format(new Date(((Number)date).longValue()));
+            }
+
             try {
-                // TODO: Take locale into account.
-                return new SimpleDateFormat((String)pattern).format((Date)date);
+                /* Oracle does not return a date object but
+                   an oracle.sql.TIMESTAMP */
+                Method meth = date.getClass()
+                    .getMethod("dateValue", new Class[] {});
+                date = meth.invoke(date, new Object[] {});
+            } catch (IllegalArgumentException e) {
+            } catch (IllegalAccessException e) {
+            } catch (InvocationTargetException e) {
+            } catch (NoSuchMethodException e) {
             }
-            catch (IllegalArgumentException iae) {
-                throw new XPathFunctionException(iae);
+
+            if (date instanceof Date) {
+                return format.format((Date)date);
             }
         }
+        catch (IllegalArgumentException iae) {
+            log.error(iae.getMessage());
+        }
+
         return "";
     }
 


More information about the Dive4Elements-commits mailing list