2 * Copyright 2017 Gregory Moyer
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openhab.binding.sleepiq.api.model;
18 import java.time.Duration;
19 import java.util.Objects;
20 import java.util.concurrent.TimeUnit;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 public class TimeSince {
25 private static final Pattern PATTERN = Pattern.compile("(([0-9]+) d )?([0-9]{2}):([0-9]{2}):([0-9]{2})",
26 Pattern.CASE_INSENSITIVE);
28 private Duration duration;
30 public Duration getDuration() {
34 public void setDuration(Duration duration) {
35 this.duration = duration == null ? null : duration.abs();
38 public TimeSince withDuration(long days, long hours, long minutes, long seconds) {
39 return withDuration(Duration.ofSeconds(TimeUnit.DAYS.toSeconds(days) + TimeUnit.HOURS.toSeconds(hours)
40 + TimeUnit.MINUTES.toSeconds(minutes) + seconds));
43 public TimeSince withDuration(Duration duration) {
44 setDuration(duration);
49 public int hashCode() {
52 result = prime * result + ((duration == null) ? 0 : duration.hashCode());
57 public boolean equals(Object obj) {
64 if (!(obj instanceof TimeSince)) {
67 TimeSince other = (TimeSince) obj;
68 if (duration == null) {
69 if (other.duration != null) {
72 } else if (!duration.equals(other.duration)) {
79 public String toString() {
80 long totalDays = duration.toDays();
81 long totalHours = duration.toHours();
82 long totalMinutes = duration.toMinutes();
83 long totalSeconds = duration.getSeconds();
85 long hours = totalHours - TimeUnit.DAYS.toHours(totalDays);
86 long minutes = totalMinutes - TimeUnit.HOURS.toMinutes(totalHours);
87 long seconds = totalSeconds - TimeUnit.MINUTES.toSeconds(totalMinutes);
90 return String.format("%d d %02d:%02d:%02d", totalDays, hours, minutes, seconds);
93 return String.format("%02d:%02d:%02d", hours, minutes, seconds);
96 public static TimeSince parse(CharSequence text) {
97 Objects.requireNonNull(text, "text");
99 Matcher matcher = PATTERN.matcher(text);
100 if (!matcher.matches()) {
101 return new TimeSince().withDuration(Duration.ZERO);
104 String dayMatch = matcher.group(2);
105 String hourMatch = matcher.group(3);
106 String minuteMatch = matcher.group(4);
107 String secondMatch = matcher.group(5);
109 StringBuilder sb = new StringBuilder("P");
110 if (dayMatch != null) {
111 sb.append(dayMatch).append('D');
113 sb.append('T').append(hourMatch).append('H').append(minuteMatch).append('M').append(secondMatch).append('S');
115 return new TimeSince().withDuration(Duration.parse(sb.toString()));