Skip to content

Commit 8a2c8c9

Browse files
committed
[GR-74747] Use tzset timezone for time.strftime
1 parent 77cdfd7 commit 8a2c8c9

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

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)