From 1329d6ee14a2cbc79024ca5f65de76b85cf740f0 Mon Sep 17 00:00:00 2001 From: InverseFalcon Date: Mon, 20 Feb 2017 22:12:00 -0800 Subject: [PATCH] Added apoc.date.convert() and apoc.date.add() and unit tests --- src/main/java/apoc/date/Date.java | 13 +++++++++++++ src/test/java/apoc/date/DateTest.java | 28 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/main/java/apoc/date/Date.java b/src/main/java/apoc/date/Date.java index 105672430d..2d24ff3fcb 100644 --- a/src/main/java/apoc/date/Date.java +++ b/src/main/java/apoc/date/Date.java @@ -136,6 +136,19 @@ public String systemTimezone() { return TimeZone.getDefault().getID(); } + @UserFunction + @Description("apoc.date.convert(12345,'ms|s|m|h|d','ms|s|m|h|d') convert a time unit into a different time unit") + public Long convert(@Name("time") long time, @Name(value = "unit") String unit, @Name(value = "toUnit") String toUnit) { + return unit(toUnit).convert(time, unit(unit)); + } + + @UserFunction + @Description("apoc.date.add(12345,'ms|s|m|h|d', 54321, 'ms|s|m|h|d') adds a value of the specified time unit to the original value, outputing in the original value's time unit") + public Long add(@Name("time") long time, @Name(value = "unit") String unit, @Name(value = "addValue") long addValue, @Name(value = "addUnit") String addUnit) { + long valueToAdd = unit(unit).convert(addValue, unit(addUnit)); + return time + valueToAdd; + } + public String parse(final @Name("millis") long millis, final @Name(value = "pattern", defaultValue = DEFAULT_FORMAT) String pattern, final @Name("timezone") String timezone) { if (millis < 0) { throw new IllegalArgumentException("The time argument should be >= 0, got: " + millis); diff --git a/src/test/java/apoc/date/DateTest.java b/src/test/java/apoc/date/DateTest.java index fbc5ad031b..e0068f1f5b 100644 --- a/src/test/java/apoc/date/DateTest.java +++ b/src/test/java/apoc/date/DateTest.java @@ -7,6 +7,7 @@ import java.time.ZoneId; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; @@ -293,6 +294,33 @@ public void testGetTimezone() throws Exception { testCall(db, "RETURN apoc.date.systemTimezone() as tz", row -> assertEquals(TimeZone.getDefault().getID(), row.get("tz").toString())); } + @Test + public void testConvert() throws Exception { + Long firstOf2017ms = 1483228800000l; + Long firstOf2017d = 17167l; + Map params = new HashMap<>(); + params.put("firstOf2017ms", firstOf2017ms); + testCall(db, "RETURN apoc.date.convert($firstOf2017ms, 'ms', 'd') as firstOf2017days", params, row -> assertEquals(firstOf2017d, row.get("firstOf2017days"))); + } + + @Test + public void testAdd() throws Exception { + Long firstOf2017ms = 1483228800000l; + Long firstOf2017Plus5Daysms = 1483660800000l; + Map params = new HashMap<>(); + params.put("firstOf2017ms", firstOf2017ms); + testCall(db, "RETURN apoc.date.add($firstOf2017ms, 'ms', 5, 'd') as firstOf2017Plus5days", params, row -> assertEquals(firstOf2017Plus5Daysms, row.get("firstOf2017Plus5days"))); + } + + @Test + public void testAddNegative() throws Exception { + Long firstOf2017ms = 1483228800000l; + Long firstOf2017Minus5Daysms = 1482796800000l; + Map params = new HashMap<>(); + params.put("firstOf2017ms", firstOf2017ms); + testCall(db, "RETURN apoc.date.add($firstOf2017ms, 'ms', -5, 'd') as firstOf2017Minus5days", params, row -> assertEquals(firstOf2017Minus5Daysms, row.get("firstOf2017Minus5days"))); + } + private SimpleDateFormat formatInUtcZone(final String pattern) { SimpleDateFormat customFormat = new SimpleDateFormat(pattern); customFormat.setTimeZone(TimeZone.getTimeZone("UTC"));