-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyTimeDelta.java
More file actions
169 lines (141 loc) · 4.76 KB
/
Copy pathPyTimeDelta.java
File metadata and controls
169 lines (141 loc) · 4.76 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
package org.python.modules._datetime;
import org.python.core.ArgParser;
import org.python.core.CompareOp;
import org.python.core.PyLong;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import java.time.Duration;
import java.time.Period;
@ExposedType(name = "datetime.timedelta")
public class PyTimeDelta extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyTimeDelta.class);
private Duration delta;
public PyTimeDelta(Period period) {
delta = Duration.ofSeconds(period.getDays() * 86400);
}
public PyTimeDelta(Duration duration) {
super(TYPE);
delta = duration;
}
public PyTimeDelta(int days) {
this(days, 0, 0);
}
public PyTimeDelta(int days, int seconds, int microseconds) {
this(TYPE, days, seconds, microseconds);
}
public PyTimeDelta(PyType subtype, int days, int seconds, int microseconds) {
super(subtype);
delta = Duration.ofDays(days).plusSeconds(seconds).plusNanos(microseconds);
}
@ExposedNew
final static PyObject timedelta_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("timedelta", args, keywords, "days", "seconds", "microseconds", "milliseconds", "minutes", "hours", "weeks");
int days = ap.getInt(0, 0);
int seconds = ap.getInt(1, 0);
int microseconds = ap.getInt(2, 0);
int millisecond = ap.getInt(3, 0);
int minutes = ap.getInt(4, 0);
int hours = ap.getInt(5, 0);
seconds += minutes * 60 + hours * 3600;
microseconds += millisecond * 1000;
return new PyTimeDelta(subtype, days, seconds, microseconds);
}
@Override
public PyObject richCompare(PyObject other, CompareOp op) {
if (other instanceof PyTimeDelta) {
return op.bool(delta.compareTo(((PyTimeDelta) other).delta));
}
return op.neq();
}
@Override
public String toString() {
if (getNano() != 0) {
return String.format("%s(%d, %d, %d)", getType().fastGetName(), getDays(), getSeconds(), getNano());
}
if (getSeconds() != 0) {
return String.format("%s(%d, %d)", getType().fastGetName(), getDays(), getSeconds());
}
return String.format("%s(%d)", getType().fastGetName(), getDays());
}
@Override
public PyObject __neg__() {
return timedelta___neg__();
}
@ExposedMethod
final PyObject timedelta___neg__() {
return new PyTimeDelta(delta.negated());
}
@ExposedMethod
public boolean __bool__() {
return timedelta___bool__();
}
public boolean timedelta___bool__() {
return !delta.isZero();
}
@Override
public PyObject __idiv__(PyObject other) {
return timedelta___idiv__(other);
}
final PyObject timedelta___idiv__(PyObject other) {
if (other instanceof PyTimeDelta) {
return new PyTimeDelta(delta.dividedBy(((PyTimeDelta) other).delta.getSeconds()));
} else if (other instanceof PyLong) {
return new PyTimeDelta(delta.dividedBy(other.asLong()));
}
return null;
}
@Override
public PyObject __floordiv__(PyObject other) {
return timedelta___floordiv__(other);
}
final PyObject timedelta___floordiv__(PyObject other) {
return timedelta___idiv__(other);
}
@Override
public PyObject __mul__(PyObject other) {
return timedelta___mul__(other);
}
@ExposedMethod
public PyObject timedelta___mul__(PyObject other) {
if (other instanceof PyLong) {
return new PyTimeDelta(delta.multipliedBy(other.asLong()));
}
return null;
}
@Override
public PyObject __rmul__(PyObject other) {
return timedelta___rmul__(other);
}
@ExposedMethod
public PyObject timedelta___rmul__(PyObject other) {
if (other instanceof PyLong) {
return new PyTimeDelta(delta.multipliedBy(other.asLong()));
}
return null;
}
@ExposedMethod
public final PyObject timedelta_total_seconds() {
return new PyLong(delta.getSeconds());
}
@ExposedGet(name = "days")
public long getDays() {
return delta.toDays();
}
@ExposedGet(name = "seconds")
public long getSeconds() {
return delta.minusDays(getDays()).getSeconds();
}
@ExposedGet(name = "microseconds")
public long getNano() {
return delta.getNano();
}
Duration toDuration() {
return delta;
}
}