osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calcite Time Conversion doubt


The root of the problem is that SQL’s TIMESTAMP type has no time zone (not even UTC) whereas Java’s Timestamp represents an instant in time, internally represented as an offset from the UTC epoch. Therefore translation between java.sql.Timestamp and SQL TIMESTAMP always needs a time zone.

> On Dec 27, 2018, at 7:53 PM, jia yichao <yc_jia@xxxxxx> wrote:
> 
> Hi community
> 
> 
> Recently I have encountered a problem with time conversion.  I want to know why the following methods need to add or subtract time zone offsets. Is this designed to meet some specifications?  These methods are located in SqlFunctions.java. 
> 
> /** Converts the Java type used for UDF parameters of SQL TIMESTAMP type
>   * ({@link java.sql.Timestamp}) to internal representation (long).
>   *
>   * <p>Converse of {@link #internalToTimestamp(long)}. */
>  public static long toLong(Timestamp v) {
>    return toLong(v, LOCAL_TZ);
>  }
> 
> // mainly intended for java.sql.Timestamp but works for other dates also
>  public static long toLong(java.util.Date v, TimeZone timeZone) {
>    final long time = v.getTime();
>    return time + timeZone.getOffset(time);
>  }
> 
> /** Converts the internal representation of a SQL TIMESTAMP (long) to the Java
>   * type used for UDF parameters ({@link java.sql.Timestamp}). */
>  public static java.sql.Timestamp internalToTimestamp(long v) {
>    return new java.sql.Timestamp(v - LOCAL_TZ.getOffset(v));
>  }
> 
> thanks
> Jiayichao