Skip to content

Commit d66c174

Browse files
committed
[GR-12718] time.struct_time takes at most 2 arguments.
PullRequest: graalpython/300
2 parents 9cab133 + 295f596 commit d66c174

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
# SOFTWARE.
3939

4040
import time
41-
41+
import unittest
4242

4343
def test_sleep():
4444
start = time.time()
@@ -56,3 +56,20 @@ def test_monotonic():
5656
times = [time.monotonic() for _ in range(100)]
5757
for t1, t2 in zip(times[:-1], times[1:]):
5858
assert t1 <= t2
59+
60+
class StructTimeTests(unittest.TestCase):
61+
62+
def test_new_struct_time(self):
63+
t = time.struct_time((2018, 11, 26, 17, 34, 12, 0, 340, -1))
64+
self.assertEqual(t.tm_year, 2018)
65+
self.assertEqual(t.tm_mon, 11)
66+
self.assertEqual(t.tm_mday, 26)
67+
self.assertEqual(t[2], 26)
68+
self.assertEqual(t.tm_zone, None)
69+
70+
self.assertRaises(TypeError, time.struct_time, (2018, 11, 26, 17, 34, 12, 0, 340))
71+
self.assertRaises(TypeError, time.struct_time, (2018, 11, 26, 17, 34, 12, 0, 340, 9, 10, 11, 12))
72+
73+
74+
75+

graalpython/lib-graalpython/time.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,47 @@
3838
# SOFTWARE.
3939

4040

41-
@__builtin__
42-
def struct_time(*args, **kwargs):
43-
from collections import namedtuple
41+
def make_struct_time():
42+
from _descriptor import make_named_tuple_class
43+
fields = ["tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst", "tm_zone", "tm_gmtoff"]
44+
struct_time_type = make_named_tuple_class("struct_time", fields)
45+
46+
class struct_time(struct_time_type):
47+
48+
def __new__(cls, iterable):
49+
count = len(iterable)
50+
if (count < 9):
51+
raise TypeError("time.struct_time() takes an at least 9-sequence (%d-sequence given)" % count)
52+
if (count > 11):
53+
raise TypeError("time.struct_time() takes an at most 11-sequence (%d-sequence given)" % count)
54+
if count == 11:
55+
return tuple.__new__(cls, iterable)
56+
if count == 10:
57+
return tuple.__new__(cls, iterable + (None, ))
58+
if count == 9:
59+
return tuple.__new__(cls, iterable + (None, None))
4460

45-
nt = namedtuple("struct_time", [
46-
"tm_year",
47-
"tm_mon",
48-
"tm_mday",
49-
"tm_hour",
50-
"tm_min",
51-
"tm_sec",
52-
"tm_wday",
53-
"tm_yday",
54-
"tm_isdst",
55-
])
61+
def __repr__(self):
62+
text = "{}(".format(self.__class__.__name__)
63+
n = len(self)
64+
for i in range(n):
65+
if self[i] != None:
66+
if i > 0 :
67+
text = text + ", "
68+
text = text + "{}={}".format(fields[i], str(self[i]))
69+
text = text + ')'
70+
return text
71+
return struct_time
5672

57-
return nt(*args, **kwargs)
5873

74+
struct_time = make_struct_time()
75+
del make_struct_time
5976

6077
@__builtin__
6178
def gmtime(seconds):
62-
return struct_time(*__truffle_gmtime_tuple__(seconds))
79+
return struct_time(__truffle_gmtime_tuple__(seconds))
6380

6481

6582
@__builtin__
6683
def localtime(seconds):
67-
return struct_time(*__truffle_localtime_tuple__(seconds))
84+
return struct_time(__truffle_localtime_tuple__(seconds))

0 commit comments

Comments
 (0)