@@ -36,37 +36,47 @@ public class DatetimeUtilsTest {
3636 private ZoneId zoneId ;
3737 // 1546413207689
3838 // 2019-01-02T15:13:27.689+08:00
39+ private final long timestamp = 1546413207689L ;
3940 private long delta ;
4041
4142 @ Before
4243 public void setUp () throws Exception {
44+ }
45+
46+ @ After
47+ public void tearDown () throws Exception {
48+ }
49+
50+ @ Test
51+ public void test1 () throws LogicalOperatorException {
4352 zoneOffset = ZonedDateTime .now ().getOffset ();
4453 zoneId = ZoneId .systemDefault ();
45- System .out .println (zoneOffset .toString ());
4654 if (zoneOffset .toString ().equals ("Z" )){
4755 delta = 8 * 3600000 ;
48- zoneOffset = ZoneOffset .of ("+00:00" );
4956 } else {
5057 delta = (8 - Long .parseLong (zoneOffset .toString ().split (":" )[0 ])) * 3600000 ;
5158 }
52- System .out .println (delta );
59+ testConvertDatetimeStrToLongWithoutMS (zoneOffset , zoneId , timestamp - 689 + delta );
60+ testConvertDatetimeStrToLongWithMS (zoneOffset , zoneId , timestamp + delta );
5361 }
5462
55- @ After
56- public void tearDown () throws Exception {
63+ @ Test
64+ public void test2 () throws LogicalOperatorException {
65+ zoneOffset = ZoneOffset .UTC ;
66+ zoneId = ZoneId .of ("Etc/UTC" );
67+ delta = 8 * 3600000 ;
68+ testConvertDatetimeStrToLongWithoutMS (zoneOffset , zoneId , timestamp - 689 + delta );
69+ testConvertDatetimeStrToLongWithMS (zoneOffset , zoneId , timestamp + delta );
5770 }
5871
59- @ Test
60- public void testConvertDatetimeStrToLongWithoutMS () throws LogicalOperatorException {
72+ public void testConvertDatetimeStrToLongWithoutMS (ZoneOffset zoneOffset , ZoneId zoneId , long res ) throws LogicalOperatorException {
6173 String [] timeFormatWithoutMs = new String []{"2019-01-02 15:13:27" , "2019/01/02 15:13:27" ,
6274 "2019.01.02 15:13:27" , "2019-01-02T15:13:27" , "2019/01/02T15:13:27" , "2019.01.02T15:13:27" ,
6375 "2019-01-02 15:13:27" + zoneOffset , "2019/01/02 15:13:27" + zoneOffset ,
6476 "2019.01.02 15:13:27" + zoneOffset , "2019-01-02T15:13:27" + zoneOffset ,
6577 "2019/01/02T15:13:27" + zoneOffset , "2019.01.02T15:13:27" + zoneOffset ,};
66-
67- long res = 1546413207000L + delta ;
6878 for (String str : timeFormatWithoutMs ) {
69- Assert .assertEquals (res , DatetimeUtils .convertDatetimeStrToMillisecond (str , zoneOffset ));
79+ Assert .assertEquals (res , DatetimeUtils .convertDatetimeStrToMillisecond (str , zoneOffset , 0 ));
7080 }
7181
7282 for (String str : timeFormatWithoutMs ) {
@@ -75,19 +85,16 @@ public void testConvertDatetimeStrToLongWithoutMS() throws LogicalOperatorExcept
7585
7686 }
7787
78- @ Test
79- public void testConvertDatetimeStrToLongWithMS () throws LogicalOperatorException {
88+ public void testConvertDatetimeStrToLongWithMS (ZoneOffset zoneOffset , ZoneId zoneId , long res ) throws LogicalOperatorException {
8089 String [] timeFormatWithoutMs = new String []{"2019-01-02 15:13:27.689" ,
8190 "2019/01/02 15:13:27.689" ,
8291 "2019.01.02 15:13:27.689" , "2019-01-02T15:13:27.689" , "2019/01/02T15:13:27.689" ,
8392 "2019.01.02T15:13:27.689" , "2019-01-02 15:13:27.689" + zoneOffset ,
8493 "2019/01/02 15:13:27.689" + zoneOffset , "2019.01.02 15:13:27.689" + zoneOffset ,
8594 "2019-01-02T15:13:27.689" + zoneOffset , "2019/01/02T15:13:27.689" + zoneOffset ,
8695 "2019.01.02T15:13:27.689" + zoneOffset ,};
87-
88- long res = 1546413207689L + delta ;
8996 for (String str : timeFormatWithoutMs ) {
90- assertEquals (res , DatetimeUtils .convertDatetimeStrToMillisecond (str , zoneOffset ));
97+ assertEquals (res , DatetimeUtils .convertDatetimeStrToMillisecond (str , zoneOffset , 0 ));
9198 }
9299
93100 for (String str : timeFormatWithoutMs ) {
@@ -101,4 +108,12 @@ public void createTest() {
101108 // ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("+08:00"));
102109 // System.out.println(zonedDateTime);
103110 }
111+
112+ public static void main (String [] args ){
113+ // System.out.println(DatetimeUtils.toZoneOffset(ZoneId.of("Etc/UTC")));
114+ for (String zoneId : ZoneId .getAvailableZoneIds ()){
115+ System .out .println (zoneId + ": " + DatetimeUtils .toZoneOffset (ZoneId .of (zoneId )));
116+ }
117+ // System.out.println(ZoneOffset.of("+00:00"));
118+ }
104119}
0 commit comments