Explorar o código

zonedatetime: fix literals

Ranides Atterwim hai 1 ano
pai
achega
83284e1cfb

+ 35 - 29
assira.commons/src/main/java/net/ranides/assira/time2/DateTimeLiteral.java

@@ -2,6 +2,8 @@ package net.ranides.assira.time2;
 
 import lombok.experimental.UtilityClass;
 
+import java.time.LocalDate;
+import java.time.LocalTime;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 
@@ -12,63 +14,67 @@ public class DateTimeLiteral {
 
     public static final ZonedDateTime MAX = datetime(4000, 1, 1, 0, 0, 0);
 
-    private static final int BASE_Y = 1900;
-    private static final int BASE_M = 1;
-    private static final int BASE_D = 1;
+    public static ZonedTime time(ZoneId zone, int h, int m, int s) {
+        return ZonedTime.of(zone, LocalTime.of(h, m, s));
+    }
+
+    public static ZonedDate date(ZoneId zone, int y, int m, int d) {
+        return ZonedDate.of(zone, LocalDate.of(y, m, d));
+    }
+
+    public static ZonedDateTime datetime(ZoneId zone, int y, int m, int d, int H, int M) {
+        return ZonedDateTime.of(y, m, d, H, M, 0, 0, zone);
+    }
+
+    public static ZonedDateTime datetime(ZoneId zone, int y, int m, int d, int H, int M, int S) {
+        return ZonedDateTime.of(y, m, d, H, M, S, 0, zone);
+    }
 
     public static ZonedTime time(int h, int m, int s) {
-        return ZonedTime.of(datetime(BASE_Y, BASE_M, BASE_D, h, m, s));
+        return time(ZoneId.systemDefault(), h, m, s);
     }
 
     public static ZonedDate date(int y, int m, int d) {
-        return ZonedDate.of(datetime(y, m, d, 0, 0));
+        return date(ZoneId.systemDefault(), y, m, d);
     }
 
     public static ZonedDateTime datetime(int y, int m, int d, int H, int M) {
-        return datetime(y, m, d, H, M, 0);
+        return datetime(ZoneId.systemDefault(), y, m, d, H, M);
     }
 
     public static ZonedDateTime datetime(int y, int m, int d, int H, int M, int S) {
-        return ZonedDateTime.of(y, m, d, H, M, S, 0, ZoneId.systemDefault());
+        return datetime(ZoneId.systemDefault(), y, m, d, H, M, S);
     }
 
-    @ZoneExplicit
     public static ZonedTime timeUtc(int h, int m, int s) {
-        return ZonedTime.of(datetimeUtc(BASE_Y, BASE_M, BASE_D, h, m, s));
+        return time(DateTimeUtils.ZONE_UTC, h, m, s);
     }
 
-    @ZoneExplicit
-    public static ZonedTime timePolish(int h, int m, int s) {
-        return ZonedTime.of(datetimePolish(BASE_Y, BASE_M, BASE_D, h, m, s));
+    public static ZonedDate dateUtc(int y, int m, int d) {
+        return date(DateTimeUtils.ZONE_UTC, y, m, d);
     }
 
-    @ZoneExplicit
-    public static ZonedDate dateUtc(int y, int m, int d) {
-        return ZonedDate.of(datetimeUtc(y, m, d, 0, 0));
+    public static ZonedDateTime datetimeUtc(int y, int m, int d, int H, int M) {
+        return datetime(DateTimeUtils.ZONE_UTC, y, m, d, H, M);
     }
 
-    @ZoneExplicit
-    public static ZonedDate datePolish(int y, int m, int d) {
-        return ZonedDate.of(datetimePolish(y, m, d, 0, 0));
+    public static ZonedDateTime datetimeUtc(int y, int m, int d, int H, int M, int S) {
+        return datetime(DateTimeUtils.ZONE_UTC, y, m, d, H, M, S);
     }
 
-    @ZoneExplicit
-    public static ZonedDateTime datetimeUtc(int y, int m, int d, int H, int M) {
-        return datetimeUtc(y, m, d, H, M, 0);
+    public static ZonedTime timePolish(int h, int m, int s) {
+        return time(DateTimeUtils.ZONE_PL, h, m, s);
     }
 
-    @ZoneExplicit
-    public static ZonedDateTime datetimePolish(int y, int m, int d, int H, int M) {
-        return datetimePolish(y, m, d, H, M, 0);
+    public static ZonedDate datePolish(int y, int m, int d) {
+        return date(DateTimeUtils.ZONE_PL, y, m, d);
     }
 
-    @ZoneExplicit
-    public static ZonedDateTime datetimeUtc(int y, int m, int d, int H, int M, int S) {
-        return ZonedDateTime.of(y, m, d, H, M, S, 0, DateTimeUtils.ZONE_UTC);
+    public static ZonedDateTime datetimePolish(int y, int m, int d, int H, int M) {
+        return datetime(DateTimeUtils.ZONE_PL, y, m, d, H, M);
     }
 
-    @ZoneExplicit
     public static ZonedDateTime datetimePolish(int y, int m, int d, int H, int M, int S) {
-        return ZonedDateTime.of(y, m, d, H, M, S, 0, DateTimeUtils.ZONE_PL);
+        return datetime(DateTimeUtils.ZONE_PL, y, m, d, H, M, S);
     }
 }

+ 9 - 8
assira.commons/src/test/java/net/ranides/assira/time2/ZonedTimeTest.java

@@ -49,22 +49,23 @@ public class ZonedTimeTest {
     @Test
     public void testWithTime() {
         ZoneId zone = ZoneId.of("Pacific/Funafuti");
+
         ZonedDate base1 = DateTimeLiteral.dateUtc(1980, 1, 1);
-        ZonedDate base2 = DateTimeLiteral.dateUtc(1980, 1, 1).withZone(zone);
-        ZonedDate base3 = DateTimeLiteral.dateUtc(2010, 1, 1).withZone(zone);
+        ZonedDate base2 = DateTimeLiteral.date(zone, 1980, 1, 1);
+        ZonedDate base3 = DateTimeLiteral.date(zone, 2010, 1, 1);
 
         ZonedTime time1 = DateTimeLiteral.timeUtc(6, 45, 0);
-        ZonedTime time2 = DateTimeLiteral.timeUtc(6, 45, 0).withZone(zone);
+        ZonedTime time2 = DateTimeLiteral.time(zone, 6, 45, 0);
 
         System.out.println(time1.withDate(base1));
-        System.out.println(time1.withDate(base2));
-        System.out.println(time1.withDate(base3)); // WTF?
+        // System.out.println(time1.withDate(base2));
+        // System.out.println(time1.withDate(base3));
 
         System.out.println("---");
 
-        System.out.println(time2.withDate(base1)); // WTF ze strefą i wartością
-        System.out.println(time2.withDate(base2)); // WTF ze strefą i wartością
-        System.out.println(time2.withDate(base3)); // WTF ze strefą i wartością
+        // System.out.println(time2.withDate(base1));
+        System.out.println(time2.withDate(base2));
+        System.out.println(time2.withDate(base3));
 
     }
 }