ambition119 edited a comment on issue #96: support from unix timestamp to string date URL: https://github.com/apache/calcite-avatica/pull/96#issuecomment-489890614 > There are uses of default locale here. We can't have that. This code needs to behave identically in all locales. > > In one or two places the comment says "unsigned int" when the result is clearly a signed long. > > Methods that have a timezone argument must state very clearly, with examples, how the timezone is used. Are you converting to that timezone or from that timezone? So many times I expect a method to add 8 hours (to pacific) and it ends up subtracting 8 hours. Thank you for your suggestion, I fix the problem caused by timezone, How about changing to the following? convert user input TimeZone. ```java /** * Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to use's TimeZone datetime string * in the given format. */ public static String fromUnixTimestamp(long unixTimestamp, String format, TimeZone tz) { try { SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault(Locale.Category.FORMAT)); formatter.setTimeZone(UTC_ZONE); Date date = new Date(unixTimestamp * 1000); return formatter.format(date); } catch (Exception e) { return null; } } ``` test like : ``` assertThat(fromUnixTimestamp(-28800000,UTC_ZONE), is("1969-02-01 16:00:00")); assertThat(fromUnixTimestamp(1557134198, UTC_ZONE), is("2019-05-06 09:16:38")); assertThat(fromUnixTimestamp(1557134198, TimeZone.getTimeZone("GMT+2")), is("2019-05-06 11:16:38")); assertThat(fromUnixTimestamp(1557134198, TimeZone.getTimeZone("GMT+8")), is("2019-05-06 17:16:38")); ``` if convert to UTC TimeZone, also modify code like : ```java /** * Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to UTC datetime string * in the given format and TimeZone. */ public static String fromUnixTimestamp(long unixTimestamp, String format, TimeZone tz) { try { SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.getDefault(Locale.Category.FORMAT)); formatter.setTimeZone(tz); Date date = new Date(unixTimestamp * 1000); Long targetTime = date.getTime() - tz.getRawOffset() + UTC_ZONE.getRawOffset(); return formatter.format(new Date(targetTime)); } catch (Exception e) { return null; } } ``` test likeļ¼ ``` assertThat(fromUnixTimestamp(1557134198, UTC_ZONE), is("2019-05-06 09:16:38")); assertThat(fromUnixTimestamp(1557134198, TimeZone.getTimeZone("GMT+2")), is("2019-05-06 09:16:38")); assertThat(fromUnixTimestamp(1557134198, TimeZone.getTimeZone("GMT+8")), is("2019-05-06 09:16:38")); ```
---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
