|
|
@@ -4,140 +4,167 @@ import net.ranides.assira.junit.NewAssert;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import java.time.ZoneId;
|
|
|
-import java.time.ZoneOffset;
|
|
|
-import java.time.ZonedDateTime;
|
|
|
import java.time.format.DateTimeParseException;
|
|
|
|
|
|
-import static net.ranides.assira.time.DateTimeZones.PL;
|
|
|
+import static net.ranides.assira.time.DateTimeFormat.DATE;
|
|
|
+import static net.ranides.assira.time.DateTimeFormat.DATETIME;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
import static org.junit.Assert.assertThrows;
|
|
|
|
|
|
public class DateTimeParsersTest {
|
|
|
|
|
|
@Test
|
|
|
- public void testParsePolishDateTime() {
|
|
|
- ZonedDate date1 = ZonedDate.now(DateTimeZones.UTC);
|
|
|
- ZonedDate date2 = ZonedDate.now().plusDays(25);
|
|
|
- ZonedDateTime date3 = ZonedDateTime.now().plusDays(40);
|
|
|
- ZonedDate date4 = ZonedDate.ofEpochSecond(PL, 11_800_150L);
|
|
|
-
|
|
|
- date1.isAfter(date2);
|
|
|
- date1.isAfter(date3);
|
|
|
-
|
|
|
- ZonedTime time1 = ZonedTime.now();
|
|
|
- ZonedTime time2 = ZonedTime.now().plusMinutes(15);
|
|
|
- ZonedDateTime time3 = ZonedDateTime.now().plusMinutes(30);
|
|
|
-
|
|
|
- time1.isAfter(time2);
|
|
|
- time1.isAfter(time3);
|
|
|
-
|
|
|
- ZonedDateTime a = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00+02:00[Poland]");
|
|
|
- ZonedDateTime b = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00Z");
|
|
|
- ZonedDateTime c = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00+06:00[Europe/Warsaw]");
|
|
|
- ZonedDateTime d = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00+17:00[Europe/Warsaw]");
|
|
|
- ZonedDateTime e = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00+06:00");
|
|
|
- ZonedDateTime f = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00+17:00");
|
|
|
- ZonedDateTime g = DateTimeFormat.DATETIME.withZone(PL).parse("2020-08-24T08:45:00");
|
|
|
- System.out.println(a);
|
|
|
- System.out.println(b);
|
|
|
- System.out.println(c);
|
|
|
- System.out.println(d);
|
|
|
- System.out.println(e);
|
|
|
- System.out.println(f);
|
|
|
- System.out.println(g);
|
|
|
+ public void testParseDateTimeDefault() {
|
|
|
+ NewAssert.assertThrows(DateTimeParseException.class, () -> {
|
|
|
+ DATETIME.parse("2020-08-24T08:45:00");
|
|
|
+ });
|
|
|
+
|
|
|
+ assertEquals("2020-08-24T08:45+02:00[Poland]", DATETIME.parse("2020-08-24T08:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-24T08:45Z", DATETIME.parse("2020-08-24T08:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+02:00[Europe/Warsaw]", DATETIME.parse("2020-08-24T08:45:00+06:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+02:00[Europe/Warsaw]", DATETIME.parse("2020-08-24T08:45:00+17:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+06:00", DATETIME.parse("2020-08-24T08:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+17:00", DATETIME.parse("2020-08-24T08:45:00+17:00").toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testParseDateTime() {
|
|
|
+ public void testParseDateTimeSourced() {
|
|
|
+ ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
|
|
|
- ZonedDateTime a = DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00+02:00[Poland]");
|
|
|
- ZonedDateTime b = DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00Z");
|
|
|
- ZonedDateTime c = DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00+06:00[Europe/Warsaw]");
|
|
|
- ZonedDateTime d = DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00+17:00[Europe/Warsaw]");
|
|
|
- ZonedDateTime e = DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00+06:00");
|
|
|
- ZonedDateTime f = DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00+17:00");
|
|
|
+ assertEquals("2020-08-24T08:45+12:00[Pacific/Funafuti]", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+02:00[Poland]", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-24T08:45Z", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+02:00[Europe/Warsaw]", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00+06:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+02:00[Europe/Warsaw]", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00+17:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+06:00", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-24T08:45+17:00", DATETIME.withSourceZone(zone).parse("2020-08-24T08:45:00+17:00").toString());
|
|
|
+ }
|
|
|
|
|
|
- NewAssert.assertThrows(DateTimeParseException.class, () -> {
|
|
|
- DateTimeFormat.DATETIME.parse("2020-08-24T08:45:00");
|
|
|
+ @Test
|
|
|
+ public void testParseDateTimeTargeted() {
|
|
|
+ ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
+
|
|
|
+ assertEquals("2020-08-24T08:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00").toString());
|
|
|
+ assertEquals("2020-08-24T18:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-24T20:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-24T18:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00+06:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T18:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00+17:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T14:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-24T03:45+12:00[Pacific/Funafuti]", DATETIME.withTargetZone(zone).parse("2020-08-24T08:45:00+17:00").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testParseDateTimeZoned() {
|
|
|
+ ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
+
|
|
|
+ assertEquals("2020-08-24T08:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00").toString());
|
|
|
+ assertEquals("2020-08-24T18:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-24T20:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-24T18:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00+06:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T18:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00+17:00[Europe/Warsaw]").toString());
|
|
|
+ assertEquals("2020-08-24T14:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-24T03:45+12:00[Pacific/Funafuti]", DATETIME.withZone(zone).parse("2020-08-24T08:45:00+17:00").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testParseDateDefault() {
|
|
|
+ assertThrows(DateTimeParseException.class, () -> {
|
|
|
+ DATE.parseDate("2024-10-16");
|
|
|
});
|
|
|
+ assertEquals("2020-08-16 [Poland]", DATE.parseDate("2020-08-16T20:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-16 [Z]", DATE.parseDate("2020-08-16T20:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-16 [+06:00]", DATE.parseDate("2020-08-16T20:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-16 [+18:00]", DATE.parseDate("2020-08-16T20:45:00+18:00").toString());
|
|
|
+ assertThrows(DateTimeParseException.class, () -> {
|
|
|
+ DATE.parseDate("2020-08-16T20:45:00");
|
|
|
+ });
|
|
|
+ assertEquals("2020-08-16 [Poland]", DATE.parseDate("2020-08-16+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-16 [+07:00]", DATE.parseDate("2020-08-16+07:00").toString());
|
|
|
+ }
|
|
|
|
|
|
- assertEquals("2020-08-24T08:45+02:00[Poland]", a.toString());
|
|
|
- assertEquals("2020-08-24T08:45Z", b.toString());
|
|
|
- assertEquals("2020-08-24T08:45+02:00[Europe/Warsaw]", c.toString());
|
|
|
- assertEquals("2020-08-24T08:45+02:00[Europe/Warsaw]", d.toString());
|
|
|
- assertEquals("2020-08-24T08:45+06:00", e.toString());
|
|
|
- assertEquals("2020-08-24T08:45+17:00", f.toString());
|
|
|
+ @Test
|
|
|
+ public void testParseDateSourced() {
|
|
|
+ ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
+
|
|
|
+ assertEquals("2024-10-16 [Pacific/Funafuti]", DATE.withSourceZone(zone).parseDate("2024-10-16").toString());
|
|
|
+ assertEquals("2020-08-16 [Poland]", DATE.withSourceZone(zone).parseDate("2020-08-16T20:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-16 [Z]", DATE.withSourceZone(zone).parseDate("2020-08-16T20:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-16 [+06:00]", DATE.withSourceZone(zone).parseDate("2020-08-16T20:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-16 [+18:00]", DATE.withSourceZone(zone).parseDate("2020-08-16T20:45:00+18:00").toString());
|
|
|
+ assertEquals("2020-08-16 [Pacific/Funafuti]", DATE.withSourceZone(zone).parseDate("2020-08-16T20:45:00").toString());
|
|
|
+ assertEquals("2020-08-16 [Poland]", DATE.withSourceZone(zone).parseDate("2020-08-16+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-16 [+07:00]", DATE.withSourceZone(zone).parseDate("2020-08-16+07:00").toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testParseDate() {
|
|
|
+ public void testParseDateTargeted() {
|
|
|
ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
|
|
|
- ZonedDate a = DateTimeFormat.DATE.withZone(zone).parseDate("2024-10-16");
|
|
|
- ZonedDate b = DateTimeFormat.DATE.withZone(zone).parseDate("2020-08-16T20:45:00+02:00[Poland]");
|
|
|
- ZonedDate c = DateTimeFormat.DATE.withZone(zone).parseDate("2020-08-16T20:45:00Z");
|
|
|
- ZonedDate d = DateTimeFormat.DATE.withZone(zone).parseDate("2020-08-16T20:45:00+06:00");
|
|
|
- ZonedDate e = DateTimeFormat.DATE.withZone(zone).parseDate("2020-08-16T20:45:00+18:00");
|
|
|
- ZonedDate f = DateTimeFormat.DATE.withZone(zone).parseDate("2020-08-16T20:45:00");
|
|
|
-
|
|
|
- System.out.println(a);
|
|
|
- System.out.println(b);
|
|
|
- System.out.println(c);
|
|
|
- System.out.println(d);
|
|
|
- System.out.println(e);
|
|
|
- System.out.println(f);
|
|
|
+ assertEquals("2024-10-16 [Pacific/Funafuti]", DATE.withTargetZone(zone).parseDate("2024-10-16").toString());
|
|
|
+ assertEquals("2020-08-17 [Pacific/Funafuti]", DATE.withTargetZone(zone).parseDate("2020-08-16T20:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("2020-08-17 [Pacific/Funafuti]", DATE.withTargetZone(zone).parseDate("2020-08-16T20:45:00Z").toString());
|
|
|
+ assertEquals("2020-08-17 [Pacific/Funafuti]", DATE.withTargetZone(zone).parseDate("2020-08-16T20:45:00+06:00").toString());
|
|
|
+ assertEquals("2020-08-16 [Pacific/Funafuti]", DATE.withTargetZone(zone).parseDate("2020-08-16T20:45:00+18:00").toString());
|
|
|
+ assertEquals("2020-08-16 [Pacific/Funafuti]", DATE.withTargetZone(zone).parseDate("2020-08-16T20:45:00").toString());
|
|
|
+
|
|
|
+ assertThrows(DateTimeParseException.class, () -> {
|
|
|
+ DATE.withTargetZone(zone).parseDate("2020-08-16+02:00[Poland]");
|
|
|
+ });
|
|
|
+
|
|
|
+ assertThrows(DateTimeParseException.class, () -> {
|
|
|
+ DATE.withTargetZone(zone).parseDate("2020-08-16+07:00");
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testParseTime1() {
|
|
|
+ public void testParseTimeDefault() {
|
|
|
+
|
|
|
assertThrows(DateTimeParseException.class, () -> {
|
|
|
DateTimeFormat.TIME.parseTime("10:45");
|
|
|
});
|
|
|
- ZonedTime b = DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00+02:00[Poland]");
|
|
|
- ZonedTime c = DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00Z");
|
|
|
- ZonedTime d = DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00+06:00");
|
|
|
- ZonedTime e = DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00+18:00");
|
|
|
assertThrows(DateTimeParseException.class, () -> {
|
|
|
DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00");
|
|
|
});
|
|
|
|
|
|
- ZonedTime g = DateTimeFormat.TIME.parseTime("20:45:00+06:00");
|
|
|
- ZonedTime h = DateTimeFormat.TIME.parseTime("20:45:00+04:00[Europe/Astrakhan]");
|
|
|
+ assertEquals("20:45 [Poland]", DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("20:45 [Z]", DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00Z").toString());
|
|
|
+ assertEquals("20:45 [+06:00]", DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00+06:00").toString());
|
|
|
+ assertEquals("20:45 [+18:00]", DateTimeFormat.TIME.parseTime("2020-08-16T20:45:00+18:00").toString());
|
|
|
+ assertEquals("20:45 [+06:00]", DateTimeFormat.TIME.parseTime("20:45:00+06:00").toString());
|
|
|
+ assertEquals("20:45 [Europe/Astrakhan]", DateTimeFormat.TIME.parseTime("20:45:00+04:00[Europe/Astrakhan]").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testParseTimeSourced() {
|
|
|
+ ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
|
|
|
- assertEquals("20:45 [Poland]", b.toString());
|
|
|
- assertEquals("20:45 [Z]", c.toString());
|
|
|
- assertEquals("20:45 [+06:00]", d.toString());
|
|
|
- assertEquals("20:45 [+18:00]", e.toString());
|
|
|
- assertEquals("20:45 [+06:00]", g.toString());
|
|
|
- assertEquals("20:45 [+04:00]", h.toString());
|
|
|
+ assertEquals("10:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("10:45").toString());
|
|
|
+ assertEquals("20:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("2020-08-16T20:45:00").toString());
|
|
|
+ assertEquals("20:45 [Poland]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("2020-08-16T20:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("20:45 [Z]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("2020-08-16T20:45:00Z").toString());
|
|
|
+ assertEquals("20:45 [+06:00]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("2020-08-16T20:45:00+06:00").toString());
|
|
|
+ assertEquals("20:45 [+18:00]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("2020-08-16T20:45:00+18:00").toString());
|
|
|
+ assertEquals("20:45 [+06:00]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("20:45:00+06:00").toString());
|
|
|
+ assertEquals("20:45 [Europe/Astrakhan]", DateTimeFormat.TIME.withSourceZone(zone).parseTime("20:45:00+04:00[Europe/Astrakhan]").toString());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testParseTime2() {
|
|
|
+ public void testParseTimeTargeted() {
|
|
|
ZoneId zone = ZoneId.of("Pacific/Funafuti");
|
|
|
|
|
|
- ZonedTime a = DateTimeFormat.TIME.withTargetZone(zone).parseTime("10:45");
|
|
|
- ZonedTime b = DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00+02:00[Poland]");
|
|
|
- ZonedTime c = DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00Z");
|
|
|
- ZonedTime d = DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00+06:00");
|
|
|
- ZonedTime e = DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00+18:00");
|
|
|
- ZonedTime f = DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00");
|
|
|
assertThrows(DateTimeParseException.class, () -> {
|
|
|
DateTimeFormat.TIME.withTargetZone(zone).parseTime("20:45:00+06:00");
|
|
|
});
|
|
|
assertThrows(DateTimeParseException.class, () -> {
|
|
|
- // @todo wrong, zoneid is ignored
|
|
|
- // @todo use case for withTargetZone(fixed offset) "20:45:00+04:00[Europe/Astrakhan]"
|
|
|
- // @todo use case for withTargetZone(fixed offset) "20:45:00+04:00"
|
|
|
DateTimeFormat.TIME.withTargetZone(zone).parseTime("20:45:00+04:00[Europe/Astrakhan]");
|
|
|
});
|
|
|
|
|
|
- assertEquals("10:45 [Pacific/Funafuti]", a.toString());
|
|
|
- assertEquals("06:45 [Pacific/Funafuti]", b.toString());
|
|
|
- assertEquals("08:45 [Pacific/Funafuti]", c.toString());
|
|
|
- assertEquals("02:45 [Pacific/Funafuti]", d.toString());
|
|
|
- assertEquals("14:45 [Pacific/Funafuti]", e.toString());
|
|
|
- assertEquals("20:45 [Pacific/Funafuti]", f.toString());
|
|
|
+ assertEquals("10:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withTargetZone(zone).parseTime("10:45").toString());
|
|
|
+ assertEquals("06:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00+02:00[Poland]").toString());
|
|
|
+ assertEquals("08:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00Z").toString());
|
|
|
+ assertEquals("02:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00+06:00").toString());
|
|
|
+ assertEquals("14:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00+18:00").toString());
|
|
|
+ assertEquals("20:45 [Pacific/Funafuti]", DateTimeFormat.TIME.withTargetZone(zone).parseTime("2020-08-16T20:45:00").toString());
|
|
|
}
|
|
|
|
|
|
}
|