|
17 | 17 | package org.apache.cloudstack.resource; |
18 | 18 |
|
19 | 19 | import java.lang.reflect.Field; |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.Instant; |
| 22 | +import java.time.ZoneId; |
| 23 | +import java.time.ZonedDateTime; |
| 24 | +import java.time.zone.ZoneRules; |
20 | 25 | import java.util.ArrayList; |
21 | 26 | import java.util.Calendar; |
22 | 27 | import java.util.Date; |
@@ -629,9 +634,23 @@ public void testCalculatePastDateFromConfig() { |
629 | 634 | Date result = resourceCleanupService.calculatePastDateFromConfig( |
630 | 635 | ResourceCleanupService.ExpungedResourcesPurgeKeepPastDays.key(), |
631 | 636 | days); |
632 | | - Date today = new Date(); |
633 | | - long diff = today.getTime() - result.getTime(); |
634 | | - Assert.assertEquals(days, TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS)); |
| 637 | + Instant resultInstant = result.toInstant(); |
| 638 | + ZoneId systemZone = ZoneId.systemDefault(); |
| 639 | + ZoneRules rules = systemZone.getRules(); |
| 640 | + ZonedDateTime resultDateTime = resultInstant.atZone(systemZone); |
| 641 | + boolean isDSTInResultDateTime = rules.isDaylightSavings(resultDateTime.toInstant()); |
| 642 | + |
| 643 | + ZonedDateTime todayDateTime = ZonedDateTime.now(systemZone); |
| 644 | + boolean isDSTInTodayDateTime = rules.isDaylightSavings(todayDateTime.toInstant()); |
| 645 | + |
| 646 | + Duration duration = Duration.between(resultDateTime, todayDateTime); |
| 647 | + long actualDays = TimeUnit.DAYS.convert(duration.toMillis(), TimeUnit.MILLISECONDS); |
| 648 | + |
| 649 | + if (!isDSTInResultDateTime && isDSTInTodayDateTime) { |
| 650 | + Assert.assertEquals(days - 1, actualDays); |
| 651 | + } else { |
| 652 | + Assert.assertEquals(days, actualDays); |
| 653 | + } |
635 | 654 | } |
636 | 655 |
|
637 | 656 | @Test |
|
0 commit comments