2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.astro.internal.model;
15 import java.time.Duration;
16 import java.time.temporal.ChronoUnit;
17 import java.util.Calendar;
19 import javax.measure.quantity.Time;
21 import org.openhab.binding.astro.internal.util.DateTimeUtils;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.Units;
26 * Holds the season dates of the year and the current name.
28 * @author Gerhard Riegler - Initial contribution
31 private Calendar spring;
32 private Calendar summer;
33 private Calendar autumn;
34 private Calendar winter;
36 private SeasonName name;
39 * Returns the date of the beginning of spring.
41 public Calendar getSpring() {
46 * Sets the date of the beginning of spring.
48 public void setSpring(Calendar spring) {
53 * Returns the date of the beginning of summer.
55 public Calendar getSummer() {
60 * Sets the date of the beginning of summer.
62 public void setSummer(Calendar summer) {
67 * Returns the date of the beginning of autumn.
69 public Calendar getAutumn() {
74 * Sets the date of the beginning of autumn.
76 public void setAutumn(Calendar autumn) {
81 * Returns the date of the beginning of winter.
83 public Calendar getWinter() {
88 * Returns the date of the beginning of winter.
90 public void setWinter(Calendar winter) {
95 * Returns the current season name.
97 public SeasonName getName() {
102 * Sets the current season name.
104 public void setName(SeasonName name) {
109 * Returns the next season.
111 public Calendar getNextSeason() {
112 return DateTimeUtils.getNextFromToday(spring, summer, autumn, winter);
116 * Returns the next season name.
118 public SeasonName getNextName() {
119 int ordinal = name.ordinal() + 1;
123 return SeasonName.values()[ordinal];
127 * Returns the time left for current season
129 public QuantityType<Time> getTimeLeft() {
130 final Calendar now = Calendar.getInstance();
131 final Calendar next = getNextSeason();
132 final Duration timeLeft = Duration.of(next.getTimeInMillis() - now.getTimeInMillis(), ChronoUnit.MILLIS);
134 return new QuantityType<>(timeLeft.toDays(), Units.DAY);