-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyTime.java
More file actions
111 lines (94 loc) · 3.38 KB
/
Copy pathPyTime.java
File metadata and controls
111 lines (94 loc) · 3.38 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
package org.python.modules._datetime;
import org.python.core.ArgParser;
import org.python.core.CompareOp;
import org.python.core.Py;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.core.PyUnicode;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.modules.time.PyTimeTuple;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
@ExposedType(name = "datetime.time")
public class PyTime extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyTime.class);
private LocalTime time;
private ZoneOffset timezone;
public PyTime(LocalTime time, ZoneOffset timezone) {
this(TYPE, time, timezone);
}
public PyTime(PyType subtype, LocalTime time, ZoneOffset timezone) {
super(subtype);
this.time = time;
this.timezone = timezone;
}
public PyTime(PyType subtype, LocalTime time) {
this(subtype, time, ZoneOffset.UTC);
}
@ExposedNew
final static PyObject time_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("time", args, keywords, "hour", "minute", "second", "microsecond", "tzinfo");
int hour = ap.getInt(0, 0);
int minute = ap.getInt(1, 0);
int second = ap.getInt(2, 0);
int microsecond = ap.getInt(3, 0);
PyObject tzinfo = ap.getPyObject(4, Py.None);
LocalTime time = LocalTime.of(hour, minute, second, microsecond * 1000);
if (tzinfo == Py.None) {
return new PyTime(subtype, time);
}
return new PyTime(subtype, time, ZoneOffset.of(tzinfo.toString()));
}
@ExposedMethod
public final String time_tzname() {
return timezone.getId();
}
@ExposedMethod
public final int time_utcoffset() {
return timezone.compareTo(ZoneOffset.UTC);
}
@ExposedMethod
public final int time_hour() {
return time.getHour();
}
@ExposedMethod
public final PyObject time_strftime(PyObject format) {
return DatetimeModule.wrap_strftime(format, timetuple());
}
@Override
public PyUnicode __str__() {
return time___str__();
}
@ExposedMethod
public PyUnicode time___str__() {
return new PyUnicode(time.toString());
}
@Override
public String toString() {
int sec = time.getSecond();
int microsec = time.getNano() / 1000;
if (microsec == 0) {
if (sec == 0) {
return String.format("%s(%d, %d)", getType().fastGetName(), time.getHour(), time.getMinute());
}
return String.format("%s(%d, %d, %d)", getType().fastGetName(), time.getHour(), time.getMinute(), time.getSecond());
} else {
return String.format("%s(%d, %d, %d, %d)", getType().fastGetName(), time.getHour(), time.getMinute(), time.getSecond(), microsec);
}
}
@Override
public PyObject richCompare(PyObject other, CompareOp op) {
if (other instanceof PyTime) {
return op.bool(time.compareTo(((PyTime) other).time));
}
return op.neq();
}
private PyObject timetuple() {
return DatetimeModule.build_struct_time(LocalDate.MIN, time, -1);
}
}