Skip to content

Commit

Permalink
💚 prepare timezone for util.Date and zone based time
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Jan 29, 2024
1 parent 634823b commit 461fbb2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
61 changes: 37 additions & 24 deletions src/test/java/pro/fessional/mirana/time/DateFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@ public class DateFormatterTest {
final LocalDateTime ldt0 = LocalDateTime.of(ld, lm0);
final Date ud0 = new Date(zdt0.toEpochSecond() * 1000L);

@Test
public void utilDate() {
// SimpleDateFormat, Calendar and util.Date deps on System defaultTimezone
TimeZone df = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
DateFormatter.DATE_FORMAT_19.remove();
DateFormatter.DATE_FORMAT_23.remove();

Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full(ud9));
Assertions.assertEquals("2017-04-26 13:34:56.789", DateFormatter.full(ud9, tz9));
Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full19(ud9));
Assertions.assertEquals("2017-04-26 13:34:56", DateFormatter.full19(ud9, tz9));
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full23(ud9));
Assertions.assertEquals("2017-04-26 13:34:56.789", DateFormatter.full23(ud9, tz9));
Assertions.assertEquals("2017-04-26 12:34:56.000", DateFormatter.full23(ud0));
Assertions.assertEquals("2017-04-26 13:34:56.000", DateFormatter.full23(ud0, tz9));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(ud9));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(ud9, tz9));
Assertions.assertEquals("12:34:56", DateFormatter.time08(ud9));
Assertions.assertEquals("13:34:56", DateFormatter.time08(ud9, tz9));
Assertions.assertEquals("12:34:56", DateFormatter.time08(ud0));
Assertions.assertEquals("13:34:56", DateFormatter.time08(ud0, tz9));
Assertions.assertEquals("12:34:56.789", DateFormatter.time(ud9));
Assertions.assertEquals("13:34:56.789", DateFormatter.time(ud9, tz9));
Assertions.assertEquals("12:34:56", DateFormatter.time(ud0));
Assertions.assertEquals("13:34:56", DateFormatter.time(ud0, tz9));
Assertions.assertEquals("12:34:56.789", DateFormatter.time12(ud9));
Assertions.assertEquals("13:34:56.789", DateFormatter.time12(ud9, tz9));
Assertions.assertEquals("12:34:56.000", DateFormatter.time12(ud0));
Assertions.assertEquals("13:34:56.000", DateFormatter.time12(ud0, tz9));
}
finally {
TimeZone.setDefault(df);
}
}

@Test
public void full19() {
Assertions.assertEquals("2017-04-26 14:38:22", DateFormatter.fixFull19("2017-04-26"));
Expand All @@ -51,51 +88,39 @@ public void full19() {
Assertions.assertEquals(new SimpleDateFormat(PTN_FULL_19), DateFormatter.full19());
Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full19(ldt9));
Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full19(zdt9));
Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full19(ud9));
Assertions.assertEquals("2017-04-26 13:34:56", DateFormatter.full19(zdt9, tz9));
Assertions.assertEquals("2017-04-26 13:34:56", DateFormatter.full19(ud9, tz9));
}

@Test
public void full23() {
Assertions.assertEquals(new SimpleDateFormat(PTN_FULL_23), DateFormatter.full23());
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full23(ldt9));
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full23(zdt9));
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full23(ud9));
Assertions.assertEquals("2017-04-26 13:34:56.789", DateFormatter.full23(zdt9, tz9));
Assertions.assertEquals("2017-04-26 13:34:56.789", DateFormatter.full23(ud9, tz9));

Assertions.assertEquals("2017-04-26 12:34:56.000", DateFormatter.full23(ldt0));
Assertions.assertEquals("2017-04-26 12:34:56.000", DateFormatter.full23(zdt0));
Assertions.assertEquals("2017-04-26 12:34:56.000", DateFormatter.full23(ud0));
Assertions.assertEquals("2017-04-26 13:34:56.000", DateFormatter.full23(zdt0, tz9));
Assertions.assertEquals("2017-04-26 13:34:56.000", DateFormatter.full23(ud0, tz9));
}


@Test
public void full() {
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full(ldt9));
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full(zdt9));
Assertions.assertEquals("2017-04-26 12:34:56.789", DateFormatter.full(ud9));
Assertions.assertEquals("2017-04-26 13:34:56.789", DateFormatter.full(zdt9, tz9));
Assertions.assertEquals("2017-04-26 13:34:56.789", DateFormatter.full(ud9, tz9));

Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full(ldt0));
Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full(zdt0));
Assertions.assertEquals("2017-04-26 12:34:56", DateFormatter.full(ud0));
Assertions.assertEquals("2017-04-26 13:34:56", DateFormatter.full(zdt0, tz9));
Assertions.assertEquals("2017-04-26 13:34:56", DateFormatter.full(ud0, tz9));
}

