embulk 0.8.26-java → 0.8.27-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,111 @@
1
+ package org.embulk.spi.time;
2
+
3
+ import static org.embulk.spi.time.StrptimeFormat.*;
4
+
5
+ /**
6
+ * This class is ported from org.jruby.util.RubyDateFormatter.Token in JRuby
7
+ * 9.1.5.0 and modified for StrptimeParser under EPL.
8
+ * @see <a href="https://github.com/jruby/jruby/blob/036ce39f0476d4bd718e23e64caff36bb50b8dbc/core/src/main/java/org/jruby/util/RubyDateFormatter.java">RubyDateFormatter.java</a>
9
+ *
10
+ * TODO
11
+ * This class is tentatively required for {@code StrptimeParser} class.
12
+ * The {@code StrptimeParser} and {@code RubyDateParser} will be merged into JRuby
13
+ * (jruby/jruby#4591). embulk-jruby-strptime is removed when Embulk start using
14
+ * the JRuby that bundles embulk-jruby-strptime.
15
+ */
16
+ public class StrptimeToken
17
+ {
18
+ static final StrptimeToken[] CONVERSION2TOKEN = new StrptimeToken[256];
19
+
20
+ static {
21
+ CONVERSION2TOKEN['A'] = new StrptimeToken(FORMAT_WEEK_LONG);
22
+ CONVERSION2TOKEN['a'] = new StrptimeToken(FORMAT_WEEK_SHORT);
23
+ CONVERSION2TOKEN['B'] = new StrptimeToken(FORMAT_MONTH_LONG);
24
+ CONVERSION2TOKEN['b'] = new StrptimeToken(FORMAT_MONTH_SHORT);
25
+ CONVERSION2TOKEN['h'] = CONVERSION2TOKEN['b'];
26
+ CONVERSION2TOKEN['C'] = new StrptimeToken(FORMAT_CENTURY);
27
+ CONVERSION2TOKEN['d'] = new StrptimeToken(FORMAT_DAY);
28
+ CONVERSION2TOKEN['e'] = new StrptimeToken(FORMAT_DAY_S);
29
+ CONVERSION2TOKEN['G'] = new StrptimeToken(FORMAT_WEEKYEAR);
30
+ CONVERSION2TOKEN['g'] = new StrptimeToken(FORMAT_WEEKYEAR_SHORT);
31
+ CONVERSION2TOKEN['H'] = new StrptimeToken(FORMAT_HOUR);
32
+ CONVERSION2TOKEN['I'] = new StrptimeToken(FORMAT_HOUR_M);
33
+ CONVERSION2TOKEN['j'] = new StrptimeToken(FORMAT_DAY_YEAR);
34
+ CONVERSION2TOKEN['k'] = new StrptimeToken(FORMAT_HOUR_BLANK);
35
+ CONVERSION2TOKEN['L'] = new StrptimeToken(FORMAT_MILLISEC);
36
+ CONVERSION2TOKEN['l'] = new StrptimeToken(FORMAT_HOUR_S);
37
+ CONVERSION2TOKEN['M'] = new StrptimeToken(FORMAT_MINUTES);
38
+ CONVERSION2TOKEN['m'] = new StrptimeToken(FORMAT_MONTH);
39
+ CONVERSION2TOKEN['N'] = new StrptimeToken(FORMAT_NANOSEC);
40
+ CONVERSION2TOKEN['P'] = new StrptimeToken(FORMAT_MERIDIAN_LOWER_CASE);
41
+ CONVERSION2TOKEN['p'] = new StrptimeToken(FORMAT_MERIDIAN);
42
+ CONVERSION2TOKEN['Q'] = new StrptimeToken(FORMAT_MICROSEC_EPOCH);
43
+ CONVERSION2TOKEN['S'] = new StrptimeToken(FORMAT_SECONDS);
44
+ CONVERSION2TOKEN['s'] = new StrptimeToken(FORMAT_EPOCH);
45
+ CONVERSION2TOKEN['U'] = new StrptimeToken(FORMAT_WEEK_YEAR_S);
46
+ CONVERSION2TOKEN['u'] = new StrptimeToken(FORMAT_DAY_WEEK2);
47
+ CONVERSION2TOKEN['V'] = new StrptimeToken(FORMAT_WEEK_WEEKYEAR);
48
+ CONVERSION2TOKEN['W'] = new StrptimeToken(FORMAT_WEEK_YEAR_M);
49
+ CONVERSION2TOKEN['w'] = new StrptimeToken(FORMAT_DAY_WEEK);
50
+ CONVERSION2TOKEN['Y'] = new StrptimeToken(FORMAT_YEAR_LONG);
51
+ CONVERSION2TOKEN['y'] = new StrptimeToken(FORMAT_YEAR_SHORT);
52
+ }
53
+
54
+ private final StrptimeFormat format;
55
+ private final Object data;
56
+
57
+ StrptimeToken(StrptimeFormat format)
58
+ {
59
+ this(format, null);
60
+ }
61
+
62
+ StrptimeToken(StrptimeFormat formatString, Object data)
63
+ {
64
+ this.format = formatString;
65
+ this.data = data;
66
+ }
67
+
68
+ public static StrptimeToken str(String str)
69
+ {
70
+ return new StrptimeToken(StrptimeFormat.FORMAT_STRING, str);
71
+ }
72
+
73
+ public static StrptimeToken format(char c)
74
+ {
75
+ return CONVERSION2TOKEN[c];
76
+ }
77
+
78
+ public static StrptimeToken zoneOffsetColons(int colons)
79
+ {
80
+ return new StrptimeToken(StrptimeFormat.FORMAT_COLON_ZONE_OFF, colons);
81
+ }
82
+
83
+ public static StrptimeToken special(char c)
84
+ {
85
+ return new StrptimeToken(StrptimeFormat.FORMAT_SPECIAL, c);
86
+ }
87
+
88
+ /**
89
+ * Gets the data.
90
+ * @return Returns a Object
91
+ */
92
+ Object getData()
93
+ {
94
+ return data;
95
+ }
96
+
97
+ /**
98
+ * Gets the format.
99
+ * @return Returns a int
100
+ */
101
+ StrptimeFormat getFormat()
102
+ {
103
+ return format;
104
+ }
105
+
106
+ @Override
107
+ public String toString()
108
+ {
109
+ return "<Token "+format+ " "+data+">";
110
+ }
111
+ }
@@ -0,0 +1,466 @@
1
+ package org.embulk.spi.time;
2
+
3
+ /**
4
+ * This is utility class to convert given timezone into integer based timezone
5
+ * diff. It's ported from ext/date/date_parse.c in MRI 2.3.1 under BSDL.
6
+ * @see <a href="https://github.com/ruby/ruby/blob/394fa89c67722d35bdda89f10c7de5c304a5efb1/ext/date/date_parse.c">date_parse.c</a>.
7
+ *
8
+ * TODO
9
+ * This class is tentatively required for {@code StrptimeParser} class.
10
+ * The {@code StrptimeParser} and {@code RubyDateParser} will be merged into JRuby
11
+ * (jruby/jruby#4591). embulk-jruby-strptime is removed when Embulk start using
12
+ * the JRuby that bundles embulk-jruby-strptime.
13
+ */
14
+ public class TimeZoneConverter
15
+ {
16
+ // Ported zones_source in ext/date/date_parse.c
17
+ private static int getOffsetFromZonesSource(String z)
18
+ {
19
+ switch (z) {
20
+ case "ut":
21
+ return 0 * 3600;
22
+ case "gmt":
23
+ return 0 * 3600;
24
+ case "est":
25
+ return -5 * 3600;
26
+ case "edt":
27
+ return -4 * 3600;
28
+ case "cst":
29
+ return -6 * 3600;
30
+ case "cdt":
31
+ return -5 * 3600;
32
+ case "mst":
33
+ return -7 * 3600;
34
+ case "mdt":
35
+ return -6 * 3600;
36
+ case "pst":
37
+ return -8 * 3600;
38
+ case "pdt":
39
+ return -7 * 3600;
40
+ case "a":
41
+ return 1 * 3600;
42
+ case "b":
43
+ return 2 * 3600;
44
+ case "c":
45
+ return 3 * 3600;
46
+ case "d":
47
+ return 4 * 3600;
48
+ case "e":
49
+ return 5 * 3600;
50
+ case "f":
51
+ return 6 * 3600;
52
+ case "g":
53
+ return 7 * 3600;
54
+ case "h":
55
+ return 8 * 3600;
56
+ case "i":
57
+ return 9 * 3600;
58
+ case "k":
59
+ return 10 * 3600;
60
+ case "l":
61
+ return 11 * 3600;
62
+ case "m":
63
+ return 12 * 3600;
64
+ case "n":
65
+ return -1 * 3600;
66
+ case "o":
67
+ return -2 * 3600;
68
+ case "p":
69
+ return -3 * 3600;
70
+ case "q":
71
+ return -4 * 3600;
72
+ case "r":
73
+ return -5 * 3600;
74
+ case "s":
75
+ return -6 * 3600;
76
+ case "t":
77
+ return -7 * 3600;
78
+ case "u":
79
+ return -8 * 3600;
80
+ case "v":
81
+ return -9 * 3600;
82
+ case "w":
83
+ return -10 * 3600;
84
+ case "x":
85
+ return -11 * 3600;
86
+ case "y":
87
+ return -12 * 3600;
88
+ case "z":
89
+ return 0 * 3600;
90
+ case "utc":
91
+ return 0 * 3600;
92
+ case "wet":
93
+ return 0 * 3600;
94
+ case "at":
95
+ return -2 * 3600;
96
+ case "brst":
97
+ return -2 * 3600;
98
+ case "ndt":
99
+ return -(2 * 3600 + 1800);
100
+ case "art":
101
+ return -3 * 3600;
102
+ case "adt":
103
+ return -3 * 3600;
104
+ case "brt":
105
+ return -3 * 3600;
106
+ case "clst":
107
+ return -3 * 3600;
108
+ case "nst":
109
+ return -(3 * 3600 + 1800);
110
+ case "ast":
111
+ return -4 * 3600;
112
+ case "clt":
113
+ return -4 * 3600;
114
+ case "akdt":
115
+ return -8 * 3600;
116
+ case "ydt":
117
+ return -8 * 3600;
118
+ case "akst":
119
+ return -9 * 3600;
120
+ case "hadt":
121
+ return -9 * 3600;
122
+ case "hdt":
123
+ return -9 * 3600;
124
+ case "yst":
125
+ return -9 * 3600;
126
+ case "ahst":
127
+ return -10 * 3600;
128
+ case "cat":
129
+ return -10 * 3600;
130
+ case "hast":
131
+ return -10 * 3600;
132
+ case "hst":
133
+ return -10 * 3600;
134
+ case "nt":
135
+ return -11 * 3600;
136
+ case "idlw":
137
+ return -12 * 3600;
138
+ case "bst":
139
+ return 1 * 3600;
140
+ case "cet":
141
+ return 1 * 3600;
142
+ case "fwt":
143
+ return 1 * 3600;
144
+ case "met":
145
+ return 1 * 3600;
146
+ case "mewt":
147
+ return 1 * 3600;
148
+ case "mez":
149
+ return 1 * 3600;
150
+ case "swt":
151
+ return 1 * 3600;
152
+ case "wat":
153
+ return 1 * 3600;
154
+ case "west":
155
+ return 1 * 3600;
156
+ case "cest":
157
+ return 2 * 3600;
158
+ case "eet":
159
+ return 2 * 3600;
160
+ case "fst":
161
+ return 2 * 3600;
162
+ case "mest":
163
+ return 2 * 3600;
164
+ case "mesz":
165
+ return 2 * 3600;
166
+ case "sast":
167
+ return 2 * 3600;
168
+ case "sst":
169
+ return 2 * 3600;
170
+ case "bt":
171
+ return 3 * 3600;
172
+ case "eat":
173
+ return 3 * 3600;
174
+ case "eest":
175
+ return 3 * 3600;
176
+ case "msk":
177
+ return 3 * 3600;
178
+ case "msd":
179
+ return 4 * 3600;
180
+ case "zp4":
181
+ return 4 * 3600;
182
+ case "zp5":
183
+ return 5 * 3600;
184
+ case "ist":
185
+ return 5 * 3600 + 1800;
186
+ case "zp6":
187
+ return 6 * 3600;
188
+ case "wast":
189
+ return 7 * 3600;
190
+ case "cct":
191
+ return 8 * 3600;
192
+ case "sgt":
193
+ return 8 * 3600;
194
+ case "wadt":
195
+ return 8 * 3600;
196
+ case "jst":
197
+ return 9 * 3600;
198
+ case "kst":
199
+ return 9 * 3600;
200
+ case "east":
201
+ return 10 * 3600;
202
+ case "gst":
203
+ return 10 * 3600;
204
+ case "eadt":
205
+ return 11 * 3600;
206
+ case "idle":
207
+ return 12 * 3600;
208
+ case "nzst":
209
+ return 12 * 3600;
210
+ case "nzt":
211
+ return 12 * 3600;
212
+ case "nzdt":
213
+ return 13 * 3600;
214
+ case "afghanistan":
215
+ return 16200;
216
+ case "alaskan":
217
+ return -32400;
218
+ case "arab":
219
+ return 10800;
220
+ case "arabian":
221
+ return 14400;
222
+ case "arabic":
223
+ return 10800;
224
+ case "atlantic":
225
+ return -14400;
226
+ case "aus central":
227
+ return 34200;
228
+ case "aus eastern":
229
+ return 36000;
230
+ case "azores":
231
+ return -3600;
232
+ case "canada central":
233
+ return -21600;
234
+ case "cape verde":
235
+ return -3600;
236
+ case "caucasus":
237
+ return 14400;
238
+ case "cen. australia":
239
+ return 34200;
240
+ case "central america":
241
+ return -21600;
242
+ case "central asia":
243
+ return 21600;
244
+ case "central europe":
245
+ return 3600;
246
+ case "central european":
247
+ return 3600;
248
+ case "central pacific":
249
+ return 39600;
250
+ case "central":
251
+ return -21600;
252
+ case "china":
253
+ return 28800;
254
+ case "dateline":
255
+ return -43200;
256
+ case "e. africa":
257
+ return 10800;
258
+ case "e. australia":
259
+ return 36000;
260
+ case "e. europe":
261
+ return 7200;
262
+ case "e. south america":
263
+ return -10800;
264
+ case "eastern":
265
+ return -18000;
266
+ case "egypt":
267
+ return 7200;
268
+ case "ekaterinburg":
269
+ return 18000;
270
+ case "fiji":
271
+ return 43200;
272
+ case "fle":
273
+ return 7200;
274
+ case "greenland":
275
+ return -10800;
276
+ case "greenwich":
277
+ return 0;
278
+ case "gtb":
279
+ return 7200;
280
+ case "hawaiian":
281
+ return -36000;
282
+ case "india":
283
+ return 19800;
284
+ case "iran":
285
+ return 12600;
286
+ case "jerusalem":
287
+ return 7200;
288
+ case "korea":
289
+ return 32400;
290
+ case "mexico":
291
+ return -21600;
292
+ case "mid-atlantic":
293
+ return -7200;
294
+ case "mountain":
295
+ return -25200;
296
+ case "myanmar":
297
+ return 23400;
298
+ case "n. central asia":
299
+ return 21600;
300
+ case "nepal":
301
+ return 20700;
302
+ case "new zealand":
303
+ return 43200;
304
+ case "newfoundland":
305
+ return -12600;
306
+ case "north asia east":
307
+ return 28800;
308
+ case "north asia":
309
+ return 25200;
310
+ case "pacific sa":
311
+ return -14400;
312
+ case "pacific":
313
+ return -28800;
314
+ case "romance":
315
+ return 3600;
316
+ case "russian":
317
+ return 10800;
318
+ case "sa eastern":
319
+ return -10800;
320
+ case "sa pacific":
321
+ return -18000;
322
+ case "sa western":
323
+ return -14400;
324
+ case "samoa":
325
+ return -39600;
326
+ case "se asia":
327
+ return 25200;
328
+ case "malay peninsula":
329
+ return 28800;
330
+ case "south africa":
331
+ return 7200;
332
+ case "sri lanka":
333
+ return 21600;
334
+ case "taipei":
335
+ return 28800;
336
+ case "tasmania":
337
+ return 36000;
338
+ case "tokyo":
339
+ return 32400;
340
+ case "tonga":
341
+ return 46800;
342
+ case "us eastern":
343
+ return -18000;
344
+ case "us mountain":
345
+ return -25200;
346
+ case "vladivostok":
347
+ return 36000;
348
+ case "w. australia":
349
+ return 28800;
350
+ case "w. central africa":
351
+ return 3600;
352
+ case "w. europe":
353
+ return 3600;
354
+ case "west asia":
355
+ return 18000;
356
+ case "west pacific":
357
+ return 36000;
358
+ case "yakutsk":
359
+ return 32400;
360
+ default:
361
+ return Integer.MIN_VALUE;
362
+ }
363
+ }
364
+
365
+ // Ported date_zone_to_diff in ext/date/date_parse.c
366
+ public static int dateZoneToDiff(String zone)
367
+ {
368
+ String z = zone.toLowerCase();
369
+
370
+ final boolean dst;
371
+ if (z.endsWith(" daylight time")) {
372
+ z = z.substring(0, z.length() - " daylight time".length());
373
+ dst = true;
374
+ }
375
+ else if (z.endsWith(" standard time")) {
376
+ z = z.substring(0, z.length() - " standard time".length());
377
+ dst = false;
378
+ }
379
+ else if (z.endsWith(" dst")) {
380
+ z = z.substring(0, z.length() - " dst".length());
381
+ dst = true;
382
+ }
383
+ else {
384
+ dst = false;
385
+ }
386
+
387
+ int offsetFromZonesSource;
388
+ if ((offsetFromZonesSource = getOffsetFromZonesSource(z)) != Integer.MIN_VALUE) {
389
+ if (dst) {
390
+ offsetFromZonesSource += 3600;
391
+ }
392
+ return offsetFromZonesSource;
393
+ }
394
+
395
+ if (z.startsWith("gmt") || z.startsWith("utc")) {
396
+ z = z.substring(3, z.length()); // remove "gmt" or "utc"
397
+ }
398
+
399
+ final boolean sign;
400
+ if (z.charAt(0) == '+') {
401
+ sign = true;
402
+ }
403
+ else if (z.charAt(0) == '-') {
404
+ sign = false;
405
+ }
406
+ else {
407
+ // if z doesn't start with "+" or "-", invalid
408
+ return Integer.MIN_VALUE;
409
+ }
410
+ z = z.substring(1);
411
+
412
+ int hour = 0, min = 0, sec = 0;
413
+ if (z.contains(":")) {
414
+ final String[] splited = z.split(":");
415
+ if (splited.length == 2) {
416
+ hour = Integer.parseInt(splited[0]);
417
+ min = Integer.parseInt(splited[1]);
418
+ }
419
+ else {
420
+ hour = Integer.parseInt(splited[0]);
421
+ min = Integer.parseInt(splited[1]);
422
+ sec = Integer.parseInt(splited[2]);
423
+ }
424
+
425
+ }
426
+ else if (z.contains(",") || z.contains(".")) {
427
+ // TODO min = Rational(fr.to_i, 10**fr.size) * 60
428
+ String[] splited = z.split("[\\.,]");
429
+ hour = Integer.parseInt(splited[0]);
430
+ min = (int)(Integer.parseInt(splited[1]) * 60 / Math.pow(10, splited[1].length()));
431
+
432
+ }
433
+ else {
434
+ final int len = z.length();
435
+ if (len % 2 != 0) {
436
+ if (len >= 1) {
437
+ hour = Integer.parseInt(z.substring(0, 1));
438
+ }
439
+ if (len >= 3) {
440
+ min = Integer.parseInt(z.substring(1, 3));
441
+ }
442
+ if (len >= 5) {
443
+ sec = Integer.parseInt(z.substring(3, 5));
444
+ }
445
+ }
446
+ else {
447
+ if (len >= 2) {
448
+ hour = Integer.parseInt(z.substring(0, 2));
449
+ }
450
+ if (len >= 4) {
451
+ min = Integer.parseInt(z.substring(2, 4));
452
+ }
453
+ if (len >= 6) {
454
+ sec = Integer.parseInt(z.substring(4, 6));
455
+ }
456
+ }
457
+ }
458
+
459
+ final int offset = hour * 3600 + min * 60 + sec;
460
+ return sign ? offset : -offset;
461
+ }
462
+
463
+ private TimeZoneConverter()
464
+ {
465
+ }
466
+ }