Skip to content

Commit f0a7e59

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

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

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

Lines changed: 19 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,11 @@ 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+
967974
@Specialization
968975
static TruffleString formatTime(PythonModule module, TruffleString format, @SuppressWarnings("unused") PNone time,
969976
@Bind Node inliningTarget,
@@ -972,11 +979,12 @@ static TruffleString formatTime(PythonModule module, TruffleString format, @Supp
972979
@Shared("js2ts") @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
973980
@Exclusive @Cached PRaiseNode raiseNode) {
974981
ModuleState moduleState = module.getModuleState(ModuleState.class);
975-
return format(toJavaStringNode.execute(format), getIntLocalTimeStruct(moduleState.currentZoneId, (long) timeSeconds()), fromJavaStringNode);
982+
TimeZone timeZone = TimeZone.getTimeZone(moduleState.currentZoneId);
983+
return format(toJavaStringNode.execute(format), getIntLocalTimeStruct(moduleState.currentZoneId, (long) timeSeconds()), timeZone, fromJavaStringNode);
976984
}
977985

978986
@Specialization
979-
static TruffleString formatTime(VirtualFrame frame, @SuppressWarnings("unused") PythonModule module, TruffleString format, PTuple time,
987+
static TruffleString formatTime(VirtualFrame frame, PythonModule module, TruffleString format, PTuple time,
980988
@Bind Node inliningTarget,
981989
@Cached SequenceStorageNodes.GetInternalObjectArrayNode getArray,
982990
@Cached PyNumberAsSizeNode asSizeNode,
@@ -985,7 +993,8 @@ static TruffleString formatTime(VirtualFrame frame, @SuppressWarnings("unused")
985993
@Shared("js2ts") @Cached TruffleString.FromJavaStringNode fromJavaStringNode,
986994
@Exclusive @Cached PRaiseNode raiseNode) {
987995
int[] date = checkStructtime(frame, inliningTarget, time, getArray, asSizeNode, raiseNode);
988-
return format(toJavaStringNode.execute(format), date, fromJavaStringNode);
996+
TimeZone timeZone = TimeZone.getTimeZone(module.getModuleState(ModuleState.class).currentZoneId);
997+
return format(toJavaStringNode.execute(format), date, timeZone, fromJavaStringNode);
989998
}
990999

9911000
@Specialization

0 commit comments

Comments
 (0)