-
-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Added new methods toZone and toLocaleZone to DateTimeType #945
Conversation
@cweitkamp : here are the additional format methods with tests. This works well and as expected. Now, we have the choice to link the old format methods with the new format methods using ZoneId.systemDefault() as zone. This will impact all calls to the old format methods. Impact not yet evaluated. |
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.
Thank you.
I do not want to reject your new methods to format the value but I am still thinking a toZone(ZoneId/String)
method (similar to the toUnit()
method from QuantityType
) will be more useful.
link the old format methods with the new format methods
No, the framework should not do that (see #832 (comment)). Implementing it in Basic UI will be okay. Did you check how it is done in HABmin?
|
||
DateTimeType dt2 = new DateTimeType(dt1.getZonedDateTime().withZoneSameInstant(ZoneId.of("Europe/Berlin"))); | ||
assertThat(dt2, not(is(dt1))); | ||
assertThat(dt2.toString(), is("2019-05-25T19:53:10.000+0200")); | ||
|
||
assertThat(dt2.format(null), is("2019-05-25T19:53:10")); | ||
assertThat(dt2.format("%1$td.%1$tm.%1$tY %1$tH:%1$tM"), is("25.05.2019 19:53")); | ||
|
||
assertThat(dt2.format(Locale.GERMAN, "%1$td.%1$tm.%1$tY %1$tH:%1$tM"), is("25.05.2019 19:53")); | ||
assertThat(dt1.format(null, gmt), is("2019-05-25T17:53:10")); |
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.
Should be dt2
in this and the following lines.
I am going to enhance the tests. Ok for the idea of the toZone method rather than my new format methods. |
Enhanced tests Signed-off-by: Laurent Garnier <[email protected]>
@cweitkamp : PR is now finished and ready for a review. I will change the REST sitemap by using the new methods in a following PR. |
/** | ||
* Translate to a given zone | ||
* | ||
* @param zone the destination zone |
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.
* @param zone the destination zone | |
* @param zone the target {@link ZoneId} |
I am not sure if ZoneId
is available in DSL rules or NGRE thus I suggest to add a toUnit(String zone)
method too.
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.
Ok I will add it.
@@ -108,7 +109,7 @@ public DateTimeType(String zonedValue) { | |||
throw new IllegalArgumentException(zonedValue + " is not in a valid format.", invalidFormatException); | |||
} | |||
|
|||
zonedDateTime = date; | |||
zonedDateTime = date.withFixedOffsetZone(); |
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.
It looks like the right thing to streamline the API. But in opposition I found eclipse-archived/smarthome#5076 which questions if withFixedOffsetZone()
is the right way to go. Unfortunately with no result. @openhab/core-maintainers wdyt?
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.
withFixedOffsetZone() is applied to the other constructors so it makes sense to apply to this one too. It allows to get identical DateTimeType when calling the different constructors with the same data.
* @param zone the destination zone | ||
*/ | ||
public void toZone(ZoneId zone) { | ||
zonedDateTime = zonedDateTime.withZoneSameInstant(zone); |
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 might consider to return a new copy of DateTimeType here instead (like
Lines 187 to 207 in 9e7c6af
/** | |
* Convert this QuantityType to a new {@link QuantityType} using the given target unit. | |
* | |
* @param targetUnit the unit to which this {@link QuantityType} will be converted to. | |
* @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a | |
*/ | |
@SuppressWarnings("unchecked") | |
public @Nullable QuantityType<T> toUnit(Unit<?> targetUnit) { | |
if (!targetUnit.equals(getUnit())) { | |
try { | |
UnitConverter uc = getUnit().getConverterToAny(targetUnit); | |
Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit); | |
return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit); | |
} catch (UnconvertibleException | IncommensurableException e) { | |
logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit); | |
return null; | |
} | |
} | |
return this; | |
} |
Ok. I hesitated between the two options. |
Signed-off-by: Laurent Garnier <[email protected]>
@cweitkamp : changes done. |
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.
As both exceptions are inherited of RuntimeException
and they will be thrown if the user passes some wrong values it would be okay for me if we do not catch them anywhere.
boolean thrown = false; | ||
try { | ||
dt2 = dt.toZone("XXX"); | ||
} catch (DateTimeException e) { |
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.
This should be an own test which expects the thrown exception:
@Test(expected = DateTimeException.class)
public void changingZoneThrowsExceptionTest() {
// TODO
}
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.
Done
* @param pattern | ||
* @param expectedFormattedResult | ||
*/ | ||
public ParameterSet(TimeZone defaultTimeZone, Map<String, Integer> inputTimeMap, TimeZone inputTimeZone, |
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.
I am just wondering if we can reuse this constructor called by the other constructors. But that is only a side comment. Up to you.
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.
Yes we can.
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.
Done
...rg.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DateTimeTypeTest.java
Show resolved
Hide resolved
Do you know if this is possible to run a particular test only once and not for each ParsmeterSet? |
Signed-off-by: Laurent Garnier <[email protected]>
No, I am afraid, I do not know. But does it hurt? |
No, it just certainly takes few additional ms to run. |
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.
Thanks.
@openhab/core-maintainers Any objections regarding the withFixedOffsetZone()
method?
@cweitkamp I am honestly not really clear on the effect of that change, but as you have looked deeper into it and approved it, I don't see any reason not to merge it :-) |
Cool, I can move to the next step, using these methods to fix a bug. |
Just for clarification: From https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#withFixedOffsetZone--
Which basically means that it cuts off the "US/Central" (or whatever timezone you are in) string from the object (you can see that in the debugger). The quoted text above also states that cutting it off simplifies calculations based on this object later on (e.g. |
* Added some explanation for the website build. * Adapted to jenkins config changes. * Removed additional blank line. Signed-off-by: Jerome Luckenbach <[email protected]>
Signed-off-by: Laurent Garnier <[email protected]> GitOrigin-RevId: f03f328
Enhanced tests
Signed-off-by: Laurent Garnier [email protected]