Skip to content

Commit

Permalink
feat: Add fromdateiso8601 and todateiso8601 (#379)
Browse files Browse the repository at this point in the history
* feat: Add fromdateiso8601 and todateiso8601

* fix: input type

* fix: typo

* fix: fromdateiso8601

* feat: simplify todateiso8601

* refactor: consistent naming
  • Loading branch information
yann-combarnous authored Nov 25, 2024
1 parent 7a91de1 commit b7954b4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
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);
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- q: 'fromdateiso8601(.)'
in: "2023-09-30T00:00:00Z"
out:
- 1696032000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- q: 'todateiso8601(.)'
in: 1696032000
out:
- "2023-09-30T00:00:00Z"

0 comments on commit b7954b4

Please sign in to comment.