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 + } +}