/
TimestampParser.java
236 lines (215 loc) · 10.3 KB
/
TimestampParser.java
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.embulk.spi.time;
import com.google.common.base.Optional;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneOffset;
import org.embulk.config.Config;
import org.embulk.config.ConfigDefault;
public class TimestampParser {
private TimestampParser(final TimestampParserLegacy delegate) {
this.delegate = delegate;
}
TimestampParser() {
this(null);
}
// Calling the constructor directly is deprecated, but the constructor is kept for plugin compatibility.
// Use TimestampParser.of(Task, TimestampColumnOption) instead.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public TimestampParser(final Task task,
final TimestampColumnOption columnOption) {
this(TimestampParserLegacy.of(
columnOption.getFormat().or(task.getDefaultTimestampFormat()),
TimeZoneIds.parseZoneIdWithJodaAndRubyZoneTab(
columnOption.getTimeZoneId().or(task.getDefaultTimeZoneId())),
TimeZoneIds.parseJodaDateTimeZone(
columnOption.getTimeZoneId().or(task.getDefaultTimeZoneId())),
columnOption.getDate().or(task.getDefaultDate())));
}
// Using Joda-Time is deprecated, but the constructor receives org.joda.time.DateTimeZone for plugin compatibility.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public TimestampParser(final String formatString,
final org.joda.time.DateTimeZone defaultJodaDateTimeZone) {
this(TimestampParserLegacy.of(
formatString,
TimeZoneIds.convertJodaDateTimeZoneToZoneId(defaultJodaDateTimeZone),
defaultJodaDateTimeZone,
1970,
1,
1));
}
// Using Joda-Time is deprecated, but the constructor receives org.joda.time.DateTimeZone for plugin compatibility.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public TimestampParser(final String formatString,
final org.joda.time.DateTimeZone defaultJodaDateTimeZone,
final String defaultDate) {
this(TimestampParserLegacy.of(
formatString,
TimeZoneIds.convertJodaDateTimeZoneToZoneId(defaultJodaDateTimeZone),
defaultJodaDateTimeZone,
defaultDate));
}
// "default_date" is deprecated, but the creator method is kept for plugin compatibility.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public static TimestampParser of(final String pattern,
final String defaultZoneIdString,
final String defaultDateString) {
if (pattern.startsWith("java:")) {
// TODO: Warn if "default_date" is set, which is unavailable for "java:".
final ZoneOffset zoneOffset;
if (defaultZoneIdString.equals("UTC")) {
zoneOffset = ZoneOffset.UTC;
} else {
zoneOffset = ZoneOffset.of(defaultZoneIdString);
}
return TimestampParserJava.of(pattern.substring(5),
zoneOffset);
} else if (pattern.startsWith("ruby:")) {
// TODO: Warn if "default_date" is set, which is unavailable for "ruby:".
final ZoneOffset zoneOffset;
if (defaultZoneIdString.equals("UTC")) {
zoneOffset = ZoneOffset.UTC;
} else {
zoneOffset = ZoneOffset.of(defaultZoneIdString);
}
return TimestampParserRuby.of(pattern.substring(5),
zoneOffset);
} else {
return TimestampParserLegacy.of(pattern,
TimeZoneIds.parseZoneIdWithJodaAndRubyZoneTab(defaultZoneIdString),
TimeZoneIds.parseJodaDateTimeZone(defaultZoneIdString),
defaultDateString);
}
}
public static TimestampParser of(final String pattern,
final String defaultZoneIdString) {
if (pattern.startsWith("java:")) {
final ZoneOffset zoneOffset;
if (defaultZoneIdString.equals("UTC")) {
zoneOffset = ZoneOffset.UTC;
} else {
zoneOffset = ZoneOffset.of(defaultZoneIdString);
}
return TimestampParserJava.of(pattern.substring(5),
zoneOffset);
} else if (pattern.startsWith("ruby:")) {
final ZoneOffset zoneOffset;
if (defaultZoneIdString.equals("UTC")) {
zoneOffset = ZoneOffset.UTC;
} else {
zoneOffset = ZoneOffset.of(defaultZoneIdString);
}
return TimestampParserRuby.of(pattern.substring(5),
zoneOffset);
} else {
return TimestampParserLegacy.of(pattern,
TimeZoneIds.parseZoneIdWithJodaAndRubyZoneTab(defaultZoneIdString),
TimeZoneIds.parseJodaDateTimeZone(defaultZoneIdString),
1970,
1,
1);
}
}
public static TimestampParser of(final Task task,
final TimestampColumnOption columnOption) {
final String pattern = columnOption.getFormat().or(task.getDefaultTimestampFormat());
if (pattern.startsWith("java:")) {
// TODO: Warn if "default_date" is set, which is unavailable for "java:".
final String zoneOffsetString = columnOption.getTimeZoneId().or(task.getDefaultTimeZoneId());
final ZoneOffset zoneOffset;
if (zoneOffsetString.equals("UTC")) {
zoneOffset = ZoneOffset.UTC;
} else {
zoneOffset = ZoneOffset.of(zoneOffsetString);
}
return TimestampParserJava.of(pattern.substring(5),
zoneOffset);
} else if (pattern.startsWith("ruby:")) {
// TODO: Warn if "default_date" is set, which is unavailable for "ruby:".
final String zoneOffsetString = columnOption.getTimeZoneId().or(task.getDefaultTimeZoneId());
final ZoneOffset zoneOffset;
if (zoneOffsetString.equals("UTC")) {
zoneOffset = ZoneOffset.UTC;
} else {
zoneOffset = ZoneOffset.of(zoneOffsetString);
}
return TimestampParserRuby.of(pattern.substring(5),
zoneOffset);
} else {
return TimestampParserLegacy.of(pattern,
TimeZoneIds.parseZoneIdWithJodaAndRubyZoneTab(
columnOption.getTimeZoneId().or(task.getDefaultTimeZoneId())),
TimeZoneIds.parseJodaDateTimeZone(
columnOption.getTimeZoneId().or(task.getDefaultTimeZoneId())),
columnOption.getDate().or(task.getDefaultDate()));
}
}
public interface Task {
@Config("default_timezone")
@ConfigDefault("\"UTC\"")
public String getDefaultTimeZoneId();
// Using Joda-Time is deprecated, but the getter returns org.joda.time.DateTimeZone for plugin compatibility.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public default org.joda.time.DateTimeZone getDefaultTimeZone() {
if (getDefaultTimeZoneId() != null) {
return TimeZoneIds.parseJodaDateTimeZone(getDefaultTimeZoneId());
} else {
return null;
}
}
@Config("default_timestamp_format")
@ConfigDefault("\"%Y-%m-%d %H:%M:%S.%N %z\"")
public String getDefaultTimestampFormat();
@Config("default_date")
@ConfigDefault("\"1970-01-01\"")
public String getDefaultDate();
}
public interface TimestampColumnOption {
@Config("timezone")
@ConfigDefault("null")
public Optional<String> getTimeZoneId();
// Using Joda-Time is deprecated, but the getter returns org.joda.time.DateTimeZone for plugin compatibility.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public default Optional<org.joda.time.DateTimeZone> getTimeZone() {
if (getTimeZoneId().isPresent()) {
return Optional.of(TimeZoneIds.parseJodaDateTimeZone(getTimeZoneId().get()));
} else {
return Optional.absent();
}
}
@Config("format")
@ConfigDefault("null")
public Optional<String> getFormat();
@Config("date")
@ConfigDefault("null")
public Optional<String> getDate();
}
// Using Joda-Time is deprecated, but the method return org.joda.time.DateTimeZone for plugin compatibility.
// It won't be removed very soon at least until Embulk v0.10.
@Deprecated
public org.joda.time.DateTimeZone getDefaultTimeZone() {
if (this.delegate == null) {
throw new RuntimeException("FATAL: Unexpected execution path of TimestampParser without delegate.");
}
return this.delegate.getDefaultTimeZone();
}
Instant parseInternal(final String text) throws TimestampParseException {
if (this.delegate == null) {
throw new TimestampParseException("FATAL: Unexpected execution path of TimestampParser without delegate.");
}
return this.delegate.parseInternal(text);
}
public final Timestamp parse(final String text) throws TimestampParseException {
try {
return Timestamp.ofInstant(this.parseInternal(text));
} catch (final DateTimeException ex) {
throw new TimestampParseException(ex);
}
}
private final TimestampParserLegacy delegate;
}