Merge branch 'ebraminio-kotlin' into kotlin

This commit is contained in:
Don Cross
2022-04-30 11:16:07 -04:00
5 changed files with 27 additions and 31 deletions

View File

@@ -12,7 +12,10 @@ RunDemo()
rm -rf build test
mkdir -p test
./gradlew jar || Fail "Cannot build jar file."
if ! ./gradlew jar test; then
cat build/test-results/test/TEST-io.github.cosinekitty.astronomy.demo.MainTests.xml
exit 1
fi
echo ""
echo "Java demos: starting"
echo ""

View File

@@ -8,9 +8,10 @@ if exist build ( rd /s/q build )
if exist test ( rd /s/q test )
md test
call gradlew.bat jar
call gradlew.bat jar test
if errorlevel 1 (
echo Cannot build jar file.
echo Cannot build/test jar file.
type build\test-results\test\TEST-io.github.cosinekitty.astronomy.demo.MainTests.xml
exit /b 1
)

View File

@@ -1,8 +1,9 @@
package io.github.cosinekitty.astronomy.demo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Date;
import io.github.cosinekitty.astronomy.*;
public class Main {
@@ -25,27 +26,14 @@ public class Main {
Date now = new Date();
return Time.fromMillisecondsSince1970(now.getTime());
}
Pattern pattern = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})(T(\\d{2}):(\\d{2})(:(\\d{2}(\\.\\d+)?))?Z)$");
Matcher matcher = pattern.matcher(args[index]);
if (matcher.find()) {
int year = Integer.parseInt(matcher.group(1));
int month = Integer.parseInt(matcher.group(2));
int day = Integer.parseInt(matcher.group(3));
int hour = 0;
int minute = 0;
double second = 0.0;
if (!matcher.group(4).isEmpty()) {
hour = Integer.parseInt(matcher.group(5));
minute = Integer.parseInt(matcher.group(6));
if (!matcher.group(7).isEmpty()) {
second = Double.parseDouble((matcher.group(8)));
}
}
return new Time(year, month, day, hour, minute, second);
try {
Instant instant = Instant.parse(args[index]);
return Time.fromMillisecondsSince1970(instant.toEpochMilli());
} catch (DateTimeParseException e) {
System.out.print("FATAL: Invalid date/time syntax: ");
System.out.println(args[index]);
return null;
}
System.out.print("FATAL: Invalid date/time syntax: ");
System.out.println(args[index]);
return null;
}
public static void main(String[] args) {
@@ -66,8 +54,7 @@ public class Main {
break;
case "now":
Date now = new Date();
Time time = Time.fromMillisecondsSince1970(now.getTime());
Time time = Time.fromMillisecondsSince1970(System.currentTimeMillis());
System.out.println(time);
rc = 0;
break;

View File

@@ -32,7 +32,7 @@ public class MoonPhase {
}
private static String quarterName(int quarter) {
switch(quarter) {
switch (quarter) {
case 0: return "New Moon";
case 1: return "First Quarter";
case 2: return "Full Moon";

View File

@@ -4,14 +4,19 @@ import io.github.cosinekitty.astronomy.Time;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MainTests {
@Test
void main() {
String time = "2022-01-01T12:00:00.000Z";
assertEquals(time, new Time(Date.from(Instant.parse(time))).toString());
timeTest();
}
private void timeTest() {
String text = "2022-04-29T12:34:45.321Z";
long millis = Instant.parse(text).toEpochMilli();
String check = Time.fromMillisecondsSince1970(millis).toString();
assertEquals(text, check);
}
}