This is an automated email from the ASF dual-hosted git repository.
mboehm7 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/systemds.git
The following commit(s) were added to refs/heads/main by this push:
new 7fc5b4fefa [SYSTEMDS-3211] Extended util date functions
7fc5b4fefa is described below
commit 7fc5b4fefa0380ca4b8c1e5de264a7091e8e0496
Author: Anže Jenšterle <[email protected]>
AuthorDate: Sat May 28 20:01:29 2022 +0200
[SYSTEMDS-3211] Extended util date functions
Added an overloaded toMillis that accepts a predetermined date format,
introduced dateFormat() that takes either a string (one version tries to
parse date, other allows inputting a date format) or a long containing a
date and outputs in a specified format.
DIA project WS21/22.
Closes #1515.
---
pom.xml | 2 +-
.../apache/sysds/runtime/util/UtilFunctions.java | 33 +++++++++++++++++++---
2 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/pom.xml b/pom.xml
index 12d33123e1..9b985241e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,7 +40,7 @@
<properties>
<hadoop.version>3.3.1</hadoop.version>
- <!-- Consistant with spark -->
+ <!-- Consistent with spark -->
<antlr.version>4.8</antlr.version>
<protobuf.version>3.20.1</protobuf.version>
<spark.version>3.2.0</spark.version>
diff --git a/src/main/java/org/apache/sysds/runtime/util/UtilFunctions.java
b/src/main/java/org/apache/sysds/runtime/util/UtilFunctions.java
index cb7d19b8e6..3f656e20f0 100644
--- a/src/main/java/org/apache/sysds/runtime/util/UtilFunctions.java
+++ b/src/main/java/org/apache/sysds/runtime/util/UtilFunctions.java
@@ -858,15 +858,40 @@ public class UtilFunctions {
put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
}};
- public static long toMillis (String dateString) {
- long value = 0;
+ public static long toMillis(String dateString) {
+ return toMillis(dateString, getDateFormat(dateString));
+ }
+
+ public static long toMillis(String dateString, String dateFormat) {
try {
- value = new
SimpleDateFormat(getDateFormat(dateString)).parse(dateString).getTime();
+ return new
SimpleDateFormat(dateFormat).parse(dateString).getTime();
}
catch(ParseException e) {
throw new DMLRuntimeException(e);
}
- return value ;
+ }
+
+ public static String dateFormat(String dateString, String outputFormat)
{
+ try {
+ return dateFormat(dateString,
getDateFormat(dateString), outputFormat);
+ }
+ catch(NullPointerException e) {
+ throw new DMLRuntimeException(e);
+ }
+ }
+
+ public static String dateFormat(String dateString, String inputFormat,
String outputFormat) {
+ try {
+ Date value = new
SimpleDateFormat(inputFormat).parse(dateString);
+ return new SimpleDateFormat(outputFormat).format(value);
+ }
+ catch(ParseException e) {
+ throw new DMLRuntimeException(e);
+ }
+ }
+
+ public static String dateFormat(long date, String outputFormat) {
+ return new SimpleDateFormat(outputFormat).format(new
Date(date));
}
public static String[] copyAsStringToArray(String[] input, Object
value) {