This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 784
Refactoring DateTimeType and impacts on ESH #4259
Merged
sjsf
merged 4 commits into
eclipse-archived:master
from
MusalaSoft:refactoring-T69-DateTimeType
Nov 28, 2017
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c4c8d4
Migarated DateTimeType to Java 8 and handled impacts on the ESH repo
doandzhi e3791ca
Integrated the time zone delivery service in the Astro Binding
doandzhi 48b19c8
Adressed the comments of htreu.
doandzhi 089ceef
Corrected NTP tests
doandzhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,15 @@ | |
*/ | ||
package org.eclipse.smarthome.core.library.types; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.GregorianCalendar; | ||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
|
||
import org.eclipse.smarthome.core.types.Command; | ||
import org.eclipse.smarthome.core.types.PrimitiveType; | ||
|
@@ -20,53 +24,88 @@ | |
/** | ||
* | ||
* @author Kai Kreuzer - Initial contribution | ||
* @author Erdoan Hadzhiyusein - Refactored to use ZonedDateTime | ||
*/ | ||
public class DateTimeType implements PrimitiveType, State, Command { | ||
|
||
public static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss"; | ||
public static final String DATE_PATTERN_WITH_TZ = "yyyy-MM-dd'T'HH:mm:ssz"; | ||
|
||
// this pattern returns the time zone in RFC822 format | ||
public static final String DATE_PATTERN_WITH_TZ_AND_MS = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; | ||
|
||
public static final String DATE_PATTERN_WITH_TZ_AND_MS_GENERAL = "yyyy-MM-dd'T'HH:mm:ss.SSSz"; | ||
public static final String DATE_PATTERN_WITH_TZ_AND_MS_ISO = "yyyy-MM-dd'T'HH:mm:ss.SSSX"; | ||
|
||
private Calendar calendar; | ||
private ZonedDateTime zonedDateTime; | ||
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN); | ||
private final DateTimeFormatter formatterTz = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ); | ||
private final DateTimeFormatter formatterTzMs = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ_AND_MS_GENERAL); | ||
private final DateTimeFormatter formatterTzMsRFC = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ_AND_MS); | ||
private final DateTimeFormatter formatterTzMsIso = DateTimeFormatter.ofPattern(DATE_PATTERN_WITH_TZ_AND_MS_ISO); | ||
|
||
/** | ||
* @deprecated The constructor uses Calendar object hence it doesn't store time zone. A new constructor is | ||
* available. Use {@link #DateTimeType(ZonedDateTime)} instead. | ||
* | ||
* @param calendar - The Calendar object containing the time stamp. | ||
*/ | ||
@Deprecated | ||
public DateTimeType(Calendar calendar) { | ||
this.zonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), TimeZone.getDefault().toZoneId()) | ||
.withFixedOffsetZone(); | ||
} | ||
|
||
public DateTimeType() { | ||
this(Calendar.getInstance()); | ||
this(ZonedDateTime.now()); | ||
} | ||
|
||
public DateTimeType(Calendar calendar) { | ||
this.calendar = (Calendar) calendar.clone(); | ||
public DateTimeType(ZonedDateTime zoned) { | ||
this.zonedDateTime = ZonedDateTime.from(zoned).withFixedOffsetZone(); | ||
} | ||
|
||
public DateTimeType(String calendarValue) { | ||
Date date = null; | ||
public DateTimeType(String zonedValue) { | ||
ZonedDateTime date = null; | ||
|
||
try { | ||
try { | ||
date = new SimpleDateFormat(DATE_PATTERN_WITH_TZ_AND_MS).parse(calendarValue); | ||
} catch (ParseException fpe3) { | ||
date = ZonedDateTime.parse(zonedValue, formatterTzMsRFC); | ||
} catch (DateTimeParseException tzMsRfcException) { | ||
try { | ||
date = new SimpleDateFormat(DATE_PATTERN_WITH_TZ_AND_MS_ISO).parse(calendarValue); | ||
} catch (ParseException fpe4) { | ||
date = ZonedDateTime.parse(zonedValue, formatterTzMsIso); | ||
} catch (DateTimeParseException tzMsException) { | ||
try { | ||
date = new SimpleDateFormat(DATE_PATTERN_WITH_TZ).parse(calendarValue); | ||
} catch (ParseException fpe2) { | ||
date = new SimpleDateFormat(DATE_PATTERN).parse(calendarValue); | ||
date = ZonedDateTime.parse(zonedValue, formatterTz); | ||
} catch (DateTimeParseException tzException) { | ||
try { | ||
date = ZonedDateTime.parse(zonedValue, formatterTzMs); | ||
} catch (DateTimeParseException regularFormatException) { | ||
// A ZonedDateTime object cannot be creating by parsing directly a pattern without zone | ||
LocalDateTime localDateTime = LocalDateTime.parse(zonedValue, formatter); | ||
date = ZonedDateTime.of(localDateTime, ZoneId.systemDefault()); | ||
} | ||
} | ||
} | ||
} | ||
} catch (ParseException fpe) { | ||
throw new IllegalArgumentException(calendarValue + " is not in a valid format.", fpe); | ||
} catch (DateTimeParseException invalidFormatException) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
throw new IllegalArgumentException(zonedValue + " is not in a valid format.", invalidFormatException); | ||
} | ||
|
||
if (date != null) { | ||
calendar = Calendar.getInstance(); | ||
calendar.setTime(date); | ||
zonedDateTime = date; | ||
} | ||
} | ||
|
||
/** | ||
* @deprecated The method is deprecated. You can use {@link #getZonedDateTime()} instead. | ||
*/ | ||
@Deprecated | ||
public Calendar getCalendar() { | ||
return (Calendar) calendar.clone(); | ||
return GregorianCalendar.from(zonedDateTime); | ||
} | ||
|
||
public ZonedDateTime getZonedDateTime() { | ||
return zonedDateTime; | ||
} | ||
|
||
public static DateTimeType valueOf(String value) { | ||
|
@@ -76,14 +115,14 @@ public static DateTimeType valueOf(String value) { | |
@Override | ||
public String format(String pattern) { | ||
try { | ||
return String.format(pattern, calendar); | ||
return String.format(pattern, zonedDateTime); | ||
} catch (NullPointerException npe) { | ||
return new SimpleDateFormat(DATE_PATTERN).format(calendar.getTime()); | ||
return DateTimeFormatter.ofPattern(DATE_PATTERN).format(zonedDateTime); | ||
} | ||
} | ||
|
||
public String format(Locale locale, String pattern) { | ||
return String.format(locale, pattern, calendar); | ||
return String.format(locale, pattern, zonedDateTime); | ||
} | ||
|
||
@Override | ||
|
@@ -93,14 +132,14 @@ public String toString() { | |
|
||
@Override | ||
public String toFullString() { | ||
return new SimpleDateFormat(DATE_PATTERN_WITH_TZ_AND_MS).format(calendar.getTime()); | ||
return zonedDateTime.format(formatterTzMsRFC); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((calendar == null) ? 0 : calendar.hashCode()); | ||
result = prime * result + ((getZonedDateTime() == null) ? 0 : getZonedDateTime().hashCode()); | ||
return result; | ||
} | ||
|
||
|
@@ -116,14 +155,13 @@ public boolean equals(Object obj) { | |
return false; | ||
} | ||
DateTimeType other = (DateTimeType) obj; | ||
if (calendar == null) { | ||
if (other.calendar != null) { | ||
if (zonedDateTime == null) { | ||
if (other.zonedDateTime != null) { | ||
return false; | ||
} | ||
} else if (calendar.compareTo(other.calendar) != 0) { | ||
} else if (zonedDateTime.compareTo(other.zonedDateTime) != 0) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have added a pattern for numeric time zone notation (
DATE_PATTERN_WITH_TZ_AND_MS_GENERAL
) - therefore wouldn't it make sense to add a parameter set here which uses this notation as an input?