generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 28
Adds DateTime Extension Support #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
muditchaudhary
merged 7 commits into
cedar-policy:main
from
muditchaudhary:4.4_release/features/datetime_extension
Aug 15, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
73fcd7c
adds DateTime extension value
muditchaudhary 2fb452f
adds serializer/deserializer for DateTime
muditchaudhary 7474f1a
adds DateTime tests
muditchaudhary 35eddf6
fixes nits
muditchaudhary 9648970
updates CHANGELOG
muditchaudhary 30d093b
updates DateTime to use semantic equality
muditchaudhary 072fbf4
fixes javadoc nits
muditchaudhary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
160 changes: 160 additions & 0 deletions
160
CedarJava/src/main/java/com/cedarpolicy/value/DateTime.java
This file contains hidden or 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,160 @@ | ||
/* | ||
* Copyright Cedar Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.cedarpolicy.value; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.format.ResolverStyle; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Represents a Cedar datetime extension value. DateTime values are encoded as strings in the | ||
* following formats (Follows ISO 8601 standard): | ||
* | ||
* "YYYY-MM-DD" (date only) | ||
* "YYYY-MM-DDThh:mm:ssZ" (UTC) | ||
* "YYYY-MM-DDThh:mm:ss.SSSZ" (UTC with millisecond precision) | ||
* "YYYY-MM-DDThh:mm:ss(+/-)hhmm" (With timezone offset in hours and minutes) | ||
* "YYYY-MM-DDThh:mm:ss.SSS(+/-)hhmm" (With timezone offset in hours and minutes and millisecond precision) | ||
* | ||
*/ | ||
public class DateTime extends Value { | ||
|
||
private static class DateTimeValidator { | ||
|
||
private static final List<DateTimeFormatter> FORMATTERS = Arrays.asList( | ||
DateTimeFormatter.ofPattern("uuuu-MM-dd").withResolverStyle(ResolverStyle.STRICT), | ||
DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss'Z'") | ||
.withResolverStyle(ResolverStyle.STRICT), | ||
DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'") | ||
.withResolverStyle(ResolverStyle.STRICT), | ||
DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXX") | ||
.withResolverStyle(ResolverStyle.STRICT), | ||
DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX") | ||
.withResolverStyle(ResolverStyle.STRICT)); | ||
|
||
/** | ||
* Parses a datetime string and returns the parsed Instant. Combines validation and parsing | ||
* into a single operation to avoid redundancy. All datetime formats are normalized to | ||
* Instant for consistent equality comparison. | ||
* | ||
* @param dateTimeString the string to parse | ||
* @return Optional containing the parsed Instant, or empty if parsing fails | ||
*/ | ||
private static Optional<Instant> parseToInstant(String dateTimeString) { | ||
if (dateTimeString == null || dateTimeString.trim().isEmpty()) { | ||
return java.util.Optional.empty(); | ||
} | ||
|
||
return FORMATTERS.stream() | ||
.flatMap(formatter -> tryParseWithFormatter(dateTimeString, formatter).stream()) | ||
.findFirst(); | ||
} | ||
|
||
/** | ||
* Attempts to parse a datetime string with a specific formatter. | ||
* | ||
* @param dateTimeString the string to parse | ||
* @param formatter the formatter to use | ||
* @return Optional containing the parsed Instant, or empty if parsing fails | ||
*/ | ||
private static Optional<Instant> tryParseWithFormatter(String dateTimeString, | ||
DateTimeFormatter formatter) { | ||
try { | ||
if (formatter == FORMATTERS.get(0)) { | ||
// Date-only format - convert to start of day UTC | ||
LocalDate date = LocalDate.parse(dateTimeString, formatter); | ||
return Optional.of(date.atStartOfDay(ZoneOffset.UTC).toInstant()); | ||
} else { | ||
// DateTime format - parse and convert to Instant | ||
OffsetDateTime dateTime = OffsetDateTime.parse(dateTimeString, formatter); | ||
return Optional.of(dateTime.toInstant()); | ||
} | ||
} catch (DateTimeParseException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
} | ||
|
||
/** Datetime as a string. */ | ||
private final String dateTime; | ||
|
||
/** Parsed datetime as Instant for semantic comparison. */ | ||
private final Instant parsedInstant; | ||
|
||
/** | ||
* Construct DateTime. | ||
* | ||
* @param dateTime DateTime as a String. | ||
*/ | ||
@SuppressFBWarnings("CT_CONSTRUCTOR_THROW") | ||
public DateTime(String dateTime) throws NullPointerException, IllegalArgumentException { | ||
Optional<Instant> parsed = DateTimeValidator.parseToInstant(dateTime); | ||
if (parsed.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
"Input string is not a supported DateTime format: " + dateTime); | ||
} else { | ||
this.dateTime = dateTime; | ||
this.parsedInstant = parsed.get(); | ||
} | ||
} | ||
|
||
/** Convert DateTime to Cedar expr that can be used in a Cedar policy. */ | ||
@Override | ||
public String toCedarExpr() { | ||
return "datetime(\"" + dateTime + "\")"; | ||
} | ||
|
||
/** | ||
* Equals based on semantic comparison of the parsed datetime values. Two DateTime objects are | ||
* equal if they represent the same instant in time, regardless of their string representation | ||
* format. | ||
*/ | ||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
DateTime other = (DateTime) o; | ||
return Objects.equals(this.parsedInstant, other.parsedInstant); | ||
} | ||
|
||
/** | ||
* Hash based on the parsed datetime value for semantic equality. | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(parsedInstant); | ||
} | ||
|
||
/** As a string. */ | ||
@Override | ||
public String toString() { | ||
return dateTime; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worried about this being valid, but should be fine since dateTime is validated on construction, cannot be mutated externally since it's private and it's set once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. It's
final
and only assigned after validation so should be ok. This is what we do forIpAddress
as well