@Test
public void date10() {
Assertions.assertEquals("2017-04-26", DateFormatter.date10(ld));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(ldt9));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(zdt9));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(ud9));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(zdt9, tz9));
Assertions.assertEquals("2017-04-26", DateFormatter.date10(ud9, tz9));
}


Expand All @@ -106,15 +131,11 @@ public void time() {

Assertions.assertEquals("12:34:56.789", DateFormatter.time(ldt9));
Assertions.assertEquals("12:34:56.789", DateFormatter.time(zdt9));
Assertions.assertEquals("12:34:56.789", DateFormatter.time(ud9));
Assertions.assertEquals("13:34:56.789", DateFormatter.time(zdt9, tz9));
Assertions.assertEquals("13:34:56.789", DateFormatter.time(ud9, tz9));

Assertions.assertEquals("12:34:56", DateFormatter.time(ldt0));
Assertions.assertEquals("12:34:56", DateFormatter.time(zdt0));
Assertions.assertEquals("12:34:56", DateFormatter.time(ud0));
Assertions.assertEquals("13:34:56", DateFormatter.time(zdt0, tz9));
Assertions.assertEquals("13:34:56", DateFormatter.time(ud0, tz9));
}

@Test
Expand All @@ -124,15 +145,11 @@ public void time08() {

Assertions.assertEquals("12:34:56", DateFormatter.time08(ldt9));
Assertions.assertEquals("12:34:56", DateFormatter.time08(zdt9));
Assertions.assertEquals("12:34:56", DateFormatter.time08(ud9));
Assertions.assertEquals("13:34:56", DateFormatter.time08(zdt9, tz9));
Assertions.assertEquals("13:34:56", DateFormatter.time08(ud9, tz9));

Assertions.assertEquals("12:34:56", DateFormatter.time08(ldt0));
Assertions.assertEquals("12:34:56", DateFormatter.time08(zdt0));
Assertions.assertEquals("12:34:56", DateFormatter.time08(ud0));
Assertions.assertEquals("13:34:56", DateFormatter.time08(zdt0, tz9));
Assertions.assertEquals("13:34:56", DateFormatter.time08(ud0, tz9));
}

@Test
Expand All @@ -142,15 +159,11 @@ public void time12() {

Assertions.assertEquals("12:34:56.789", DateFormatter.time12(ldt9));
Assertions.assertEquals("12:34:56.789", DateFormatter.time12(zdt9));
Assertions.assertEquals("12:34:56.789", DateFormatter.time12(ud9));
Assertions.assertEquals("13:34:56.789", DateFormatter.time12(zdt9, tz9));
Assertions.assertEquals("13:34:56.789", DateFormatter.time12(ud9, tz9));

Assertions.assertEquals("12:34:56.000", DateFormatter.time12(ldt0));
Assertions.assertEquals("12:34:56.000", DateFormatter.time12(zdt0));
Assertions.assertEquals("12:34:56.000", DateFormatter.time12(ud0));
Assertions.assertEquals("13:34:56.000", DateFormatter.time12(zdt0, tz9));
Assertions.assertEquals("13:34:56.000", DateFormatter.time12(ud0, tz9));
}

@Test
Expand Down
17 changes: 12 additions & 5 deletions src/test/java/pro/fessional/mirana/time/DateLocalingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,20 @@ public void sysAll() {
@Test
public void useAll() {
assertEquals(utcMs, DateLocaling.useEpoch(cnLdt, cnZid));

assertEquals(utcMs, DateLocaling.useEpoch(jpLdt, jpZid));
assertEquals(cnZdt, DateLocaling.useZdt(jpZdt, cnZid));
assertEquals(cnZdt, DateLocaling.useZdt(cnLdt, cnZid));

assertEquals(cnLdt, DateLocaling.useLdt(utcMs, cnZid));
assertEquals(cnLdt, DateLocaling.useLdt(jpZdt, cnZid));
assertEquals(cnLdt, DateLocaling.useLdt(cnLdt, cnZid));
assertEquals(cnLdt, DateLocaling.useLdt(utcMs, cnZid));

try {
// cnLdt default timezone is UTC+8
ThreadNow.TweakZone.tweakGlobal(TimeZone.getTimeZone("GMT+8"));
assertEquals(cnZdt, DateLocaling.useZdt(cnLdt, cnZid));
assertEquals(cnLdt, DateLocaling.useLdt(cnLdt, cnZid));
}
finally {
ThreadNow.TweakZone.resetGlobal();
}
}

@Test
Expand Down

0 comments on commit 461fbb2

Please sign in to comment.