Skip to content

Commit

Permalink
removing timezone from proof - TimeZone is always local norwegian time
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Strandenæs committed Mar 25, 2020
1 parent 9b19816 commit b8c762a
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 50 deletions.
1 change: 0 additions & 1 deletion datatypes-examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@
<day>31</day>
<hour>0</hour>
<min>0</min>
<time-zone>+01:00</time-zone>
</to>
</yearly-repeating-period>
</valid-period>
Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ Represents a legal document (Certificate, Licence, Permit, etc.) issued to a sin

|Name|Type|Required|Description|
|----|----|--------|-----------|
|from|ZonedDateTime|no|ISO8601 full DateTime|
|to|ZonedDateTime|no|ISO8601 full DateTime|
|from|LocalDateTime|no|ISO8601 full DateTime|
|to|LocalDateTime|no|ISO8601 full DateTime|

### Proof.YearlyRepeatingPeriod

Expand All @@ -621,7 +621,7 @@ Represents a legal document (Certificate, Licence, Permit, etc.) issued to a sin
|day|Integer|yes||
|hour|Integer|no||
|min|Integer|no||
|timeZone|String|no|Timezone ISO-8601|
|timeZone|String|no|Deprecated, do not use. Will be ignored.|

### Proof.ProofHolder

Expand Down Expand Up @@ -670,7 +670,6 @@ Valid values:
<day>31</day>
<hour>0</hour>
<min>0</min>
<time-zone>+01:00</time-zone>
</to>
</yearly-repeating-period>
</valid-period>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no.digipost.api.datatypes.marshalling;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeXmlAdapter extends XmlAdapter<String, LocalDateTime> {
@Override
public String marshal(LocalDateTime v) {
if (v == null) {
return null;
}
return v.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}

@Override
public LocalDateTime unmarshal(final String s) {
if (s == null) {
return null;
}
return LocalDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(s));
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
@XmlSchema(namespace = DIGIPOST_DATATYPES_NAMESPACE, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class)
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class),
@XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
})
@DataTypePackage
package no.digipost.api.datatypes.types;

import no.digipost.api.datatypes.documentation.DataTypePackage;
import no.digipost.api.datatypes.marshalling.LocalDateTimeXmlAdapter;
import no.digipost.api.datatypes.marshalling.ZonedDateTimeXmlAdapter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import static no.digipost.api.datatypes.marshalling.DataTypesJAXBContext.DIGIPOST_DATATYPES_NAMESPACE;
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package no.digipost.api.datatypes.types.proof;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;

import javax.validation.constraints.NotNull;
Expand All @@ -16,7 +14,6 @@

