Skip to content

Commit 5d572e6

Browse files
committed
[GR-74747] Fix timezone formatting mismatch when JVM timezone and tzset differ.
PullRequest: graalpython/4373
2 parents c4ec8a2 + 8a2c8c9 commit 5d572e6

2 files changed

Lines changed: 119 additions & 10 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.test.builtin.modules;
42+
43+
import static org.junit.Assert.assertEquals;
44+
45+
import java.util.LinkedHashMap;
46+
import java.util.Map;
47+
import java.util.TimeZone;
48+
49+
import org.graalvm.polyglot.Context;
50+
import org.graalvm.polyglot.Source;
51+
import org.junit.Test;
52+
53+
public class TimeModuleTests {
54+
55+
@Test
56+
public void strftimeTimezoneMatchesTzsetState() {
57+
TimeZone previousDefault = TimeZone.getDefault();
58+
try {
59+
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
60+
String result;
61+
try (Context context = Context.newBuilder("python").allowAllAccess(true).build()) {
62+
result = context.eval(Source.create("python", """
63+
import os
64+
import time
65+
66+
os.environ["TZ"] = "UTC"
67+
time.tzset()
68+
tt = time.localtime()
69+
70+
"\\n".join((
71+
f"tm_zone={tt.tm_zone}",
72+
f"tzname={time.tzname[tt.tm_isdst > 0]}",
73+
f"strftime_tuple={time.strftime('%Z', tt)}",
74+
f"strftime_now={time.strftime('%Z')}",
75+
))
76+
""")).asString();
77+
}
78+
79+
Map<String, String> values = parseKeyValueLines(result);
80+
String details = values.toString();
81+
assertEquals(details, values.get("tm_zone"), values.get("tzname"));
82+
assertEquals(details, values.get("tm_zone"), values.get("strftime_tuple"));
83+
assertEquals(details, values.get("tm_zone"), values.get("strftime_now"));
84+
} finally {
85+
TimeZone.setDefault(previousDefault);
86+
}
87+
}
88+
89+
private static Map<String, String> parseKeyValueLines(String output) {
90+
Map<String, String> values = new LinkedHashMap<>();
91+
for (String line : output.split("\\R")) {
92+
int separator = line.indexOf('=');
93+
values.put(line.substring(0, separator), line.substring(separator + 1));
94+
}
95+
return values;
96+
}
97+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TimeModuleBuiltins.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2026, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -739,15 +739,17 @@ private static String truncYear(int year) {
739739
return yearstr.substring(yearstr.length() - 2);
740740
}
741741

742-
private static GregorianCalendar getCalendar(int[] time) {
742+
private static GregorianCalendar getCalendar(int[] time, TimeZone timeZone) {
743743
Month month = Month.of(time[1]); // GregorianCalendar expect months that starts from 0
744-
return new GregorianCalendar(time[0], month.ordinal(), time[2], time[3], time[4], time[5]);
744+
GregorianCalendar calendar = new GregorianCalendar(timeZone);
745+
calendar.set(time[0], month.ordinal(), time[2], time[3], time[4], time[5]);
746+
return calendar;
745747
}
746748

747749
// This taken from JPython + some switches were corrected to provide the
748750
// same result as CPython
749751
@TruffleBoundary
750-
public static TruffleString format(String format, int[] date, TruffleString.FromJavaStringNode fromJavaStringNode) {
752+
public static TruffleString format(String format, int[] date, TimeZone timeZone, TruffleString.FromJavaStringNode fromJavaStringNode) {
751753
String s = "";
752754
int lastc = 0;
753755
int j;
@@ -869,7 +871,7 @@ public static TruffleString format(String format, int[] date, TruffleString.From
869871
// TODO this is not correct, CPython counts the week of year
870872
// from day of year item [8]
871873
if (cal == null) {
872-
cal = getCalendar(date);
874+
cal = getCalendar(date, timeZone);
873875
}
874876

875877
cal.setFirstDayOfWeek(Calendar.SUNDAY);
@@ -900,7 +902,7 @@ public static TruffleString format(String format, int[] date, TruffleString.From
900902
// from day of year item [8]
901903

902904
if (cal == null) {
903-
cal = getCalendar(date);
905+
cal = getCalendar(date, timeZone);
904906
}
905907
cal.setFirstDayOfWeek(Calendar.MONDAY);
906908
cal.setMinimalDaysInFirstWeek(7);
@@ -945,7 +947,7 @@ public static TruffleString format(String format, int[] date, TruffleString.From
945947
case 'Z':
946948
// timezone name
947949
if (cal == null) {
948-
cal = getCalendar(date);
950+
cal = getCalendar(date, timeZone);
949951
}
950952
// If items[8] == 1, we're in daylight savings time.
951953
// -1 means the information was not available; treat this as if not in dst.
@@ -964,6 +966,16 @@ public static TruffleString format(String format, int[] date, TruffleString.From
964966
return fromJavaStringNode.execute(s, TS_ENCODING);
965967
}
966968

969+
@TruffleBoundary
970+
public static TruffleString format(String format, int[] date, TruffleString.FromJavaStringNode fromJavaStringNode) {
971+
return format(format, date, TimeZone.getDefault(), fromJavaStringNode);
972+
}
973+
974+
@TruffleBoundary
975+
private static TimeZone getTimeZone(ZoneId currentZoneId) {
976+
return TimeZone.getTimeZone(currentZoneId);
977+
}
978+
967979
@Specialization
968980
static TruffleString formatTime(PythonModule module, TruffleString format, @SuppressWarnings("unused") PNone time,
969981
@Bind Node inliningTarget,
@@ -972,11 +984,11 @@ static TruffleString formatTime(PythonModule module, TruffleString format, @Supp
972984
@Shared("js2ts") @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
973985
@Exclusive @Cached PRaiseNode raiseNode) {
974986
ModuleState moduleState = module.getModuleState(ModuleState.class);
975-
return format(toJavaStringNode.execute(format), getIntLocalTimeStruct(moduleState.currentZoneId, (long) timeSeconds()), fromJavaStringNode);
987+
return format(toJavaStringNode.execute(format), getIntLocalTimeStruct(moduleState.currentZoneId, (long) timeSeconds()), getTimeZone(moduleState.currentZoneId), fromJavaStringNode);
976988
}
977989

978990
@Specialization
979-
static TruffleString formatTime(VirtualFrame frame, @SuppressWarnings("unused") PythonModule module, TruffleString format, PTuple time,
991+
static TruffleString formatTime(VirtualFrame frame, PythonModule module, TruffleString format, PTuple time,
980992
@Bind Node inliningTarget,
981993
@Cached SequenceStorageNodes.GetInternalObjectArrayNode getArray,
982994
@Cached PyNumberAsSizeNode asSizeNode,
@@ -985,7 +997,7 @@ static TruffleString formatTime(VirtualFrame frame, @SuppressWarnings("unused")
985997
@Shared("js2ts") @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
986998
@Exclusive @Cached PRaiseNode raiseNode) {
987999
int[] date = checkStructtime(frame, inliningTarget, time, getArray, asSizeNode, raiseNode);
988-
return format(toJavaStringNode.execute(format), date, fromJavaStringNode);
1000+
return format(toJavaStringNode.execute(format), date, getTimeZone(module.getModuleState(ModuleState.class).currentZoneId), fromJavaStringNode);
9891001
}
9901002

9911003
@Specialization

0 commit comments

Comments
 (0)