ChatGPT suggested changes

This commit is contained in:
Jonas Tobias Hopusch 2023-05-18 15:02:12 +02:00
parent 629fd2a516
commit 703e040d11
Signed by: jotoho
GPG Key ID: 913BDF1196DCF600
1 changed files with 49 additions and 28 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}