-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyDate.java
More file actions
170 lines (142 loc) · 5 KB
/
Copy pathPyDate.java
File metadata and controls
170 lines (142 loc) · 5 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
package org.python.modules._datetime;
import org.python.core.ArgParser;
import org.python.core.CompareOp;
import org.python.core.Py;
import org.python.core.PyLong;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.PyUnicode;
import org.python.expose.ExposedClassMethod;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedModule;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.modules.time.TimeModule;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
@ExposedType(name = "datetime.date")
public class PyDate extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyDate.class);
protected LocalDate date;
public PyDate(PyType subType, int year, int month, int day) {
super(subType);
date = LocalDate.of(year, month, day);
}
public PyDate(PyType type, LocalDate date) {
super(type);
this.date = date;
}
public PyDate(PyType type) {
super(type);
}
public PyDate(LocalDate date) {
this(TYPE, date);
}
@ExposedNew
final static PyObject date_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("date", args, keywords, "year", "month", "day");
int year = ap.getInt(0);
int month = ap.getInt(1);
int day = ap.getInt(2);
return new PyDate(subtype, year, month, day);
}
@ExposedMethod
public final PyObject date_strftime(PyObject format) {
return DatetimeModule.wrap_strftime(format, date_timetuple());
}
@ExposedMethod
public final PyObject date_timetuple() {
return DatetimeModule.build_struct_time(date, LocalTime.MIN, -1);
}
@ExposedMethod
public final PyObject date_toordinal() {
return new PyLong(date.toEpochDay());
}
@ExposedMethod
public final PyObject date_weekday() {
// A week starts from Monday in Python
return new PyLong((date.getDayOfWeek().getValue() - 1) % 7);
}
@ExposedMethod
public final PyObject date_isocalendar() {
return new PyTuple(new PyLong(date.getYear()),
new PyLong(date.getLong(ChronoField.ALIGNED_WEEK_OF_YEAR)), date_weekday());
}
@ExposedMethod
public final PyObject date_isoformat() {
return new PyUnicode(date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}
@ExposedMethod
public final PyObject date_isoweekday() {
return new PyLong(date.getDayOfWeek().getValue());
}
@ExposedMethod
public final PyObject date_replace(PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("replace", args, keywords, "year", "month", "day");
int year = ap.getInt(0, date.getYear());
int month = ap.getInt(1, date.getMonthValue());
int day = ap.getInt(2, date.getDayOfMonth());
return new PyDate(getType(), year, month, day);
}
@ExposedMethod
public final PyObject date_ctime() {
return new PyUnicode(date.format(TimeModule.DEFAULT_FORMAT_PY));
}
@ExposedClassMethod
public static final PyObject fromordinal(PyType type, long ordinal) {
Instant instant = Instant.ofEpochMilli(ordinal * 86400 * 1000);
return new PyDate(type, LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());
}
@ExposedClassMethod
public static final PyObject today(PyType type) {
return new PyDate(type, LocalDate.now());
}
@ExposedClassMethod
public static final PyObject fromtimestamp(PyType type, PyObject ts) {
return new PyDate(type, LocalDateTime.ofEpochSecond(ts.asLong(), 0, ZoneOffset.UTC).toLocalDate());
}
@Override
public PyObject __sub__(PyObject other) {
return date___sub__(other);
}
final PyObject date___sub__(PyObject other) {
if (other instanceof PyDate) {
return new PyTimeDelta(date.until(((PyDate) other).date));
}
return null;
}
@Override
public String toString() {
return String.format("%s(%d, %d, %d)", getType().fastGetName(),
date.getYear() - 1969, date.getMonthValue(), date.getDayOfMonth());
}
@Override
public PyObject richCompare(PyObject other, CompareOp op) {
if (other instanceof PyDate) {
return op.bool(date.compareTo(((PyDate) other).date));
}
return Py.NotImplemented;
}
@ExposedGet
public int year() {
return date.getYear();
}
@ExposedGet
public int month() {
return date.getMonthValue();
}
@ExposedGet
public int day() {
return date.getDayOfMonth();
}
}