andreachild commented on code in PR #3090:
URL: https://github.com/apache/tinkerpop/pull/3090#discussion_r2037766924
##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/DateAddStep.java:
##########
@@ -57,16 +48,40 @@ public DateAddStep(final Traversal.Admin traversal, final
DT dateToken, final in
}
@Override
- protected Date map(final Traverser.Admin<S> traverser) {
+ protected OffsetDateTime map(final Traverser.Admin<S> traverser) {
final Object object = traverser.get();
-
- if (!(object instanceof Date)) throw new
IllegalArgumentException("dateAdd accept only Date.");
-
- final Calendar cal = Calendar.getInstance();
- cal.setTime((Date) object);
- cal.add(DTtoCalendar.get(dateToken), value);
-
- return cal.getTime();
+ OffsetDateTime date;
+
+ if (!(object instanceof OffsetDateTime)) {
+ // allow incoming traverser to resolve into Date object for
compatibility
+ if (object instanceof Date) {
+ date = ((Date) object).toInstant().atOffset(ZoneOffset.UTC);
+ } else {
+ throw new IllegalArgumentException("dateAdd() accept only
OffsetDateTime or Date (deprecated).");
+ }
+ } else {
+ date = (OffsetDateTime) object;
+ }
+ OffsetDateTime newDate;
+
+ switch (dateToken) {
+ case second:
+ newDate = date.plus(Duration.ofSeconds(value));
+ break;
+ case minute:
+ newDate = date.plus(Duration.ofMinutes(value));
+ break;
+ case hour:
+ newDate = date.plus(Duration.ofHours(value));
+ break;
+ case day:
+ newDate = date.plus(Duration.ofDays(value));
+ break;
+ default:
+ throw new IllegalArgumentException("DT tokens should only be
second, minute, hour, or day.");
Review Comment:
Should this validation be moved to the constructor instead?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]