View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.util;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  
23  import java.sql.Timestamp;
24  import java.util.TimeZone;
25  import java.util.concurrent.TimeUnit;
26  
27  /**
28   * Binary timestamp utility class of PostgreSQL.
29   */
30  @NoArgsConstructor(access = AccessLevel.PRIVATE)
31  public final class PostgreSQLBinaryTimestampUtils {
32      
33      private static final Long POSTGRESQL_SECONDS_OFFSET = 946684800L;
34      
35      private static final Long JULIAN_GREGORIAN_CALENDAR_CUTOFF_POINT = -13165977600L;
36      
37      /**
38       * Convert Timestamp to PostgreSQL time.
39       *
40       * @param timestamp timestamp
41       * @return PostgreSQL time
42       */
43      public static long toPostgreSQLTime(final Timestamp timestamp) {
44          long millis = timestamp.getTime() - (timestamp.getNanos() / 1000000L) + (TimeZone.getDefault().getRawOffset());
45          long nanos = timestamp.getNanos() / 1000L;
46          long pgSeconds = convertJavaEpochToPgEpoch(millis / 1000L);
47          if (nanos >= 1000000L) {
48              nanos -= 1000000L;
49              pgSeconds++;
50          }
51          return pgSeconds * 1000000L + nanos;
52      }
53      
54      /**
55       * Refer to <a href="https://github.com/pgjdbc/pgjdbc/blob/e5e36bd3e8ac87ae554ac5cd1ac664fcd0010073/pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java#L1453-L1475">
56       * org.postgresql.jdbc.TimestampUtils</a>.
57       * 
58       * @param seconds seconds
59       * @return epoch of PostgreSQL
60       */
61      private static long convertJavaEpochToPgEpoch(final long seconds) {
62          long result = seconds - POSTGRESQL_SECONDS_OFFSET;
63          if (result >= JULIAN_GREGORIAN_CALENDAR_CUTOFF_POINT) {
64              return result;
65          }
66          result = convertToJulianSeconds(result);
67          if (result < -15773356800L) {
68              int years = (int) ((result + 15773356800L) / -3155823050L);
69              years++;
70              years -= years / 4;
71              result += years * 86400L;
72          }
73          return result;
74      }
75      
76      private static long convertToJulianSeconds(final long seconds) {
77          return seconds - TimeUnit.DAYS.toSeconds(10);
78      }
79  }