mdayakar commented on code in PR #4760: URL: https://github.com/apache/hive/pull/4760#discussion_r1346968329
########## standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java: ########## @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hive.metastore.utils; + +import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.sql.Date; +import java.sql.Timestamp; +import java.time.ZoneId; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +@Category(MetastoreUnitTest.class) +public class TestMetaStoreUtils { + + @Test + public void testConvertDateToString() { + TimeZone defaultTZ = TimeZone.getDefault(); + try { + TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("Asia/Hong_Kong"))); + String date = MetaStoreUtils.convertDateToString(Date.valueOf("2023-01-01")); + assertEquals("2023-01-01", date); + } finally { + TimeZone.setDefault(defaultTZ); + } + } + + @Test + public void testcConvertTimestampToString() { + TimeZone defaultTZ = TimeZone.getDefault(); + try { + String date = MetaStoreUtils.convertTimestampToString(Timestamp.valueOf("2023-01-01 10:20:30")); + assertEquals("2023-01-01 10:20:30", date); + } finally { + TimeZone.setDefault(defaultTZ); + } Review Comment: Thanks for the commit (https://github.com/apache/hive/commit/4ff9ba968b31ea16611ba5d5bc05bbfb3b4fe184) @zabetak , changed the test code as per your commit. Just added negative testcases when invalid date/timestamp is passed to the APIs. > With the new tests, I found a bug in convertTimestampToString using [zoneId=Europe/Paris, timestamp=9743-03-31T03:37:33] but I think this existed since day one so we can possibly defer it to another PR. I found this issue is not with our code, the problem is coming from `java.sql.Timestamp.valueOf()` API. Below are some observations, here for time less than 4hrs it is adding +1 as timezone and for 3hrs it is also changing 03hrs to 02hrs. Not sure exactly why it is happening. We may need to check further. ``` Input String: 9743-03-31 05:37:33 result = {Timestamp@3282} "9743-03-31 05:37:33.0" nanos = 0 fastTime = 245299894653000 cdate = {Gregorian$Date@3284} "9743-03-31T05:37:33.000+0200" Input String: 9743-03-31 04:37:33 result = {Timestamp@3236} "9743-03-31 04:37:33.0" nanos = 0 fastTime = 245299891053000 cdate = {Gregorian$Date@3238} "9743-03-31T04:37:33.000+0200" Input String: 9743-03-31 03:37:33 result = {Timestamp@3244} "9743-03-31 02:37:33.0" nanos = 0 fastTime = 245299887453000 cdate = {Gregorian$Date@3246} "9743-03-31T02:37:33.000+0100" Input String: 9743-03-31 02:37:33 result = {Timestamp@3274} "9743-03-31 02:37:33.0" nanos = 0 fastTime = 245299887453000 cdate = {Gregorian$Date@3276} "9743-03-31T02:37:33.000+0100" Input String: 9743-03-31 01:37:33 result = {Timestamp@3289} "9743-03-31 01:37:33.0" nanos = 0 fastTime = 245299883853000 cdate = {Gregorian$Date@3291} "9743-03-31T01:37:33.000+0100" ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org