-
Notifications
You must be signed in to change notification settings - Fork 10
/
RfcTimestampHandler.java
54 lines (43 loc) · 1.75 KB
/
RfcTimestampHandler.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
/*
Copyright 2014 Yahoo! Inc.
Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
package com.yahoo.xpathproto.handler;
import org.apache.commons.jxpath.JXPathContext;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import com.yahoo.xpathproto.ObjectToFieldHandler;
import com.yahoo.xpathproto.dataobject.Config;
import com.yahoo.xpathproto.dataobject.Config.Entry;
import com.yahoo.xpathproto.dataobject.Context;
public class RfcTimestampHandler implements ObjectToFieldHandler {
private static final Logger logger = LoggerFactory.getLogger(RfcTimestampHandler.class);
private static final DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
@Override
public Object getProtoValue(final JXPathContext context, final Context vars, final Config.Entry entry) {
final Object object = context.getValue(entry.getPath());
if (null == object) {
return null;
}
return parseDate(object.toString());
}
@Override
public List<Object> getRepeatedProtoValue(final JXPathContext context, final Context vars, final Entry entry) {
throw new UnsupportedOperationException("Invalid operation");
}
static Object parseDate(final String dateStr) {
DateTime date;
try {
date = formatter.parseDateTime(dateStr).toDateTime(DateTimeZone.UTC);
} catch (final IllegalArgumentException e) {
logger.error("Failed to parse date: " + dateStr, e);
return null;
}
return date.getMillis() / 1000;
}
}