@XmlType
@Value
@AllArgsConstructor
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@With
public class CalendarDate {
Expand Down Expand Up @@ -44,8 +41,22 @@ public class CalendarDate {

@XmlElement(name = "time-zone", defaultValue = "+01:00")
@Pattern(regexp = "Z|[+-][01]\\d:{0,1}[0-5]\\d|[+-][01]\\d")
@Description("Timezone ISO-8601")
@Description("Deprecated, do not use. Will be ignored.")
String timeZone;

public static CalendarDate EXAMPLE = new CalendarDate(5, 9, 0, 0, "+01:00");
@Deprecated
public CalendarDate(Integer month, Integer day, Integer hour, Integer min, String timeZone) {
this.month = month;
this.day = day;
this.hour = hour;
this.min = min;
this.timeZone = timeZone;
}

public CalendarDate(Integer month, Integer day, Integer hour, Integer min) {
this(month, day, hour, min, null);
}


public static CalendarDate EXAMPLE = new CalendarDate(5, 9, 0, 0);
}
16 changes: 11 additions & 5 deletions src/main/java/no/digipost/api/datatypes/types/proof/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;
import no.digipost.api.datatypes.validation.ValidPeriode;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

Expand All @@ -25,15 +25,21 @@
public class Period {
@XmlElement(name = "from")
@Description("ISO8601 full DateTime")
ZonedDateTime from;
LocalDateTime from;

@XmlElement(name = "to")
@Description("ISO8601 full DateTime")
ZonedDateTime to;
LocalDateTime to;

@Deprecated
public Period(ZonedDateTime from, ZonedDateTime to) {
this(from != null ? from.withZoneSameInstant(ZoneId.of("Europe/Oslo")).toLocalDateTime() : null,
to != null ? to.withZoneSameInstant(ZoneId.of("Europe/Oslo")).toLocalDateTime() : null);
}

public static Period EXAMPLE = new Period(
ZonedDateTime.of(2019, 5, 23, 10, 0, 0, 0, ZoneId.of("+0100"))
, ZonedDateTime.of(2019, 5, 23, 16, 0, 0, 0, ZoneId.of("+0100"))
LocalDateTime.of(2019, 5, 23, 10, 0, 0, 0)
, LocalDateTime.of(2019, 5, 23, 16, 0, 0, 0)
);

public String getISO8601() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import lombok.NoArgsConstructor;
import lombok.Value;
import lombok.With;
import lombok.experimental.Wither;
import no.digipost.api.datatypes.documentation.Description;

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.Instant;
import java.time.LocalDateTime;

@XmlType
@Value
Expand Down Expand Up @@ -44,39 +41,35 @@ public class YearlyRepeatingPeriod {
public static YearlyRepeatingPeriod EXAMPLE = new YearlyRepeatingPeriod(
2020,
2022,
new CalendarDate(1, 1, null, null, null),
new CalendarDate(12, 31, 0, 0, "+01:00")
new CalendarDate(1, 1, null, null),
new CalendarDate(12, 31, 0, 0)
);

public String getISO8601() {
Instant now = Instant.now();
ZonedDateTime startDate = buildZonedDateTime(now, from, startYear);
ZonedDateTime endDate = buildZonedDateTime(now, to, endYear);
String startDate = writeLocalDate(from, startYear);
String endDate = writeLocalDate(to, endYear);

if (startYear != null && endYear != null) {
return "R/" + startDate + "/" + endDate;
} else if (endYear != null) {
return "R/" + startDate.toString().substring(5) + "/" + endDate.toString();
return "R/" + startDate.substring(5) + "/" + endDate;
} else if (startYear != null) {
return "R/" + startDate + "/" + endDate.toString().substring(5);
return "R/" + startDate + "/" + endDate.substring(5);
} else {
return "R/" + startDate.toString().substring(5) + "/" + endDate.toString().substring(5);
return "R/" + startDate.substring(5) + "/" + endDate.substring(5);
}
}

private static ZonedDateTime buildZonedDateTime(Instant now, CalendarDate t_date, Integer year) {
private static String writeLocalDate(CalendarDate t_date, Integer year) {
int t_minute = 0;
int t_hour = 0;
int t_day = 1;
int t_month = 1;
ZoneId t_zone;
t_zone = t_date.getTimeZone() != null ? ZoneId.of(t_date.getTimeZone()) : ZoneId.of("+01:00");
t_minute = t_date.getMin() != null ? t_date.getMin() : t_minute;
t_hour = t_date.getHour() != null ? t_date.getHour() : t_hour;
t_day = t_date.getDay() != null ? t_date.getDay() : t_day;
t_month = t_date.getMonth() != null ? t_date.getMonth() : t_month;
ZonedDateTime nowInThatZone = now.atZone(t_zone);
int t_year = year != null ? year : nowInThatZone.getYear();
return ZonedDateTime.of(t_year, t_month, t_day, t_hour, t_minute, 0, 0, t_zone);
int t_year = year != null ? year : 2019; // note: the default value will be discarded, but we need it to create a LocalDateTime
return LocalDateTime.of(t_year, t_month, t_day, t_hour, t_minute, 0, 0).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
@XmlAccessorType(XmlAccessType.FIELD)
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(ZonedDateTimeXmlAdapter.class),
@XmlJavaTypeAdapter(LocalDateTimeXmlAdapter.class)
})
@DataTypePackage
package no.digipost.api.datatypes.types.proof;

import no.digipost.api.datatypes.documentation.DataTypePackage;
import no.digipost.api.datatypes.marshalling.LocalDateTimeXmlAdapter;
import no.digipost.api.datatypes.marshalling.ZonedDateTimeXmlAdapter;

import javax.xml.bind.annotation.XmlAccessType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

Expand All @@ -16,52 +17,62 @@ public class TimePeriodTest {
@Test
void period_fra_til() {
Period period = new Period(
ZonedDateTime.of(2019, 8, 1, 0, 0, 0, 0, ZoneId.of("+01:00")),
ZonedDateTime.of(2022, 8, 1, 0, 0, 0, 0, ZoneId.of("+01:00"))
LocalDateTime.of(2019, 8, 1, 0, 0, 0, 0),
LocalDateTime.of(2022, 8, 1, 0, 0, 0, 0)
);

assertThat(period.getISO8601(), equalTo("2019-08-01T00:00+01:00/2022-08-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("2019-08-01T00:00/2022-08-01T00:00"));
}

@Test
void period_fra() {
Period period = new Period(
ZonedDateTime.of(2019, 8, 1, 0, 0, 0, 0, ZoneId.of("+01:00")),
LocalDateTime.of(2019, 8, 1, 0, 0, 0, 0),
null
);

assertThat(period.getISO8601(), equalTo("2019-08-01T00:00+01:00/.."));
assertThat(period.getISO8601(), equalTo("2019-08-01T00:00/.."));
}

@Test
void period_til() {
Period period = new Period(
null,
ZonedDateTime.of(2022, 8, 1, 0, 0, 0, 0, ZoneId.of("+01:00"))
LocalDateTime.of(2022, 8, 1, 0, 0, 0, 0)
);

assertThat(period.getISO8601(), equalTo("../2022-08-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("../2022-08-01T00:00"));
}

@Test
void period() {
Period period = new Period(
null,
(LocalDateTime) null,
null
);

assertThat(period.getISO8601(), containsString("../.."));
}

@Test
void deprecated_constructor_period_should_give_correct_norwegian_time() {
Period period = new Period(
ZonedDateTime.of(2019, 8, 1, 0, 0, 0, 0, ZoneId.of("+02:00")),
ZonedDateTime.of(2022, 8, 1, 0, 0, 0, 0, ZoneId.of("+02:00"))
);

assertThat(period.getISO8601(), equalTo("2019-08-01T00:00/2022-08-01T00:00"));
}

@Test
void repeating_fra_til() {
YearlyRepeatingPeriod period = new YearlyRepeatingPeriod(
2019, 2022
, new CalendarDate(5, 1, 0, 0, "+01:00")
, new CalendarDate(3, 1, 0, 0, "+01:00")
, new CalendarDate(10, 1, 0, 0, "+01:00")
);

assertThat(period.getISO8601(), equalTo("R/2019-05-01T00:00+01:00/2022-10-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("R/2019-03-01T00:00/2022-10-01T00:00"));
}

@Test
Expand All @@ -72,7 +83,7 @@ void repeating_fra() {
, new CalendarDate(10, 1, 0, 0, "+01:00")
);

assertThat(period.getISO8601(), equalTo("R/05-01T00:00+01:00/2022-10-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("R/05-01T00:00/2022-10-01T00:00"));
}

@Test
Expand All @@ -83,7 +94,7 @@ void repeating_to() {
, new CalendarDate(10, 1, 0, 0, "+01:00")
);

assertThat(period.getISO8601(), equalTo("R/2019-05-01T00:00+01:00/10-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("R/2019-05-01T00:00/10-01T00:00"));
}

@Test
Expand All @@ -94,7 +105,7 @@ void repeating() {
, new CalendarDate(10, 1, 0, 0, "+01:00")
);

assertThat(period.getISO8601(), equalTo("R/05-01T00:00+01:00/10-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("R/05-01T00:00/10-01T00:00"));
}

@Test
Expand All @@ -105,18 +116,18 @@ void repeating_only_required() {
, new CalendarDate(10, 1, null, null, null)
);

assertThat(period.getISO8601(), equalTo("R/05-01T00:00+01:00/10-01T00:00+01:00"));
assertThat(period.getISO8601(), equalTo("R/05-01T00:00/10-01T00:00"));
}

@Test
void repeating_esoteric_timezones() {
void repeating_discard_esoteric_timezones() {
YearlyRepeatingPeriod period = new YearlyRepeatingPeriod(
null, null
, new CalendarDate(5, 1, 0, 0, "+03:00")
, new CalendarDate(10, 1, 0, 0, "+04:00")
);

assertThat(period.getISO8601(), equalTo("R/05-01T00:00+03:00/10-01T00:00+04:00"));
assertThat(period.getISO8601(), equalTo("R/05-01T00:00/10-01T00:00"));
}

}

0 comments on commit b8c762a

Please sign in to comment.