Skip to content

Commit f37d4fa

Browse files
committed
[GR-12896] time.gmtime() doesn't work without parameter.
PullRequest: graalpython/315
2 parents c549329 + ca14af6 commit f37d4fa

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ def test_new_struct_time(self):
7171
self.assertRaises(TypeError, time.struct_time, (2018, 11, 26, 17, 34, 12, 0, 340))
7272
self.assertRaises(TypeError, time.struct_time, (2018, 11, 26, 17, 34, 12, 0, 340, 9, 10, 11, 12))
7373

74+
def test_from_times(self):
75+
gt = time.gmtime()
76+
self.assertNotEqual(gt.tm_zone, None)
77+
self.assertNotEqual(gt.tm_gmtoff, None)
78+
79+
lt = time.localtime()
80+
self.assertNotEqual(lt.tm_zone, None)
81+
self.assertNotEqual(lt.tm_gmtoff, None)
82+
83+
def test_destructuring_assignment(self):
84+
t = time.struct_time((1,2,3,4,5,6,7,8,9))
85+
y,m,d,h,mi,s,wd,yd,dst = t
86+
self.assertEqual(y, 1)
87+
self.assertEqual(mi, 5)
88+
self.assertEqual(dst, 9)
89+
self.assertEqual(t.tm_zone, None)
90+
self.assertEqual(t.tm_gmtoff, None)
91+
92+
t = time.struct_time((11,12,13,14,15,16,17,18,19,20, 21))
93+
y,m,d,h,mi,s,wd,yd,dst = t
94+
self.assertEqual(y, 11)
95+
self.assertEqual(mi, 15)
96+
self.assertEqual(dst, 19)
97+
self.assertEqual(t.tm_zone, 20)
98+
self.assertEqual(t.tm_gmtoff, 21)
99+
74100
class StrftimeTests(unittest.TestCase):
75101

76102
def check_format(self, format, date, expectedStr):

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ public static double timeSeconds() {
7171

7272
@TruffleBoundary
7373
private static Object[] getTimeStruct(double seconds, boolean local) {
74-
Object[] timeStruct = new Object[9];
74+
Object[] timeStruct = new Object[11];
7575
Instant instant = Instant.ofEpochSecond((long) seconds);
76-
7776
ZoneId zone = (local) ? ZoneId.systemDefault() : ZoneId.of("GMT");
7877
ZonedDateTime zonedDateTime = LocalDateTime.ofInstant(instant, zone).atZone(zone);
7978
timeStruct[0] = zonedDateTime.getYear();
@@ -85,6 +84,8 @@ private static Object[] getTimeStruct(double seconds, boolean local) {
8584
timeStruct[6] = zonedDateTime.getDayOfWeek().getValue();
8685
timeStruct[7] = zonedDateTime.getDayOfYear();
8786
timeStruct[8] = (zonedDateTime.getZone().getRules().isDaylightSavings(instant)) ? 1 : 0;
87+
timeStruct[9] = zone.getId();
88+
timeStruct[10] = zonedDateTime.getOffset().getTotalSeconds();
8889

8990
return timeStruct;
9091
}
@@ -94,6 +95,17 @@ private static Object[] getTimeStruct(double seconds, boolean local) {
9495
@GenerateNodeFactory
9596
@TypeSystemReference(PythonArithmeticTypes.class)
9697
public abstract static class PythonGMTimeNode extends PythonBuiltinNode {
98+
99+
@Specialization
100+
public PTuple gmtime(@SuppressWarnings("unused") PNone seconds) {
101+
return factory().createTuple(getTimeStruct(timeSeconds(), false));
102+
}
103+
104+
@Specialization
105+
public PTuple gmtime(long seconds) {
106+
return factory().createTuple(getTimeStruct(seconds, false));
107+
}
108+
97109
@Specialization
98110
public PTuple gmtime(double seconds) {
99111
return factory().createTuple(getTimeStruct(seconds, false));

graalpython/lib-graalpython/time.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,30 @@
4040

4141
def make_struct_time():
4242
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"]
43+
fields = ["tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"]
4444
struct_time_type = make_named_tuple_class("struct_time", fields)
4545

4646
class struct_time(struct_time_type):
47-
47+
4848
def __new__(cls, iterable):
49+
def create_struct(iter, zone, gmtoff):
50+
result = tuple.__new__(cls, iter)
51+
result.tm_zone = zone
52+
result.tm_gmtoff = gmtoff
53+
return result
54+
4955
count = len(iterable)
5056
if (count < 9):
5157
raise TypeError("time.struct_time() takes an at least 9-sequence (%d-sequence given)" % count)
5258
if (count > 11):
5359
raise TypeError("time.struct_time() takes an at most 11-sequence (%d-sequence given)" % count)
5460
if count == 11:
55-
return tuple.__new__(cls, iterable)
61+
return create_struct(iterable[0:9], iterable[9], iterable[10])
5662
if count == 10:
57-
return tuple.__new__(cls, iterable + (None, ))
63+
return create_struct(iterable[0:9], iterable[9], None)
5864
if count == 9:
59-
return tuple.__new__(cls, iterable + (None, None))
65+
return create_struct(iterable, None, None)
66+
6067

6168
def __repr__(self):
6269
text = "{}(".format(self.__class__.__name__)
@@ -74,8 +81,10 @@ def __repr__(self):
7481
struct_time = make_struct_time()
7582
del make_struct_time
7683

84+
_STRUCT_TM_ITEMS = 11
85+
7786
@__builtin__
78-
def gmtime(seconds):
87+
def gmtime(seconds=None):
7988
return struct_time(__truffle_gmtime_tuple__(seconds))
8089

8190

0 commit comments

Comments
 (0)