From 532cbc89a93d907f68868d5b3d4a7e2e2a5fcfe6 Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 20:26:59 +0200 Subject: [PATCH] Basic implementation --- .../main/kotlin/de/jotoho/waituntil/start.kt | 80 ++++++++++++++++++- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/de/jotoho/waituntil/start.kt b/app/src/main/kotlin/de/jotoho/waituntil/start.kt index 3b90fbc..54bc9ce 100644 --- a/app/src/main/kotlin/de/jotoho/waituntil/start.kt +++ b/app/src/main/kotlin/de/jotoho/waituntil/start.kt @@ -1,9 +1,64 @@ package de.jotoho.waituntil +import java.util.Locale; +import java.util.Locale.LanguageRange; +import java.text.DateFormat; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Date; +import java.util.TimeZone +import java.time.Instant; +import java.time.LocalTime; +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit + // 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 // Author: Jonas Tobias Hopusch (@jotoho) +val langGerman: String = Locale.GERMAN.getLanguage(); +val applicationOutputLanguage: String = if (Locale.getDefault().getLanguage().equals(Locale.GERMAN.getLanguage())) + Locale.GERMAN.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) { val optionDictionary = mapOf(Pair("-h", "--help")); @@ -25,8 +80,25 @@ fun main(args: Array) { words.add(arg); } - println("Content of options:"); - println(options.toString()); - println("Content of words:"); - println(words.toString()); + if (options.contains("help")) { + when (applicationOutputLanguage) { + langGerman -> println("Hilfe kommt noch. (Nicht implementiert)"); + else -> { + println("Help is yet to come. (Not implemented)"); + } + } + } + else if (words.size == 1) { + val target = calculateAndAnnounceTargetTime(words.iterator().next()); + waitUntilTimeStamp(target); + } + else { + when (applicationOutputLanguage) { + langGerman -> System.err.println("FATAL: Es wurde exact ein nicht-flag Argument erwartet. (${words.size} erhalten)"); + else -> { + System.err.println("FATAL: Expected one non-flag argument. (Got ${words.size})"); + } + } + System.exit(1); + } }