Post-Implementation refactoring of application #9
3 changed files with 69 additions and 38 deletions
25
app/src/main/kotlin/de/jotoho/waituntil/sleeping.kt
Normal file
25
app/src/main/kotlin/de/jotoho/waituntil/sleeping.kt
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package de.jotoho.waituntil
|
||||||
|
|
||||||
|
import java.lang.Math
|
||||||
|
import java.time.Instant
|
||||||
|
import java.time.ZonedDateTime
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
import java.time.format.FormatStyle
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
|
public fun waitUntilTimeStamp(timestamp: ZonedDateTime) {
|
||||||
|
Thread.sleep(Math.max(Instant.now().until(timestamp, ChronoUnit.MILLIS), 0))
|
||||||
|
|
||||||
|
val formattedTimeStamp: String =
|
||||||
|
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
|
||||||
|
.withZone(TimeZone.getDefault().toZoneId())
|
||||||
|
.format(Instant.now())
|
||||||
|
|
||||||
|
when (applicationOutputLanguage) {
|
||||||
|
langGerman -> System.err.println("Erfolgreich bis $formattedTimeStamp gewartet!")
|
||||||
|
else -> {
|
||||||
|
System.err.println("Successfully waited until $formattedTimeStamp")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package de.jotoho.waituntil
|
||||||
|
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
import java.time.format.FormatStyle
|
|
||||||
import java.util.TimeZone
|
import java.util.TimeZone
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.time.LocalTime
|
import java.time.LocalTime
|
||||||
|
@ -10,6 +9,9 @@ import java.time.LocalDate
|
||||||
import java.time.ZonedDateTime
|
import java.time.ZonedDateTime
|
||||||
import java.time.temporal.ChronoUnit
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
import de.jotoho.waituntil.waitUntilTimeStamp
|
||||||
|
import de.jotoho.waituntil.calculateAndAnnounceTargetTime
|
||||||
|
|
||||||
// This file contains the main function and other utility function necessary for interpreting the terminal arguments.
|
// This file contains the main function and other utility function necessary for interpreting the terminal arguments.
|
||||||
// See README.md and LICENSE.md for license information
|
// See README.md and LICENSE.md for license information
|
||||||
// Author: Jonas Tobias Hopusch (@jotoho)
|
// Author: Jonas Tobias Hopusch (@jotoho)
|
||||||
|
@ -19,43 +21,6 @@ val applicationOutputLanguage: String = if (Locale.getDefault().getLanguage().eq
|
||||||
Locale.GERMAN.getLanguage()
|
Locale.GERMAN.getLanguage()
|
||||||
else Locale.ENGLISH.getLanguage();
|
else Locale.ENGLISH.getLanguage();
|
||||||
|
|
||||||
fun waitUntilTimeStamp(timestamp: ZonedDateTime) {
|
|
||||||
Thread.sleep(
|
|
||||||
Math.max(Instant.now().until(timestamp, ChronoUnit.MILLIS), 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
val formattedTimeStamp: String = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
|
|
||||||
.withZone(TimeZone.getDefault().toZoneId())
|
|
||||||
.format(Instant.now());
|
|
||||||
|
|
||||||
when (applicationOutputLanguage) {
|
|
||||||
langGerman -> System.err.println("Erfolgreich bis $formattedTimeStamp gewartet!");
|
|
||||||
else -> {
|
|
||||||
System.err.println("Successfully waited until $formattedTimeStamp");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun calculateAndAnnounceTargetTime(userTimeInputRaw: String): ZonedDateTime {
|
|
||||||
val userTimeInputRelative = LocalTime.parse(userTimeInputRaw);
|
|
||||||
val userTimeInputAbsolute = ZonedDateTime.of(LocalDate.now(), userTimeInputRelative, TimeZone.getDefault().toZoneId());
|
|
||||||
|
|
||||||
val userTimeInputFinal = if (Instant.now().isBefore(userTimeInputAbsolute.toInstant())) userTimeInputAbsolute else userTimeInputAbsolute.plusDays(1);
|
|
||||||
|
|
||||||
val formattedTimeStamp = userTimeInputFinal.format(
|
|
||||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
|
|
||||||
);
|
|
||||||
|
|
||||||
when (applicationOutputLanguage) {
|
|
||||||
langGerman -> System.err.println("Dieses Program wird bis zum $formattedTimeStamp warten.");
|
|
||||||
else -> {
|
|
||||||
println("WaitUntil will suspend until $formattedTimeStamp");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return userTimeInputFinal;
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val optionDictionary = mapOf(Pair("-h", "--help"));
|
val optionDictionary = mapOf(Pair("-h", "--help"));
|
||||||
|
|
||||||
|
|
41
app/src/main/kotlin/de/jotoho/waituntil/timecalc.kt
Normal file
41
app/src/main/kotlin/de/jotoho/waituntil/timecalc.kt
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
public fun calculateAndAnnounceTargetTime(userTimeInputRaw: String): ZonedDateTime {
|
||||||
|
val userTimeInputRelative = LocalTime.parse(userTimeInputRaw)
|
||||||
|
val userTimeInputAbsolute =
|
||||||
|
ZonedDateTime.of(
|
||||||
|
LocalDate.now(),
|
||||||
|
userTimeInputRelative,
|
||||||
|
TimeZone.getDefault().toZoneId()
|
||||||
|
)
|
||||||
|
|
||||||
|
val userTimeInputFinal =
|
||||||
|
if (Instant.now().isBefore(userTimeInputAbsolute.toInstant()))
|
||||||
|
userTimeInputAbsolute
|
||||||
|
else userTimeInputAbsolute.plusDays(1)
|
||||||
|
|
||||||
|
val formattedTimeStamp =
|
||||||
|
userTimeInputFinal.format(
|
||||||
|
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
|
||||||
|
)
|
||||||
|
|
||||||
|
when (applicationOutputLanguage) {
|
||||||
|
langGerman ->
|
||||||
|
System.err.println(
|
||||||
|
"Dieses Program wird bis zum $formattedTimeStamp warten."
|
||||||
|
)
|
||||||
|
else -> {
|
||||||
|
println("WaitUntil will suspend until $formattedTimeStamp")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return userTimeInputFinal
|
||||||
|
}
|
Loading…
Reference in a new issue