Skip to content

Commit ab6de8c

Browse files
Temporal Parse deprecations
1 parent d64c654 commit ab6de8c

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

core/src/main/java/apoc/date/Date.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public double toYears(
8888
}
8989

9090
@UserFunction("apoc.date.fields")
91+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_5)
9192
@Description("Splits the given date into fields returning a `MAP` containing the values of each field.")
92-
public Map<String, Object> fields(
93+
public Map<String, Object> fieldsCypher5(
9394
final @Name(value = "date", description = "A string representation of a temporal value.") String date,
9495
final @Name(
9596
value = "pattern",
@@ -109,6 +110,30 @@ public Map<String, Object> fields(
109110
return result.asMap();
110111
}
111112

113+
@Deprecated
114+
@UserFunction(value = "apoc.date.fields", deprecatedBy = "Cypher's temporal pattern constructors.")
115+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_25)
116+
@Description("Splits the given date into fields returning a `MAP` containing the values of each field.")
117+
public Map<String, Object> fields(
118+
final @Name(value = "date", description = "A string representation of a temporal value.") String date,
119+
final @Name(
120+
value = "pattern",
121+
defaultValue = DEFAULT_FORMAT,
122+
description = "The format the given temporal is formatted as.") String pattern) {
123+
if (date == null) {
124+
return Util.map();
125+
}
126+
DateTimeFormatter fmt = getSafeDateTimeFormatter(pattern);
127+
TemporalAccessor temporal = fmt.parse(date);
128+
FieldResult result = new FieldResult();
129+
130+
for (final TemporalQuery<Consumer<FieldResult>> query : DT_FIELDS_SELECTORS) {
131+
query.queryFrom(temporal).accept(result);
132+
}
133+
134+
return result.asMap();
135+
}
136+
112137
@UserFunction("apoc.date.field")
113138
@QueryLanguageScope(scope = {QueryLanguage.CYPHER_5})
114139
@Description("Returns the value of one field from the given date time.")
@@ -278,8 +303,9 @@ public Long fromISO8601(final @Name(value = "time", description = "The datetime
278303
}
279304

280305
@UserFunction("apoc.date.parse")
306+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_5)
281307
@Description("Parses the given date `STRING` from a specified format into the specified time unit.")
282-
public Long parse(
308+
public Long parseCypher5(
283309
@Name(value = "time", description = "The datetime to convert.") String time,
284310
@Name(value = "unit", defaultValue = "ms", description = "The conversion unit.") String unit,
285311
@Name(value = "format", defaultValue = DEFAULT_FORMAT, description = "The format the given datetime is in.")
@@ -290,6 +316,21 @@ public Long parse(
290316
return value == null ? null : unit(unit).convert(value, TimeUnit.MILLISECONDS);
291317
}
292318

319+
@Deprecated
320+
@UserFunction(value = "apoc.date.parse", deprecatedBy = "Cypher's temporal pattern constructors.")
321+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_25)
322+
@Description("Parses the given date `STRING` from a specified format into the specified time unit.")
323+
public Long parse(
324+
@Name(value = "time", description = "The datetime to convert.") String time,
325+
@Name(value = "unit", defaultValue = "ms", description = "The conversion unit.") String unit,
326+
@Name(value = "format", defaultValue = DEFAULT_FORMAT, description = "The format the given datetime is in.")
327+
String format,
328+
final @Name(value = "timezone", defaultValue = "", description = "The timezone the given datetime is in.")
329+
String timezone) {
330+
Long value = StringUtils.isBlank(time) ? null : parseOrThrow(time, getFormat(format, timezone));
331+
return value == null ? null : unit(unit).convert(value, TimeUnit.MILLISECONDS);
332+
}
333+
293334
@UserFunction("apoc.date.systemTimezone")
294335
@Description("Returns the display name of the system time zone (e.g. Europe/London).")
295336
public String systemTimezone() {
@@ -306,8 +347,9 @@ public Long convert(
306347
}
307348

308349
@UserFunction("apoc.date.convertFormat")
350+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_5)
309351
@Description("Converts a `STRING` of one type of date format into a `STRING` of another type of date format.")
310-
public String convertFormat(
352+
public String convertFormatCypher5(
311353
@Name(value = "temporal", description = "A string representation of a temporal value.") String input,
312354
@Name(value = "currentFormat", description = "The format the given temporal is formatted as.")
313355
String currentFormat,
@@ -326,6 +368,29 @@ public String convertFormat(
326368
return convertToFormatter.format(currentFormatter.parse(input));
327369
}
328370

371+
@Deprecated
372+
@UserFunction(value = "apoc.date.convertFormat", deprecatedBy = "Cypher's temporal pattern constructors and format() function.")
373+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_25)
374+
@Description("Converts a `STRING` of one type of date format into a `STRING` of another type of date format.")
375+
public String convertFormat(
376+
@Name(value = "temporal", description = "A string representation of a temporal value.") String input,
377+
@Name(value = "currentFormat", description = "The format the given temporal is formatted as.")
378+
String currentFormat,
379+
@Name(
380+
value = "convertTo",
381+
defaultValue = "yyyy-MM-dd",
382+
description = "The format to convert the given temporal value to.")
383+
String convertTo) {
384+
if (input == null || input.isEmpty()) {
385+
return null;
386+
}
387+
388+
DateTimeFormatter currentFormatter = DateFormatUtil.getOrCreate(currentFormat);
389+
DateTimeFormatter convertToFormatter = DateFormatUtil.getOrCreate(convertTo);
390+
391+
return convertToFormatter.format(currentFormatter.parse(input));
392+
}
393+
329394
@UserFunction("apoc.date.add")
330395
@Description("Adds a unit of specified time to the given timestamp.")
331396
public Long add(

core/src/main/java/apoc/temporal/TemporalProcedures.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ public String formatDuration(
157157
}
158158

159159
@UserFunction("apoc.temporal.toZonedTemporal")
160+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_5)
160161
@Description("Parses the given date `STRING` using the specified format into the given time zone.")
161-
public ZonedDateTime toZonedTemporal(
162+
public ZonedDateTime toZonedTemporalCypher5(
162163
@Name(value = "time", description = "The date string to be parsed.") String time,
163164
@Name(value = "format", defaultValue = DEFAULT_FORMAT, description = "The format of the given date string.")
164165
String format,
@@ -167,4 +168,18 @@ public ZonedDateTime toZonedTemporal(
167168
Long value = parseOrThrow(time, getFormat(format, timezone));
168169
return value == null ? null : Instant.ofEpochMilli(value).atZone(ZoneId.of(timezone));
169170
}
171+
172+
@Deprecated
173+
@UserFunction(value = "apoc.temporal.toZonedTemporal", deprecatedBy = "Cypher's temporal pattern constructors.")
174+
@QueryLanguageScope(scope = QueryLanguage.CYPHER_25)
175+
@Description("Parses the given date `STRING` using the specified format into the given time zone.")
176+
public ZonedDateTime toZonedTemporal(
177+
@Name(value = "time", description = "The date string to be parsed.") String time,
178+
@Name(value = "format", defaultValue = DEFAULT_FORMAT, description = "The format of the given date string.")
179+
String format,
180+
final @Name(value = "timezone", defaultValue = "UTC", description = "The timezone the given string is in.")
181+
String timezone) {
182+
Long value = parseOrThrow(time, getFormat(format, timezone));
183+
return value == null ? null : Instant.ofEpochMilli(value).atZone(ZoneId.of(timezone));
184+
}
170185
}

0 commit comments

Comments
 (0)