From e3dd7dd938d2c010ee8e6fa9a77cde8bc7900dd0 Mon Sep 17 00:00:00 2001 From: Elyahu41 Date: Fri, 6 Nov 2026 10:51:44 -0500 Subject: [PATCH] ZmanimFormatter.formatInstant: format in the ZoneId passed in The DateTimeFormatter created by the constructor has the formatter's zone as its override zone, and an override zone converts the ZonedDateTime built from the zoneId argument back to the formatter's zone, so the argument was ignored (a UTC formatter returned the UTC time for every zoneId). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015Er4Sx7LnKzzCbgDEatxkU --- .../zmanim/util/ZmanimFormatter.java | 8 ++-- .../ZmanimFormatterFormatInstantTest.java | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterFormatInstantTest.java diff --git a/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java b/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java index 7608ab01..f5d45622 100644 --- a/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java +++ b/src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java @@ -259,16 +259,18 @@ public String format(Duration duration) { } /** - * Formats an {@link Instant} using this class's {@link #getDateTimeFormatter()}. + * Formats an {@link Instant} in the given {@link ZoneId} using this class's {@link #getDateTimeFormatter()}. * * @param instant the {@code Instant} to format. - * @param zoneId the {@link ZoneId} used to help format based on the {@code Instant}'s DST and other settings. + * @param zoneId the {@link ZoneId} to format the {@code Instant} in (including its DST and other settings). * @return the formatted String * @see #formatXSDateTime(Instant) */ public String formatInstant(Instant instant, ZoneId zoneId) { ZonedDateTime dateTime = instant.atZone(zoneId); - return this.getDateTimeFormatter().format(dateTime); + // The formatter has this ZmanimFormatter's zone as its override zone (see the constructor), and an override zone converts + // the ZonedDateTime back to that zone, so it has to be replaced by the requested one. + return this.getDateTimeFormatter().withZone(zoneId).format(dateTime); } /** diff --git a/src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterFormatInstantTest.java b/src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterFormatInstantTest.java new file mode 100644 index 00000000..a22d22ef --- /dev/null +++ b/src/test/java/com/kosherjava/zmanim/util/ZmanimFormatterFormatInstantTest.java @@ -0,0 +1,46 @@ +/* + * Zmanim Java API + * Copyright © 2004-2026 Eliyahu Hershfeld + * + * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, + * or connect to: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html + */ +package com.kosherjava.zmanim.util; + +import static org.junit.Assert.assertEquals; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + +import org.junit.Test; + +/** + * {@link ZmanimFormatter#formatInstant(Instant, ZoneId)} must format in the {@code ZoneId} passed to it, not in the formatter's + * own zone. + */ +public class ZmanimFormatterFormatInstantTest { + private static final Instant INSTANT = Instant.parse("2020-03-25T21:37:09Z"); + + @Test + public void formatsInTheRequestedZone() { + ZmanimFormatter formatter = new ZmanimFormatter(ZoneId.of("UTC")); + assertEquals("9:37:09", formatter.formatInstant(INSTANT, ZoneId.of("UTC"))); + assertEquals("5:37:09", formatter.formatInstant(INSTANT, ZoneId.of("America/New_York"))); // EDT, UTC-4 + assertEquals("11:37:09", formatter.formatInstant(INSTANT, ZoneId.of("Pacific/Kiritimati"))); // UTC+14, the next day + } + + @Test + public void worksWithAFormatterSetWithoutAZone() { + ZmanimFormatter formatter = new ZmanimFormatter(ZoneId.of("UTC")); + formatter.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); + assertEquals("2020-03-26 08:37", formatter.formatInstant(INSTANT, ZoneId.of("Australia/Sydney"))); // AEDT, UTC+11 + } +}