From 703e040d1187d67df24cdd5b6b48ab62efb682c0 Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Thu, 18 May 2023 15:02:12 +0200 Subject: [PATCH] ChatGPT suggested changes --- .../de/jotoho/waituntil/TimeCalculator.java | 77 ++++++++++++------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/src/main/java/de/jotoho/waituntil/TimeCalculator.java b/src/main/java/de/jotoho/waituntil/TimeCalculator.java index c3b2903..6155850 100644 --- a/src/main/java/de/jotoho/waituntil/TimeCalculator.java +++ b/src/main/java/de/jotoho/waituntil/TimeCalculator.java @@ -1,8 +1,18 @@ package de.jotoho.waituntil; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.TimeZone; + +import static java.lang.System.Logger.Level; + /* waituntil - a tool for delaying command execution until the specified time - Copyright (C) 2022 Jonas Tobias Hopusch + Copyright (C) 2023 Jonas Tobias Hopusch This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as @@ -18,40 +28,51 @@ package de.jotoho.waituntil; along with this program. If not, see . */ -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalTime; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.time.format.FormatStyle; -import java.util.TimeZone; - -import static java.lang.System.Logger.Level; - +/** + * A utility class for calculating and announcing a target time based on user input. + */ public final class TimeCalculator { + private static final String LANG_GERMAN = GlobalConf.langGerman; + + /** + * Calculates and announces the target time based on the user input. + * + * @param userTimeInputRaw the user input representing the desired time + * @return the calculated target time as a {@link ZonedDateTime} object + */ public static ZonedDateTime calculateAndAnnounceTargetTime(final String userTimeInputRaw) { + // Parsing user input to obtain the desired time final var userTimeInputRelative = LocalTime.parse(userTimeInputRaw); - final var userTimeInputAbsolute = ZonedDateTime.of(LocalDate.now(), - userTimeInputRelative, - TimeZone.getDefault() - .toZoneId()); + final var userTimeInputAbsolute = ZonedDateTime.of( + LocalDate.now(), + userTimeInputRelative, + TimeZone.getDefault().toZoneId() + ); - final var userTimeInputFinal = (Instant.now() - .isBefore(userTimeInputAbsolute.toInstant())) - ? userTimeInputAbsolute - : userTimeInputAbsolute.plusDays(1); + // Adjusting the target time if it has already passed for the current day + final var userTimeInputFinal = (Instant.now().isBefore(userTimeInputAbsolute.toInstant())) + ? userTimeInputAbsolute + : userTimeInputAbsolute.plusDays(1); - final var formattedTimeStamp = - userTimeInputFinal.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)); + // Formatting the target time for display + final var formattedTimeStamp = userTimeInputFinal.format( + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG) + ); - final String msg = switch (GlobalConf.applicationOutputLanguage) { - case GlobalConf.langGerman -> ("Dieses Program wird bis zum %s warten.").formatted( - formattedTimeStamp); - default -> "WaitUntil will suspend until %s".formatted(formattedTimeStamp); - }; - System.getLogger("timecalculator") - .log(Level.INFO, msg); + // Constructing the appropriate message based on the output language + final String formattedMessage; + switch (GlobalConf.applicationOutputLanguage) { + case LANG_GERMAN: + formattedMessage = ("Dieses Program wird bis zum %s warten.").formatted(formattedTimeStamp); + break; + default: + formattedMessage = "WaitUntil will suspend until %s".formatted(formattedTimeStamp); + break; + } + + // Logging the message + System.getLogger("timecalculator").log(Level.INFO, formattedMessage); return userTimeInputFinal; }