Skip to content

Commit 83c80a1

Browse files
committed
Date and time are printed in specific target timezone; default is host's timezone; CINDY_TARGET_ZONE will override.
1 parent b1d4f51 commit 83c80a1

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- `CINDY_DATA`: File system folder where Cindy persists internal data. For example, Cindy rembembers the instant when it last pulled calenders, so it understands which calender events are new/modified, and which are old. The default is `.`
1818
- `CINDY_POLLING_SECONDS`: Time between two calendar polls. The default frequency is one minute, i. e. `60`.
1919
- `CINDY_CALENDAR_SOURCES`: Comma-separated list of download URLs of calendars to poll. There is no default.
20+
- `CINDY_TARGET_ZONE`: Timezone to use when formatting event message (e. g. "Europe/Berlin" will tell German local time in event message). Default is host's timezone.
2021

2122

2223
## Build

src/main/java/CindyBootstrap.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@
6565
*/
6666
public final class CindyBootstrap {
6767
private static final int MAXIMUM_MASTODON_MESSAGE_LENGTH = 500;
68-
private static final DateTimeFormatter GERMAN_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern("d. LLLL yyyy HH:mm", GERMANY);
69-
private static final DateTimeFormatter GERMAN_DATE_FORMATTER = DateTimeFormatter.ofPattern("d. LLLL yyyy", GERMANY);
7068
private static final Logger LOGGER = Logger.getLogger(CindyBootstrap.class.getName());
71-
private static final ZoneId EUROPE_BERLIN = ZoneId.of("Europe/Berlin");
7269

7370
public static void main(final String[] args) throws InterruptedException, ExecutionException {
71+
final var targetZone = Optional.ofNullable(System.getenv("CINDY_TARGET_ZONE")).map(ZoneId::of).orElseGet(ZoneId::systemDefault);
72+
final var TARGET_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern("d. LLLL yyyy HH:mm", GERMANY).withZone(targetZone);
73+
final var TARGET_DATE_FORMATTER = DateTimeFormatter.ofPattern("d. LLLL yyyy", GERMANY).withZone(targetZone);
7474
final var mastodonHost = System.getenv("CINDY_MASTODON_HOST");
7575
final var mastodonAccessToken = System.getenv("CINDY_MASTODON_ACCESS_TOKEN");
7676
final var lastRunFile = Path.of(System.getenv().getOrDefault("CINDY_DATA", "."), "lastRun");
@@ -177,8 +177,8 @@ public void run() {
177177
switch (event.begin()) {
178178
case ZonedDateTime zonedDatetime when zonedDatetime.toInstant().isAfter(startOfPullingCalendar) -> true;
179179
case OffsetDateTime offsetDateTime when offsetDateTime.toInstant().isAfter(startOfPullingCalendar) -> true;
180-
case LocalDateTime localDateTime when localDateTime.atZone(EUROPE_BERLIN).toInstant().isAfter(startOfPullingCalendar) -> true;
181-
case LocalDate localDate when localDate.atStartOfDay().atZone(EUROPE_BERLIN).toInstant().isAfter(startOfPullingCalendar) -> true;
180+
case LocalDateTime localDateTime when localDateTime.atZone(targetZone).toInstant().isAfter(startOfPullingCalendar) -> true;
181+
case LocalDate localDate when localDate.atStartOfDay().atZone(targetZone).toInstant().isAfter(startOfPullingCalendar) -> true;
182182
case ZonedDateTime zonedDatetime -> {
183183
LOGGER.finer(() -> "Ignoring event because it begins in the past: '%s'.".formatted(event.uid()));
184184
yield false;
@@ -223,10 +223,10 @@ public void run() {
223223
case null:
224224
break;
225225
case LocalDate ld:
226-
message.append("\n📅 ").append(GERMAN_DATE_FORMATTER.format(ld));
226+
message.append("\n📅 ").append(TARGET_DATE_FORMATTER.format(ld));
227227
break;
228228
default:
229-
message.append("\n📅 ").append(GERMAN_TIMESTAMP_FORMATTER.format(event.begin()));
229+
message.append("\n📅 ").append(TARGET_TIMESTAMP_FORMATTER.format(event.begin()));
230230
}
231231
if (event.location() != null)
232232
message.append("\n🏠️ ").append(event.location());

0 commit comments

Comments
 (0)