Compare commits
1 commit
master
...
chatgpt-te
Author | SHA1 | Date | |
---|---|---|---|
703e040d11 |
1 changed files with 49 additions and 28 deletions
|
@ -1,8 +1,18 @@
|
||||||
package de.jotoho.waituntil;
|
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
|
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
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Affero General Public License as
|
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/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.time.Instant;
|
/**
|
||||||
import java.time.LocalDate;
|
* A utility class for calculating and announcing a target time based on user input.
|
||||||
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;
|
|
||||||
|
|
||||||
public final class TimeCalculator {
|
public final class TimeCalculator {
|
||||||
|
|
||||||
public static ZonedDateTime calculateAndAnnounceTargetTime(final String userTimeInputRaw) {
|
private static final String LANG_GERMAN = GlobalConf.langGerman;
|
||||||
final var userTimeInputRelative = LocalTime.parse(userTimeInputRaw);
|
|
||||||
final var userTimeInputAbsolute = ZonedDateTime.of(LocalDate.now(),
|
|
||||||
userTimeInputRelative,
|
|
||||||
TimeZone.getDefault()
|
|
||||||
.toZoneId());
|
|
||||||
|
|
||||||
final var userTimeInputFinal = (Instant.now()
|
/**
|
||||||
.isBefore(userTimeInputAbsolute.toInstant()))
|
* 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()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Adjusting the target time if it has already passed for the current day
|
||||||
|
final var userTimeInputFinal = (Instant.now().isBefore(userTimeInputAbsolute.toInstant()))
|
||||||
? userTimeInputAbsolute
|
? userTimeInputAbsolute
|
||||||
: userTimeInputAbsolute.plusDays(1);
|
: userTimeInputAbsolute.plusDays(1);
|
||||||
|
|
||||||
final var formattedTimeStamp =
|
// Formatting the target time for display
|
||||||
userTimeInputFinal.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
|
final var formattedTimeStamp = userTimeInputFinal.format(
|
||||||
|
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
|
||||||
|
);
|
||||||
|
|
||||||
final String msg = switch (GlobalConf.applicationOutputLanguage) {
|
// Constructing the appropriate message based on the output language
|
||||||
case GlobalConf.langGerman -> ("Dieses Program wird bis zum %s warten.").formatted(
|
final String formattedMessage;
|
||||||
formattedTimeStamp);
|
switch (GlobalConf.applicationOutputLanguage) {
|
||||||
default -> "WaitUntil will suspend until %s".formatted(formattedTimeStamp);
|
case LANG_GERMAN:
|
||||||
};
|
formattedMessage = ("Dieses Program wird bis zum %s warten.").formatted(formattedTimeStamp);
|
||||||
System.getLogger("timecalculator")
|
break;
|
||||||
.log(Level.INFO, msg);
|
default:
|
||||||
|
formattedMessage = "WaitUntil will suspend until %s".formatted(formattedTimeStamp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logging the message
|
||||||
|
System.getLogger("timecalculator").log(Level.INFO, formattedMessage);
|
||||||
|
|
||||||
return userTimeInputFinal;
|
return userTimeInputFinal;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue