-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add fromdateiso8601 and todateiso8601 (#379)
* feat: Add fromdateiso8601 and todateiso8601 * fix: input type * fix: typo * fix: fromdateiso8601 * feat: simplify todateiso8601 * refactor: consistent naming
- Loading branch information
1 parent
7a91de1
commit b7954b4
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...n-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/FromDateIso8601Function.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.thisptr.jackson.jq.internal.functions; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeType; | ||
import com.google.auto.service.AutoService; | ||
import net.thisptr.jackson.jq.*; | ||
import net.thisptr.jackson.jq.exception.JsonQueryException; | ||
import net.thisptr.jackson.jq.internal.misc.Preconditions; | ||
import net.thisptr.jackson.jq.internal.misc.JsonNodeUtils; | ||
import net.thisptr.jackson.jq.path.Path; | ||
|
||
import java.time.Instant; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.List; | ||
|
||
@AutoService(Function.class) | ||
@BuiltinFunction({ "fromdateiso8601/0" }) | ||
public class FromDateIso8601Function implements Function { | ||
@Override | ||
public void apply(final Scope scope, final List<Expression> args, final JsonNode in, final Path ipath, final PathOutput output, final Version version) throws JsonQueryException { | ||
Preconditions.checkInputType("fromdateiso8601", in, JsonNodeType.STRING); | ||
try { | ||
String iso8601String = in.asText(); | ||
long epochSeconds = Instant.parse(iso8601String).getEpochSecond(); | ||
output.emit(JsonNodeUtils.asNumericNode(epochSeconds), null); | ||
} catch (DateTimeParseException e) { | ||
throw new JsonQueryException(e); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...son-jq/src/main/java/net/thisptr/jackson/jq/internal/functions/ToDateIso8601Function.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.thisptr.jackson.jq.internal.functions; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeType; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import com.google.auto.service.AutoService; | ||
import net.thisptr.jackson.jq.*; | ||
import net.thisptr.jackson.jq.exception.JsonQueryException; | ||
import net.thisptr.jackson.jq.internal.misc.Preconditions; | ||
import net.thisptr.jackson.jq.path.Path; | ||
|
||
import java.time.DateTimeException; | ||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
@AutoService(Function.class) | ||
@BuiltinFunction({ "todateiso8601/0" }) | ||
public class ToDateIso8601Function implements Function { | ||
@Override | ||
public void apply(final Scope scope, final List<Expression> args, final JsonNode in, final Path ipath, final PathOutput output, final Version version) throws JsonQueryException { | ||
Preconditions.checkInputType("todateiso8601", in, JsonNodeType.NUMBER); | ||
try { | ||
long epochSeconds = in.asLong(); | ||
String iso8601String = Instant.ofEpochSecond(epochSeconds).toString(); | ||
output.emit(new TextNode(iso8601String), null); | ||
} catch (DateTimeException e) { | ||
throw new JsonQueryException(e); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
jackson-jq/src/test/resources/tests/functions/fromdateiso8601.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- q: 'fromdateiso8601(.)' | ||
in: "2023-09-30T00:00:00Z" | ||
out: | ||
- 1696032000 |
4 changes: 4 additions & 0 deletions
4
jackson-jq/src/test/resources/tests/functions/todateiso8601.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- q: 'todateiso8601(.)' | ||
in: 1696032000 | ||
out: | ||
- "2023-09-30T00:00:00Z" |