Skip to content

Commit 21ce4a0

Browse files
committed
Fix time.strftime() and implement missing formatters - G, V, u
1 parent fe9c3e8 commit 21ce4a0

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_time.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def test_sec(self):
242242
self.assertRaises(ValueError, time.strftime, "%S", time.struct_time((2018, 8, 2, 10, -1, 30, 1, 1, 0)))
243243
self.assertRaises(ValueError, time.strftime, "%S", time.struct_time((2018, 8, 2, 24, 62, 30, 1, 1, 0)))
244244

245-
def test_weekDay(self):
245+
def test_weekDayw(self):
246246
self.check_format("%w", (2018, 11, 28, 10, 0, 0, -1, 1, 0), '0')
247247
self.check_format("%w", (2018, 11, 28, 10, 0, 0, 0, 1, 0), '1')
248248
self.check_format("%w", (2018, 11, 25, 11, 12, 2, 3, 1, 0), '4')
@@ -251,6 +251,20 @@ def test_weekDay(self):
251251
self.check_format("%w", (2018, 11, 24, 23, 20, 61, 999, 1, 0), '6')
252252
self.assertRaises(ValueError, time.strftime, "%w", time.struct_time((2018, 8, 2, 10, 20, 30, -2, 1, 0)))
253253

254+
def test_weekDayu(self):
255+
self.check_format("%u", (2018, 11, 28, 10, 0, 0, 2, 332, -1), '3')
256+
self.check_format("%u", (2018, 11, 25, 11, 12, 2, 6, 329, -1), '7')
257+
self.check_format("%u", (2018, 11, 24, 23, 20, 0, 5, 328, -1), '6')
258+
self.assertRaises(ValueError, time.strftime, "%u", time.struct_time((2018, 8, 2, 10, 20, 30, -2, 1, 0)))
259+
260+
def test_week_of_yearU(self):
261+
self.check_format("%U", (2018, 11, 28, 0, 0, 0, 2, 332, -1), '47')
262+
self.check_format("%U", (2018, 1, 7, 0, 0, 0, 6, 7, -1), '01')
263+
264+
def test_week_of_yearV(self):
265+
self.check_format("%V", (2018, 11, 28, 0, 0, 0, 2, 332, -1), '48')
266+
self.check_format("%V", (2018, 1, 7, 0, 0, 0, 6, 7, -1), '01')
267+
254268
def test_YearY(self):
255269
self.check_format("%Y", (2018, 11, 28, 10, 0, 0, -1, 1, 0), '2018')
256270
self.check_format("%Y", (18, 11, 28, 10, 0, 0, 0, 1, 0), '18')
@@ -266,11 +280,18 @@ def test_Yeary(self):
266280
#self.check_format("%y", (-365, 11, 24, 23, 20, 61, 6, 1, 0), '65')
267281
self.check_format("%y", (17829, 11, 24, 23, 20, 61, 7, 1, 0), '29')
268282

283+
def test_YearG(self):
284+
self.check_format("%G", (2024, 1, 1, 1, 1, 1, 1, 1, 1), '2024') # Jan 1, 2024 is Monday
285+
269286
def test_wrongInput(self):
270287
self.assertRaises(TypeError, time.strftime, 10, (2018, 8, 2, 10, 20, 30, -2, 1, 0))
271288
self.assertRaises(TypeError, time.strftime, "%w", 10)
272289
self.assertRaises(TypeError, time.strftime, "%w", (2018, 11, 29))
273290

291+
def test_trailing_percent(self):
292+
self.check_format("%", (2018, 8, 8, 0, 24, 0, 3, 1, 0), "%")
293+
self.check_format("abc%", (2018, 8, 8, 0, 24, 0, 3, 1, 0), "abc%")
294+
274295
def test_padding(self):
275296
self.check_format("%d", (2018, 8, 8, 5, 24, 10, 3, 1, 0), '08')
276297
self.check_format("%-d", (2018, 8, 8, 5, 24, 10, 3, 1, 0), '8')

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@
3939
import java.lang.management.ManagementFactory;
4040
import java.text.DateFormatSymbols;
4141
import java.time.Instant;
42+
import java.time.LocalDate;
4243
import java.time.LocalDateTime;
4344
import java.time.Month;
4445
import java.time.Year;
4546
import java.time.ZoneId;
4647
import java.time.ZonedDateTime;
48+
import java.time.temporal.IsoFields;
4749
import java.util.Calendar;
4850
import java.util.GregorianCalendar;
4951
import java.util.List;
@@ -761,6 +763,7 @@ private static TruffleString format(String format, int[] date, TruffleString.Fro
761763
// there's a bare % at the end of the string. Python lets
762764
// this go by just sticking a % at the end of the result
763765
// string
766+
s = s + format.substring(lastc, i);
764767
s = s + "%";
765768
break;
766769
}
@@ -807,6 +810,12 @@ private static TruffleString format(String format, int[] date, TruffleString.Fro
807810
// day of month (01-31)
808811
s = s + (pad ? format("%02d", date[TM_MDAY]) : format("%d", date[TM_MDAY]));
809812
break;
813+
case 'G': {
814+
LocalDate localDate = LocalDate.of(date[TM_YEAR], date[TM_MON], date[TM_MDAY]);
815+
j = localDate.get(IsoFields.WEEK_BASED_YEAR);
816+
s = s + format("%04d", j);
817+
break;
818+
}
810819
case 'H':
811820
// hour (00-23)
812821
s = s + (pad ? format("%02d", date[TM_HOUR]) : format("%d", date[TM_HOUR]));
@@ -845,6 +854,12 @@ private static TruffleString format(String format, int[] date, TruffleString.Fro
845854
// seconds (00-61)
846855
s = s + (pad ? format("%02d", date[TM_SEC]) : format("%d", date[TM_SEC]));
847856
break;
857+
case 'u': {
858+
LocalDate localDate = LocalDate.of(date[TM_YEAR], date[TM_MON], date[TM_MDAY]);
859+
j = localDate.getDayOfWeek().getValue();
860+
s = s + format("%d", j);
861+
break;
862+
}
848863
case 'U':
849864
// week of year (sunday is first day) (00-53). all days in
850865
// new year preceding first sunday are considered to be in
@@ -864,6 +879,12 @@ private static TruffleString format(String format, int[] date, TruffleString.Fro
864879
}
865880
s = s + (pad ? format("%02d", j) : format("%d", j));
866881
break;
882+
case 'V': {
883+
LocalDate localDate = LocalDate.of(date[TM_YEAR], date[TM_MON], date[TM_MDAY]);
884+
j = localDate.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
885+
s = s + format("%02d", j);
886+
break;
887+
}
867888
case 'w':
868889
// weekday as decimal (0=Sunday-6)
869890
j = (date[TM_WDAY] + 1) % 7;
@@ -934,9 +955,7 @@ private static TruffleString format(String format, int[] date, TruffleString.Fro
934955
s = s + "%";
935956
break;
936957
default:
937-
// TBD: should this raise a ValueError?
938958
s = s + "%" + format.charAt(i);
939-
i++;
940959
break;
941960
}
942961
lastc = i + 1;

0 commit comments

Comments
 (0)