Merge pull request #9768 from thingsboard/fix_bug_tbDate_default_Tz
[3.6.2]Fix_bug_Tbdate_default_tz
This commit is contained in:
commit
6a0a5750e0
@ -58,7 +58,7 @@ public class TbDate implements Serializable, Cloneable {
|
||||
}
|
||||
|
||||
public TbDate(String s, String pattern, Locale locale) {
|
||||
instant = parseInstant(s, pattern, locale, zoneIdUTC);
|
||||
instant = parseInstant(s, pattern, locale, ZoneId.systemDefault());
|
||||
}
|
||||
public TbDate(String s, String pattern, Locale locale, String zoneIdStr) {
|
||||
ZoneId zoneId = ZoneId.of(zoneIdStr);
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.thingsboard.script.api.tbel;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
@ -151,15 +153,25 @@ class TbDateTest {
|
||||
|
||||
@Test
|
||||
void testToLocaleDateString() {
|
||||
String s = "09:15:30 PM, Sun 10/09/2022";
|
||||
String s = "02:15:30 PM, Sun 10/09/2022";
|
||||
String pattern = "hh:mm:ss a, EEE M/d/uuuu";
|
||||
TbDate d = new TbDate(s, pattern, Locale.US);
|
||||
Assert.assertEquals("2022-10-09T21:15:30Z", d.toISOString());
|
||||
// tz = local
|
||||
int localOffsetHrs = ZoneId.systemDefault().getRules().getOffset(d.getInstant()).getTotalSeconds()/60/60;
|
||||
int hrs = 14 - localOffsetHrs;
|
||||
String expected = "2022-10-09T" + hrs + ":15:30Z";
|
||||
Assert.assertEquals(expected, d.toISOString());
|
||||
|
||||
// tz = "-04:00"
|
||||
s = "2023-08-06T04:04:05.00-04:00";
|
||||
d = new TbDate(s);
|
||||
Assert.assertEquals("2023-08-06T08:04:05Z", d.toISOString());
|
||||
|
||||
s = "02:15:30 PM, Sun 10/09/2022";
|
||||
d = new TbDate(s, pattern, Locale.US, "-04:00");
|
||||
Assert.assertEquals("2022-10-10T01:15:30Z", d.toISOString());
|
||||
Assert.assertEquals("2022-10-09T18:15:30Z", d.toISOString());
|
||||
d = new TbDate(s, pattern, Locale.US, "America/New_York");
|
||||
Assert.assertEquals("2022-10-10T01:15:30Z", d.toISOString());
|
||||
Assert.assertEquals("2022-10-09T18:15:30Z", d.toISOString());
|
||||
// tz = "+02:00"
|
||||
/**
|
||||
* For Java 11:
|
||||
@ -171,10 +183,13 @@ class TbDateTest {
|
||||
d = new TbDate(s, pattern, Locale.GERMAN, ZoneId.of("Europe/Berlin"));
|
||||
Assert.assertEquals("2022-10-09T19:15:30Z", d.toISOString());
|
||||
|
||||
s = "09:15:30 пп, середа, 4 жовтня 2023 р.";
|
||||
s = "02:15:30 пп, середа, 4 жовтня 2023 р.";
|
||||
pattern = "hh:mm:ss a, EEEE, d MMMM y 'р.'";
|
||||
d = new TbDate(s, pattern, Locale.forLanguageTag("uk-UA"));
|
||||
Assert.assertEquals("2023-10-04T21:15:30Z", d.toISOString());
|
||||
localOffsetHrs = ZoneId.systemDefault().getRules().getOffset(d.getInstant()).getTotalSeconds()/60/60;
|
||||
hrs = 14 - localOffsetHrs;
|
||||
expected = "2023-10-04T" + hrs + ":15:30Z";
|
||||
Assert.assertEquals(expected, d.toISOString());
|
||||
|
||||
d = new TbDate(1693962245000L);
|
||||
Assert.assertEquals("2023-09-06T01:04:05Z", d.toISOString());
|
||||
@ -782,4 +797,19 @@ class TbDateTest {
|
||||
Assert.assertEquals(d.getSeconds(), d.getUTCSeconds());
|
||||
Assert.assertEquals(d.getMilliseconds(), d.getUTCMilliseconds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tbDateSerializedPMapperTest() {
|
||||
String stringDateUTC = "2023-09-06T01:04:05.345Z";
|
||||
TbDate expectedDate = new TbDate(stringDateUTC);
|
||||
String serializedTbDate = JacksonUtil.toString(expectedDate);
|
||||
JsonNode tbDateNode = JacksonUtil.toJsonNode(serializedTbDate);
|
||||
Assert.assertNotNull(tbDateNode);
|
||||
((ObjectNode) tbDateNode).put("test", (String) null);
|
||||
serializedTbDate = JacksonUtil.toString(tbDateNode);
|
||||
TbDate actualDate = JacksonUtil.fromStringIgnoreUnknownProperties(serializedTbDate, TbDate.class);
|
||||
Assert.assertNotNull(actualDate);
|
||||
Assert.assertEquals(expectedDate.toString(), actualDate.toString());
|
||||
Assert.assertEquals(expectedDate.getInstant(), actualDate.getInstant());
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,7 +52,9 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class JacksonUtil {
|
||||
|
||||
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
|
||||
.registerModule(new JavaTimeModule())
|
||||
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
public static final ObjectMapper PRETTY_SORTED_JSON_MAPPER = JsonMapper.builder()
|
||||
.enable(SerializationFeature.INDENT_OUTPUT)
|
||||
.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
|
||||
@ -64,7 +66,8 @@ public class JacksonUtil {
|
||||
.build();
|
||||
public static final ObjectMapper IGNORE_UNKNOWN_PROPERTIES_JSON_MAPPER = JsonMapper.builder()
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
||||
.build();
|
||||
.build()
|
||||
.registerModule(new JavaTimeModule());
|
||||
|
||||
public static ObjectMapper getObjectMapperWithJavaTimeModule() {
|
||||
return new ObjectMapper().registerModule(new JavaTimeModule());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user