Skip to content

Commit 042ec9a

Browse files
committed
fix critical daylight saving time in Downloader
1 parent f653967 commit 042ec9a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/main/java/edu/ie3/tools/Downloader.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.IOException;
1616
import java.net.HttpURLConnection;
1717
import java.net.URL;
18+
import java.time.ZoneId;
1819
import java.time.ZonedDateTime;
1920
import java.util.Collections;
2021
import java.util.List;
@@ -35,8 +36,12 @@ public class Downloader implements Runnable {
3536
public static final Logger logger = LogManager.getLogger(Downloader.class);
3637
public static final Logger filestatusLogger = LogManager.getLogger("FileStatus");
3738

38-
public int downloadedFiles = 0;
39-
private DatabaseController dbController;
39+
private int downloadedFiles = 0;
40+
private final DatabaseController dbController;
41+
42+
public Downloader() {
43+
dbController = new DatabaseController(PERSISTENCE_UNIT_NAME, validateConnectionProperties());
44+
}
4045

4146
/** @return if a connection to given url could be established */
4247
public static boolean isUrlReachable(String url) {
@@ -53,21 +58,24 @@ public static boolean isUrlReachable(String url) {
5358
return false;
5459
}
5560

56-
public void printInit() {
61+
private void printInit() {
5762
logger.info("________________________________________________________________________________");
5863
logger.info("Downloader started");
5964
}
6065

66+
private ZonedDateTime safeZonedDateTime(ZonedDateTime zdt) {
67+
return zdt.toLocalDateTime().atZone(ZoneId.of("UTC"));
68+
}
69+
6170
public void run() {
6271
filestatusLogger.setLevel(Main.filestatus ? Level.ALL : Level.OFF);
6372
logger.setLevel(Main.debug ? Level.ALL : Level.INFO);
6473
printInit();
6574
try {
6675

67-
validateConnectionProperties();
68-
6976
// get newest possible modelrun: (now - 3h) rounded down to the next multiple of 3
70-
ZonedDateTime newestPossibleModelrun = ZonedDateTime.now();
77+
ZonedDateTime newestPossibleModelrun = safeZonedDateTime(ZonedDateTime.now());
78+
7179
newestPossibleModelrun =
7280
newestPossibleModelrun
7381
.minusHours(3)
@@ -108,7 +116,7 @@ public void run() {
108116
downloadedFiles = 0;
109117

110118
// download new files
111-
ZonedDateTime currentModelrun = newestDateDownloaded.plusHours(3);
119+
ZonedDateTime currentModelrun = safeZonedDateTime(newestDateDownloaded).plusHours(3);
112120

113121
// while the newest possible modelrun is later than or equal to the current one
114122
// write Files from DWD database to file
@@ -225,7 +233,7 @@ private void shutdown() {
225233
}
226234

227235
/** Validates Connection Properties from user input */
228-
private void validateConnectionProperties() {
236+
private Properties validateConnectionProperties() {
229237
Properties receivedProperties = new Properties();
230238

231239
if (Main.connectionUrl != null)
@@ -237,6 +245,6 @@ private void validateConnectionProperties() {
237245
if (Main.databasePassword != null)
238246
receivedProperties.setProperty("javax.persistence.jdbc.password", Main.databasePassword);
239247

240-
dbController = new DatabaseController(PERSISTENCE_UNIT_NAME, receivedProperties);
248+
return receivedProperties;
241249
}
242250
}

0 commit comments

Comments
 (0)