-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyTimeTuple.java
More file actions
193 lines (174 loc) · 5.81 KB
/
PyTimeTuple.java
File metadata and controls
193 lines (174 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Copyright (c) 2005-2008 Jython Developers */
package org.python.modules.time;
import org.python.core.ArgParser;
import org.python.core.Py;
import org.python.core.PyList;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.Visitproc;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;
/**
* struct_time of the time module.
*
*/
@ExposedType(name = "time.struct_time", isBaseType = false)
public class PyTimeTuple extends PyTuple {
@ExposedGet
public PyObject tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst;
@ExposedGet
public final int n_sequence_fields = 9, n_fields = 9, n_unnamed_fields = 0;
public static final PyType TYPE = PyType.fromClass(PyTimeTuple.class);
PyTimeTuple(PyObject... vals) {
super(TYPE, vals);
tm_year = vals[0];
tm_mon = vals[1];
tm_mday = vals[2];
tm_hour = vals[3];
tm_min = vals[4];
tm_sec = vals[5];
tm_wday = vals[6];
tm_yday = vals[7];
tm_isdst =vals[8];
}
@ExposedNew
static PyObject struct_time_new(PyNewWrapper wrapper, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("struct_time", args, keywords, new String[] {"tuple"}, 1);
PyObject obj = ap.getPyObject(0);
if (obj instanceof PyTuple) {
if (obj.__len__() != 9) {
throw Py.TypeError("time.struct_time() takes a 9-sequence (1-sequence given)");
}
// tuples are immutable, so we can just use its underlying array
return new PyTimeTuple(((PyTuple)obj).getArray());
}
else {
PyList seq = new PyList(obj);
if (seq.__len__() != 9) {
throw Py.TypeError("time.struct_time() takes a 9-sequence (1-sequence given)");
}
return new PyTimeTuple((PyObject[])seq.__tojava__(PyObject[].class));
}
}
public synchronized PyObject __eq__(PyObject o) {
return struct_time___eq__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject struct_time___eq__(PyObject o) {
if (getType() != o.getType() && !getType().isSubType(o.getType())) {
return null;
}
int tl = __len__();
int ol = o.__len__();
if (tl != ol) {
return Py.False;
}
int i = cmp(this, tl, o, ol);
return (i < 0) ? Py.True : Py.False;
}
public synchronized PyObject __ne__(PyObject o) {
return struct_time___ne__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject struct_time___ne__(PyObject o) {
PyObject eq = struct_time___eq__(o);
if (eq == null) {
return null;
}
return eq.__not__();
}
/**
* Used for pickling.
*
* @return a tuple of (class, tuple)
*/
public PyObject __reduce__() {
return struct_time___reduce__();
}
@ExposedMethod
final PyObject struct_time___reduce__() {
PyTuple newargs = __getnewargs__();
return new PyTuple(getType(), newargs);
}
public PyTuple __getnewargs__() {
return new PyTuple(new PyList(getArray()));
}
@Override
public String toString() {
return struct_time_toString();
}
@ExposedMethod(names = {"__str__", "__repr__"})
final String struct_time_toString() {
return String.format("time.struct_time(tm_year=%s, tm_mon=%s, tm_mday=%s, tm_hour=%s, tm_min=%s, tm_sec=%s, tm_wday=%s, tm_yday=%s, tm_isdst=%s)",
tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst);
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal = super.traverse(visit, arg);
if (retVal != 0) {
return retVal;
}
if (tm_year != null) {
retVal = visit.visit(tm_year, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_mon != null) {
retVal = visit.visit(tm_mon, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_mday != null) {
retVal = visit.visit(tm_mday, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_hour != null) {
retVal = visit.visit(tm_hour, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_min != null) {
retVal = visit.visit(tm_min, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_sec != null) {
retVal = visit.visit(tm_sec, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_wday != null) {
retVal = visit.visit(tm_wday, arg);
if (retVal != 0) {
return retVal;
}
}
if (tm_yday != null) {
retVal = visit.visit(tm_yday, arg);
if (retVal != 0) {
return retVal;
}
}
return tm_isdst != null ? visit.visit(tm_isdst, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && (ob == tm_year || ob == tm_mon || ob == tm_mday
|| ob == tm_hour || ob == tm_min || ob == tm_sec || ob == tm_wday
|| ob == tm_yday || ob == tm_isdst || super.refersDirectlyTo(ob));
}
}