Basic Implementation of Time Parsing and waiting logic #3
4 changed files with 93 additions and 5 deletions
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
|
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
|
||||||
id("org.jetbrains.kotlin.jvm") version "1.5.0"
|
id("org.jetbrains.kotlin.jvm") version "1.+"
|
||||||
|
|
||||||
// Apply the application plugin to add support for building a CLI application in Java.
|
// Apply the application plugin to add support for building a CLI application in Java.
|
||||||
application
|
application
|
||||||
|
@ -36,6 +36,19 @@ dependencies {
|
||||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
|
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_16
|
||||||
|
targetCompatibility = JavaVersion.VERSION_16
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks {
|
||||||
|
compileKotlin {
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "16"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
// Define the main class for the application.
|
// Define the main class for the application.
|
||||||
mainClass.set("de.jotoho.waituntil.StartKt")
|
mainClass.set("de.jotoho.waituntil.StartKt")
|
||||||
|
|
|
@ -1,9 +1,61 @@
|
||||||
package de.jotoho.waituntil
|
package de.jotoho.waituntil
|
||||||
|
|
||||||
|
import java.util.Locale
|
||||||
|
import java.time.format.DateTimeFormatter
|
||||||
|
import java.time.format.FormatStyle
|
||||||
|
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.
|
// 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)
|
||||||
|
|
||||||
|
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<String>) {
|
fun main(args: Array<String>) {
|
||||||
val optionDictionary = mapOf(Pair("-h", "--help"));
|
val optionDictionary = mapOf(Pair("-h", "--help"));
|
||||||
|
|
||||||
|
@ -25,8 +77,25 @@ fun main(args: Array<String>) {
|
||||||
words.add(arg);
|
words.add(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Content of options:");
|
if (options.contains("help")) {
|
||||||
println(options.toString());
|
when (applicationOutputLanguage) {
|
||||||
println("Content of words:");
|
langGerman -> println("Hilfe kommt noch. (Nicht implementiert)");
|
||||||
println(words.toString());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
3
compile.sh
Executable file
3
compile.sh
Executable file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt -jvm-target 16 -include-runtime -d waituntil.jar
|
3
run.sh
Executable file
3
run.sh
Executable file
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
java -jar waituntil.jar $*
|
Loading…
Reference in a new issue