Skip to content

Commit

Permalink
DateTimeType: new methods toZone and toLocaleZone (#945)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and kaikreuzer committed Aug 2, 2019
1 parent c3af31e commit f03f328
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*/
package org.eclipse.smarthome.core.library.types;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.zone.ZoneRulesException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
Expand All @@ -34,6 +36,7 @@
* @author Erdoan Hadzhiyusein - Refactored to use ZonedDateTime
* @author Jan N. Klug - add ability to use time or date only
* @author Wouter Born - increase parsing and formatting precision
* @author Laurent Garnier - added methods toLocaleZone and toZone
*/
@NonNullByDefault
public class DateTimeType implements PrimitiveType, State, Command {
Expand Down Expand Up @@ -108,7 +111,7 @@ public DateTimeType(String zonedValue) {
throw new IllegalArgumentException(zonedValue + " is not in a valid format.", invalidFormatException);
}

zonedDateTime = date;
zonedDateTime = date.withFixedOffsetZone();
}

/**
Expand Down Expand Up @@ -140,6 +143,46 @@ public String format(Locale locale, String pattern) {
return String.format(locale, pattern, zonedDateTime);
}

/**
* Create a {@link DateTimeType} being the translation of the current object to the locale time zone
*
* @return a {@link DateTimeType} translated to the locale time zone
*
* @throws DateTimeException if the converted zone ID has an invalid format or the result exceeds the supported date
* range
* @throws ZoneRulesException if the converted zone region ID cannot be found
*/
public DateTimeType toLocaleZone() throws DateTimeException, ZoneRulesException {
return toZone(ZoneId.systemDefault());
}

/**
* Create a {@link DateTimeType} being the translation of the current object to a given zone
*
* @param zone the target zone as a string
*
* @return a {@link DateTimeType} translated to the given zone
*
* @throws DateTimeException if the zone has an invalid format or the result exceeds the supported date range
* @throws ZoneRulesException if the zone is a region ID that cannot be found
*/
public DateTimeType toZone(String zone) throws DateTimeException, ZoneRulesException {
return toZone(ZoneId.of(zone));
}

/**
* Create a {@link DateTimeType} being the translation of the current object to a given zone
*
* @param zoneId the target {@link ZoneId}
*
* @return a {@link DateTimeType} translated to the given zone
*
* @throws DateTimeException if the result exceeds the supported date range
*/
public DateTimeType toZone(ZoneId zoneId) throws DateTimeException {
return new DateTimeType(zonedDateTime.withZoneSameInstant(zoneId));
}

@Override
public String toString() {
return toFullString();
Expand Down
Loading

0 comments on commit f03f328

Please sign in to comment.