blob: f8b9757d4fa4943c677ae27b21ce41dc4c2904b8 [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 HIGUCHI0c47d532017-08-18 23:16:35 -070033import org.joda.time.DateTimeZone;
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070036
Simon Hunt3d1b0652015-05-05 17:27:24 -070037/**
38 * Formats time values using {@link DateTimeFormatter}.
39 */
Simon Hunt66f75432015-05-06 14:07:03 -070040public final class TimeFormatter extends AbstractCellFormatter {
Simon Hunt3d1b0652015-05-05 17:27:24 -070041
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070042 private static final Logger log = LoggerFactory.getLogger(TimeFormatter.class);
43
Simon Hunt5939e652015-05-06 16:20:23 -070044 private DateTimeFormatter dtf;
Simon Hunt3d1b0652015-05-05 17:27:24 -070045
Simon Hunt5939e652015-05-06 16:20:23 -070046 // NOTE: Unlike other formatters in this package, this one is not
47 // implemented as a Singleton, because instances may be
48 // decorated with alternate locale and/or timezone.
Simon Hunt66f75432015-05-06 14:07:03 -070049
Simon Hunt5939e652015-05-06 16:20:23 -070050 /**
51 * Constructs a time formatter that uses the default locale and timezone.
52 */
53 public TimeFormatter() {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070054 dtf = new DateTimeFormatterBuilder()
55 .appendValue(CLOCK_HOUR_OF_AMPM)
56 .appendLiteral(':')
57 .appendValue(MINUTE_OF_HOUR, 2)
58 .optionalStart()
59 .appendLiteral(':')
60 .appendValue(SECOND_OF_MINUTE, 2)
61 .appendLiteral(' ')
62 .appendText(ChronoField.AMPM_OF_DAY)
63 .optionalStart()
64 .appendLiteral(' ')
65 .appendOffset("+HH:MM", "+00:00")
Yuta HIGUCHI905cc822017-10-20 16:55:21 -070066 .toFormatter()
67 .withLocale(Locale.getDefault())
68 .withZone(ZoneId.systemDefault());
Simon Hunt3d1b0652015-05-05 17:27:24 -070069 }
70
71 /**
Simon Hunt5939e652015-05-06 16:20:23 -070072 * Sets the locale to use for formatting the time.
73 *
74 * @param locale locale to use for formatting
75 * @return self, for chaining
Simon Hunt3d1b0652015-05-05 17:27:24 -070076 */
Simon Hunt5939e652015-05-06 16:20:23 -070077 public TimeFormatter withLocale(Locale locale) {
78 dtf = dtf.withLocale(locale);
79 return this;
80 }
81
82 /**
83 * Sets the time zone to use for formatting the time.
84 *
85 * @param zone time zone to use
86 * @return self, for chaining
87 */
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070088 public TimeFormatter withZone(ZoneId zone) {
Simon Hunt5939e652015-05-06 16:20:23 -070089 dtf = dtf.withZone(zone);
90 return this;
91 }
92
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070093 /**
94 * Sets the time zone to use for formatting the time.
95 *
96 * @param zone time zone to use
97 * @return self, for chaining
98 *
99 * @deprecated in 1.12.0
100 */
101 @Deprecated
102 public TimeFormatter withZone(DateTimeZone zone) {
103 return withZone(zone.toTimeZone().toZoneId());
104 }
105
Simon Hunt5939e652015-05-06 16:20:23 -0700106 @Override
107 protected String nonNullFormat(Object value) {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700108 if (value instanceof TemporalAccessor) {
Yuta HIGUCHI905cc822017-10-20 16:55:21 -0700109 try {
110 return dtf.format((TemporalAccessor) value);
111 } catch (DateTimeException e) {
112 log.error("Failed formatting {} [{}]", value, value.getClass().getSimpleName(), e);
113 log.warn("dtf zone was {}", dtf.getZone());
114 throw e;
115 }
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700116 } else if (value instanceof org.joda.time.DateTime) {
117 return dtf.format(Instant.ofEpochMilli(((org.joda.time.DateTime) value).getMillis()));
118 }
119 // should never reach here
120 return String.valueOf(value);
Simon Hunt5939e652015-05-06 16:20:23 -0700121 }
122
Simon Hunt3d1b0652015-05-05 17:27:24 -0700123}