Skip to content

ResourceCleanupService test fix for daylight saving time #10749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
package org.apache.cloudstack.resource;

import java.lang.reflect.Field;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -629,9 +634,23 @@ public void testCalculatePastDateFromConfig() {
Date result = resourceCleanupService.calculatePastDateFromConfig(
ResourceCleanupService.ExpungedResourcesPurgeKeepPastDays.key(),
days);
Date today = new Date();
long diff = today.getTime() - result.getTime();
Assert.assertEquals(days, TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
Instant resultInstant = result.toInstant();
ZoneId systemZone = ZoneId.systemDefault();
ZoneRules rules = systemZone.getRules();
ZonedDateTime resultDateTime = resultInstant.atZone(systemZone);
boolean isDSTInResultDateTime = rules.isDaylightSavings(resultDateTime.toInstant());

ZonedDateTime todayDateTime = ZonedDateTime.now(systemZone);
boolean isDSTInTodayDateTime = rules.isDaylightSavings(todayDateTime.toInstant());

Duration duration = Duration.between(resultDateTime, todayDateTime);
long actualDays = TimeUnit.DAYS.convert(duration.toMillis(), TimeUnit.MILLISECONDS);

if (!isDSTInResultDateTime && isDSTInTodayDateTime) {
Assert.assertEquals(days - 1, actualDays);
} else {
Assert.assertEquals(days, actualDays);
}
}

@Test
Expand Down