Skip to content

Commit

Permalink
Added apoc.date.convert() and apoc.date.add() and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
InverseFalcon committed Feb 21, 2017
1 parent c1ffee1 commit 1329d6e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/apoc/date/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/apoc/date/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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<String, Object> 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"));
Expand Down

0 comments on commit 1329d6e

Please sign in to comment.