You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/best-practices.adoc
+222Lines changed: 222 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -381,3 +381,225 @@ following one:
381
381
@JsonAnySetter
382
382
private Map<String, JsonNode> additionalProperties = new HashMap<>();
383
383
----
384
+
385
+
[[implementing-intervals]]
386
+
== Implementing Intervals in RESTful APIs
387
+
388
+
Unfortunately the out-of-the-box library support for open intervals in most
389
+
programming languages is very limited. As a consequence, you will likely need
390
+
to implement your own parser for ISO 8601 compliant intervals, which also supports open boundaries using `..`. The following sections provide example implementations for Java/Kotlin/Scala, Go, and Python.
391
+
392
+
[[intervals-java]]
393
+
=== In Java/Kotlin/Scala
394
+
395
+
In Java/Kotlin/Scala you can use the Time4J library, which provides
396
+
a powerful and flexible API for working with date and time, including support
397
+
for intervals. However, it does not provide a built-in parser for ISO 8601 compliant intervals, but a custom format using `-` to signal open ends.
398
+
399
+
A common approach to implement ISO 8601 compliant interval parsing is to use regular expressions to identify and transform the input string into the format expected by Time4J. Below is an example implementation of such a parser:
400
+
401
+
[source,java]
402
+
----
403
+
import java.util.regex.Pattern;
404
+
import net.time4j.range.MomentInterval;
405
+
import org.slf4j.Logger;
406
+
import org.slf4j.LoggerFactory;
407
+
408
+
public class IntervalParser {
409
+
private static final Logger log =
410
+
LoggerFactory.getLogger(IntervalParser.class);
411
+
412
+
// Pre-compile the patterns for performance
413
+
private static final Pattern ISO_INCOMPLIANT_INTERVAL =
414
+
Pattern.compile("(^-/|/-$)");
415
+
private static final Pattern ISO_OPEN_START_INTERVAL =
416
+
Pattern.compile("^(\\.\\.)?/");
417
+
private static final Pattern ISO_OPEN_END_INTERVAL =
418
+
Pattern.compile("/(\\.\\.)?$");
419
+
420
+
public MomentInterval parseDateTimeInterval(String interval) {
421
+
if (ISO_INCOMPLIANT_INTERVAL.matcher(interval).find()) {
0 commit comments