-
Notifications
You must be signed in to change notification settings - Fork 199
/
TimestampFormatConfig.java
99 lines (88 loc) · 3.21 KB
/
TimestampFormatConfig.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
package org.quickload.time;
import java.util.Set;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import com.google.common.collect.ImmutableSet;
import com.fasterxml.jackson.annotation.JsonValue;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.jruby.embed.ScriptingContainer;
public class TimestampFormatConfig
{
private final ScriptingContainer jruby;
private final String format;
public TimestampFormatConfig(ScriptingContainer jruby, String format)
{
this.jruby = jruby;
this.format = format;
}
@JsonValue
public String getFormat()
{
return format;
}
public TimestampFormatter newFormatter(TimestampFormatterTask task)
{
return new TimestampFormatter(jruby, format, task);
}
public TimestampParser newParser(TimestampParserTask task)
{
return new TimestampParser(jruby, format, task);
}
private static Set<String> availableTimeZoneNames = ImmutableSet.copyOf(DateTimeZone.getAvailableIDs());
static DateTimeZone parseDateTimeZone(String s)
{
if(s.startsWith("+") || s.startsWith("-")) {
return DateTimeZone.forID("GMT"+s);
} else {
try {
int rawOffset = (int) DateTimeFormat.forPattern("z").parseMillis(s);
if(rawOffset == 0) {
return DateTimeZone.UTC;
}
int offset = rawOffset / -1000;
int h = offset / 3600;
int m = offset % 3600;
return DateTimeZone.forOffsetHoursMinutes(h, m);
} catch (IllegalArgumentException ex) {
// parseMillis failed
}
// TimeZone.getTimeZone returns GMT zone if given timezone id is not found
// we want to only return timezone if exact match, otherwise exception
if (availableTimeZoneNames.contains(s)) {
//return TimeZone.getTimeZone(s);
return DateTimeZone.forID(s);
}
return null;
}
}
//// Java standard TimeZone
//static TimeZone parseDateTimeZone(String s)
//{
// if(s.startsWith("+") || s.startsWith("-")) {
// return TimeZone.getTimeZone("GMT"+s);
//
// } else {
// ParsePosition pp = new ParsePosition(0);
// Date off = new SimpleDateFormat("z").parse(s, pp);
// if(off != null && pp.getErrorIndex() == -1) {
// int rawOffset = (int) off.getTime();
// if(rawOffset == 0) {
// return TimeZone.UTC;
// }
// int offset = rawOffset / -1000;
// int h = offset / 3600;
// int m = offset % 3600;
// return DateTimeZone.getTimeZone(String.format("GMT%+02d%02d", h, m));
// }
//
// // TimeZone.getTimeZone returns GMT zone if given timezone id is not found
// // we want to only return timezone if exact match, otherwise exception
// if (availableTimeZoneNames.contains(s)) {
// return TimeZone.getTimeZone(s);
// }
// return null;
// }
//}
}