Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@
<includeJsr303Annotations>true</includeJsr303Annotations>
<includeGeneratedAnnotation>false</includeGeneratedAnnotation>
<useJakartaValidation>true</useJakartaValidation>
<useJodaDates>true</useJodaDates>
<useJodaLocalDates>true</useJodaLocalDates>
<useJodaLocalTimes>true</useJodaLocalTimes>
<dateType>java.time.LocalDate</dateType>
<dateTimeType>java.time.OffsetDateTime</dateTimeType>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -210,11 +209,6 @@
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -225,11 +219,6 @@
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/de/rwth/idsg/ocpp/DateTimeUtils.java
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 src/main/java/de/rwth/idsg/ocpp/jaxb/JavaDateTimeConverter.java
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"));
Copy link
Member

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.

Copy link
Author

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.

}

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 src/main/java/de/rwth/idsg/ocpp/jaxb/JodaDateTimeConverter.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/resources/wsdl-binding/ocpp_binding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

<jaxb:globalBindings generateIsSetMethod="true">

<xjc:javaType name="org.joda.time.DateTime" xmlType="xs:dateTime"
adapter="de.rwth.idsg.ocpp.jaxb.JodaDateTimeConverter" />
<xjc:javaType name="java.time.OffsetDateTime" xmlType="xs:dateTime"
adapter="de.rwth.idsg.ocpp.jaxb.JavaDateTimeConverter" />

</jaxb:globalBindings>

Expand Down
Loading