-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Move from JodaTime to java.time #16
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
Open
juherr
wants to merge
2
commits into
steve-community:master
Choose a base branch
from
juherr:feature/java-time
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package de.rwth.idsg.ocpp; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.time.temporal.ChronoField; | ||
import java.time.temporal.TemporalAccessor; | ||
|
||
public final class DateTimeUtils { | ||
private DateTimeUtils() { | ||
} | ||
|
||
// Flexible ISO-8601 parser: supports the optional fraction and optional offset. | ||
// ISO_LOCAL_DATE_TIME cannot be used because SECOND_OF_MINUTE are required here. | ||
public static final DateTimeFormatter OCPP_DATETIME_PARSER = new DateTimeFormatterBuilder() | ||
.parseCaseInsensitive() | ||
.appendPattern("uuuu-MM-dd'T'HH:mm:ss") | ||
|
||
// Optional: .SSS... (fractional seconds) | ||
.optionalStart() | ||
.appendLiteral('.') | ||
.appendFraction(ChronoField.NANO_OF_SECOND, 1, 9, false) | ||
.optionalEnd() | ||
|
||
// Optional: +02:00 or Z (offset) | ||
.optionalStart() | ||
.appendOffsetId() | ||
.optionalEnd() | ||
|
||
.parseStrict() | ||
.toFormatter(); | ||
|
||
// ISO-8601 formatter: outputs timestamps with fixed 3-digits nanosecond precision. | ||
// ISO_LOCAL_DATE_TIME cannot be used because it does not support the fixed 3-digits nanosecond precision. | ||
// Note: PARSER and FORMATTER are not interchangeable because PARSER is flexible. | ||
public static final DateTimeFormatter OCPP_DATETIME_FORMATTER = new DateTimeFormatterBuilder() | ||
.parseCaseInsensitive() | ||
.appendPattern("uuuu-MM-dd'T'HH:mm:ss.SSS") | ||
.parseLenient() | ||
.appendOffsetId() | ||
.parseStrict() | ||
.toFormatter(); | ||
|
||
public static OffsetDateTime toOffsetDateTime(String value, ZoneId fallbackZoneId) { | ||
if (value == null || value.isBlank()) { | ||
return null; | ||
} | ||
TemporalAccessor parsed = OCPP_DATETIME_PARSER.parse(value); | ||
if (!parsed.isSupported(ChronoField.OFFSET_SECONDS)) { | ||
if (fallbackZoneId == null) { | ||
throw new IllegalArgumentException("No offset and no fallback zone id provided"); | ||
} | ||
// Input has no offset → assume fallback zone | ||
LocalDateTime ldt = LocalDateTime.from(parsed); | ||
ZoneOffset offset = fallbackZoneId.getRules().getOffset(ldt); | ||
return ldt.atOffset(offset); | ||
} | ||
return OffsetDateTime.from(parsed); | ||
} | ||
|
||
public static String toString(OffsetDateTime dateTime, ZoneId zoneId) { | ||
if (dateTime == null) { | ||
return null; | ||
} | ||
if (zoneId == null) { | ||
// Convert to UTC before formatting. | ||
// From specification: OCPP does not prescribe the use of a specific time zone for time values. | ||
// However, it is strongly recommended to use UTC for all time values to improve interoperability | ||
// between Central Systems and Charge Points. | ||
dateTime = dateTime.withOffsetSameInstant(ZoneOffset.UTC); | ||
} else { | ||
dateTime = dateTime.withOffsetSameInstant(zoneId.getRules().getOffset(dateTime.toLocalDateTime())); | ||
} | ||
return OCPP_DATETIME_FORMATTER.format(dateTime); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/de/rwth/idsg/ocpp/jaxb/JavaDateTimeConverter.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,35 @@ | ||
package de.rwth.idsg.ocpp.jaxb; | ||
|
||
import de.rwth.idsg.ocpp.DateTimeUtils; | ||
import jakarta.xml.bind.annotation.adapters.XmlAdapter; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
|
||
/** | ||
* Java-Time and XSD represent data and time information according to ISO 8601. | ||
*/ | ||
public class JavaDateTimeConverter extends XmlAdapter<String, OffsetDateTime> { | ||
|
||
private final ZoneId fallbackZoneId; | ||
private final boolean marchallToUtc; | ||
|
||
public JavaDateTimeConverter() { | ||
this(ZoneId.systemDefault(), System.getProperty("steve.ocpp.marshall-to-utc", "true").equals("true")); | ||
} | ||
|
||
public JavaDateTimeConverter(ZoneId fallbackZoneId, boolean marchallToUtc) { | ||
this.fallbackZoneId = fallbackZoneId; | ||
this.marchallToUtc = marchallToUtc; | ||
} | ||
|
||
@Override | ||
public OffsetDateTime unmarshal(String v) { | ||
return DateTimeUtils.toOffsetDateTime(v, fallbackZoneId); | ||
} | ||
|
||
@Override | ||
public String marshal(OffsetDateTime v) { | ||
return DateTimeUtils.toString(v, marchallToUtc ? null : fallbackZoneId); | ||
} | ||
} |
100 changes: 0 additions & 100 deletions
100
src/main/java/de/rwth/idsg/ocpp/jaxb/JodaDateTimeConverter.java
This file was deleted.
Oops, something went wrong.
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
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.
i dont get why this property is necessary.
steve has a UTC setting, which has this consequence. finally, the library actually should just respect the zone id the java app runs in and that is it.
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.
The property configures the zone used by the output format: utc as expected by the specification or the default java zone.
It helps when default zone is not UTC but you want to respect the specification.
As Steve configures utc by default both configuration will have the same effect.
UTC could always be used (my 1st implementation) but your test suite checks cases with not utc as default zone.