diff --git a/pom.xml b/pom.xml
index f4d403e..b44aff1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
com.synergyhelix
countdown
- 1.0.0-SNAPSHOT
+ 1.0.1-SNAPSHOT
countdown
Countdown Timer API
diff --git a/src/main/java/com/synergyhelix/countdown/CountdownController.java b/src/main/java/com/synergyhelix/countdown/CountdownController.java
index f3e4523..46207fb 100644
--- a/src/main/java/com/synergyhelix/countdown/CountdownController.java
+++ b/src/main/java/com/synergyhelix/countdown/CountdownController.java
@@ -1,9 +1,12 @@
package com.synergyhelix.countdown;
import org.apache.commons.lang3.time.DurationFormatUtils;
+import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.server.ResponseStatusException;
import javax.imageio.ImageIO;
import java.awt.*;
@@ -11,19 +14,28 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.*;
+import java.time.format.DateTimeParseException;
+import java.util.Optional;
@RestController
public class CountdownController {
@GetMapping(value = "/", produces = MediaType.IMAGE_JPEG_VALUE)
- public byte[] countdown() throws IOException {
+ public byte[] countdown(@RequestParam Optional time) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- ImageIO.write(createImage(), "jpg", outputStream);
+ ZonedDateTime inputTime;
+ try {
+ inputTime = ZonedDateTime.of(LocalDateTime.parse(time.orElse(LocalDateTime.now().toString())), ZoneOffset.UTC);
+ } catch (DateTimeParseException e) {
+ throw new ResponseStatusException(HttpStatus.BAD_REQUEST, String.format("Unable to parse the input time: %s\n %s", time, e));
+ }
+
+ ImageIO.write(createImage(inputTime), "jpg", outputStream);
return outputStream.toByteArray();
}
- private BufferedImage createImage() throws IOException {
+ private BufferedImage createImage(ZonedDateTime time) throws IOException {
final BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/chicago.jpg"));
Graphics g = image.getGraphics();
@@ -34,7 +46,7 @@ public class CountdownController {
g.setColor(Color.WHITE);
g.setFont(new Font("Courier", Font.BOLD, 57));
- g.drawString(calculateTimeUntilThing(), 15, 60);
+ g.drawString(calculateTimeUntil(time), 15, 60);
g.setColor(new Color(0xE4002B));
g.setFont(g.getFont().deriveFont(46f));
@@ -45,11 +57,13 @@ public class CountdownController {
return image;
}
- private String calculateTimeUntilThing() {
- ZonedDateTime trip = ZonedDateTime.of(LocalDateTime.parse("2022-08-13T13:00:00"), ZoneId.of("UTC"));
+ private String calculateTimeUntil(ZonedDateTime time) {
+ Duration duration = Duration.between(Instant.now(), time);
- Duration duration = Duration.between(Instant.now(), trip);
-
- return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true);
+ if (duration.isNegative()) {
+ return "00D 00H 00M 00S";
+ } else {
+ return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true);
+ }
}
}