blob: 955892565e5313bbc26bf91a82cbeae6b49ef6cb [file] [log] [blame]
Simon Hunt3d1b0652015-05-05 17:27:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt3d1b0652015-05-05 17:27:24 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Simon Hunt3d1b0652015-05-05 17:27:24 -070015 */
16
17package org.onosproject.ui.table.cell;
18
Simon Hunt5939e652015-05-06 16:20:23 -070019
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070020import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM;
21import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
22import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
23
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070024import java.time.DateTimeException;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070025import java.time.Instant;
26import java.time.ZoneId;
27import java.time.format.DateTimeFormatter;
28import java.time.format.DateTimeFormatterBuilder;
29import java.time.temporal.ChronoField;
30import java.time.temporal.TemporalAccessor;
Simon Hunt5939e652015-05-06 16:20:23 -070031import java.util.Locale;
Simon Hunt3d1b0652015-05-05 17:27:24 -070032
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070035
Simon Hunt3d1b0652015-05-05 17:27:24 -070036/**
37 * Formats time values using {@link DateTimeFormatter}.
38 */
Simon Hunt66f75432015-05-06 14:07:03 -070039public final class TimeFormatter extends AbstractCellFormatter {
Simon Hunt3d1b0652015-05-05 17:27:24 -070040
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070041 private static final Logger log = LoggerFactory.getLogger(TimeFormatter.class);
42
Simon Hunt5939e652015-05-06 16:20:23 -070043 private DateTimeFormatter dtf;
Simon Hunt3d1b0652015-05-05 17:27:24 -070044
Simon Hunt5939e652015-05-06 16:20:23 -070045 // NOTE: Unlike other formatters in this package, this one is not
46 // implemented as a Singleton, because instances may be
47 // decorated with alternate locale and/or timezone.
Simon Hunt66f75432015-05-06 14:07:03 -070048
Simon Hunt5939e652015-05-06 16:20:23 -070049 /**
50 * Constructs a time formatter that uses the default locale and timezone.
51 */
52 public TimeFormatter() {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070053 dtf = new DateTimeFormatterBuilder()
54 .appendValue(CLOCK_HOUR_OF_AMPM)
55 .appendLiteral(':')
56 .appendValue(MINUTE_OF_HOUR, 2)
57 .optionalStart()
58 .appendLiteral(':')
59 .appendValue(SECOND_OF_MINUTE, 2)
60 .appendLiteral(' ')
61 .appendText(ChronoField.AMPM_OF_DAY)
62 .optionalStart()
63 .appendLiteral(' ')
64 .appendOffset("+HH:MM", "+00:00")
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070065 .toFormatter()
66 .withLocale(Locale.getDefault())
67 .withZone(ZoneId.systemDefault());
Simon Hunt3d1b0652015-05-05 17:27:24 -070068 }
69
70 /**
Simon Hunt5939e652015-05-06 16:20:23 -070071 * Sets the locale to use for formatting the time.
72 *
73 * @param locale locale to use for formatting
74 * @return self, for chaining
Simon Hunt3d1b0652015-05-05 17:27:24 -070075 */
Simon Hunt5939e652015-05-06 16:20:23 -070076 public TimeFormatter withLocale(Locale locale) {
77 dtf = dtf.withLocale(locale);
78 return this;
79 }
80
81 /**
82 * Sets the time zone to use for formatting the time.
83 *
84 * @param zone time zone to use
85 * @return self, for chaining
86 */
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070087 public TimeFormatter withZone(ZoneId zone) {
Simon Hunt5939e652015-05-06 16:20:23 -070088 dtf = dtf.withZone(zone);
89 return this;
90 }
91
92 @Override
93 protected String nonNullFormat(Object value) {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070094 if (value instanceof TemporalAccessor) {
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070095 try {
96 return dtf.format((TemporalAccessor) value);
97 } catch (DateTimeException e) {
98 log.error("Failed formatting {} [{}]", value, value.getClass().getSimpleName(), e);
99 log.warn("dtf zone was {}", dtf.getZone());
100 throw e;
101 }
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700102 } else if (value instanceof org.joda.time.DateTime) {
103 return dtf.format(Instant.ofEpochMilli(((org.joda.time.DateTime) value).getMillis()));
104 }
105 // should never reach here
106 return String.valueOf(value);
Simon Hunt5939e652015-05-06 16:20:23 -0700107 }
108
Simon Hunt3d1b0652015-05-05 17:27:24 -0700109}