Package org.postgresql.jdbc
Class TimestampUtils
- java.lang.Object
-
- org.postgresql.jdbc.TimestampUtils
-
public class TimestampUtils extends java.lang.ObjectMisc utils for handling time and date values.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTimestampUtils.Infinityprivate static classTimestampUtils.ParsedBinaryTimestampprivate static classTimestampUtils.ParsedTimestamp
-
Field Summary
Fields Modifier and Type Field Description private java.util.CalendarcalCacheprivate java.time.ZoneOffsetcalCacheZoneprivate java.util.CalendarcalendarWithUserTzprivate static java.lang.reflect.FieldDEFAULT_TIME_ZONE_FIELDprivate java.util.TimeZonedefaultTimeZoneCacheprivate static java.util.HashMap<java.lang.String,java.util.TimeZone>GMT_ZONESprivate static byte[]INFINITYprivate ResourceLocklockprivate static java.time.LocalDateTimeMAX_LOCAL_DATETIMEprivate static intMAX_NANOS_BEFORE_WRAP_ON_ROUNDprivate static byte[]MAX_OFFSETprivate static java.time.OffsetDateTimeMAX_OFFSET_DATETIMEprivate static java.time.LocalTimeMAX_TIMEprivate static longMAX_TIME_NANOSprivate static java.time.LocalDateMIN_LOCAL_DATEprivate static java.time.LocalDateTimeMIN_LOCAL_DATETIMEprivate static java.time.OffsetDateTimeMIN_OFFSET_DATETIMEprivate static byte[]NEGATIVE_INFINITYprivate static char[][]NUMBERSprivate static java.time.DurationONE_MICROSECONDprivate static intONEDAYNumber of milliseconds in one day.private static java.time.DurationPG_EPOCH_DIFFprivate java.util.TimeZoneprevDefaultZoneFieldValueprivate java.lang.StringBuildersbufprivate Provider<java.util.TimeZone>timeZoneProviderprivate booleanusesDoubleTrue if the backend uses doubles for time values.private static java.util.TimeZoneUTC_TIMEZONEprivate static char[]ZEROS
-
Constructor Summary
Constructors Constructor Description TimestampUtils(boolean usesDouble, Provider<java.util.TimeZone> timeZoneProvider)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private static voidappendDate(java.lang.StringBuilder sb, int year, int month, int day)private static voidappendDate(java.lang.StringBuilder sb, java.time.LocalDate localDate)private static voidappendDate(java.lang.StringBuilder sb, java.util.Calendar cal)private static voidappendEra(java.lang.StringBuilder sb, java.time.LocalDate localDate)private static voidappendEra(java.lang.StringBuilder sb, java.util.Calendar cal)private static voidappendTime(java.lang.StringBuilder sb, int hours, int minutes, int seconds, int nanos)Appends time part to theStringBuilderin PostgreSQL-compatible format.private static voidappendTime(java.lang.StringBuilder sb, java.time.LocalTime localTime)private static voidappendTime(java.lang.StringBuilder sb, java.util.Calendar cal, int nanos)private static voidappendTimeZone(java.lang.StringBuilder sb, int offset)private static voidappendTimeZone(java.lang.StringBuilder sb, java.time.ZoneOffset offset)private static voidappendTimeZone(java.lang.StringBuilder sb, java.util.Calendar cal)java.sql.DateconvertToDate(long millis, java.util.TimeZone tz)Extracts the date part from a timestamp.java.sql.TimeconvertToTime(long millis, java.util.TimeZone tz)Extracts the time part from a timestamp.private static intfirstNonDigit(byte[] bytes, int start)private static longfloorDiv(long x, long y)private static longfloorMod(long x, long y)private java.util.CalendargetCalendar(java.time.ZoneOffset offset)private java.util.TimeZonegetDefaultTz()java.util.CalendargetSharedCalendar(java.util.TimeZone timeZone)Get a shared calendar, applying the supplied time zone or the default time zone if null.private longguessTimestamp(long millis, java.util.TimeZone tz)Given a UTC timestampmillisfinds another point in time that is rendered in given time zonetzexactly as "millis in UTC".booleanhasFastDefaultTimeZone()private static booleanisSimpleTimeZone(java.lang.String id)private static booleannanosExceed499(int nanos)Returns true when microsecond part of the time should be increased when rounding to microsecondsprivate static intnumber(byte[] bytes, int start, int end)private static TimestampUtils.ParsedTimestampparseBackendTimestamp(byte[] s)Load date/time information into the provided calendar returning the fractional seconds.static java.util.TimeZoneparseBackendTimeZone(java.lang.String timeZone)Converts backend's TimeZone parameter to java format.(package private) TimestampUtils.ParsedTimestampparseDate(byte[] dateBytes)private java.util.CalendarsetupCalendar(java.util.Calendar cal)private static intskipWhitespace(byte[] bytes, int start)java.lang.StringtimeToString(java.util.Date time, boolean withTimeZone)Returns the given time value as String matching what the current postgresql server would send in text mode.voidtoBinDate(java.util.TimeZone tz, byte[] bytes, java.sql.Date value)Converts the SQL Date to binary representation forOid.DATE.java.sql.DatetoDate(java.util.Calendar cal, byte[] dateBytes)java.sql.DatetoDate(java.util.Calendar cal, java.lang.String s)java.sql.DatetoDateBin(java.util.TimeZone tz, byte[] bytes)Returns the SQL Date object matching the given bytes withOid.DATE.private static longtoJavaSecs(long secs)Converts the given postgresql seconds to java seconds.java.time.LocalDatetoLocalDate(byte[] dateBytes)java.time.LocalDatetoLocalDateBin(byte[] bytes)Returns the local date time object matching the given bytes withOid.DATEorOid.TIMESTAMP.java.time.LocalDateTimetoLocalDateTime(byte[] bytes)Parse an array of bytes and return a LocalDateTime representing its value.java.time.LocalDateTimetoLocalDateTime(java.lang.String s)java.time.LocalDateTimetoLocalDateTimeBin(byte[] bytes)Returns the local date time object matching the given bytes withOid.TIMESTAMPorOid.TIMESTAMPTZ.java.time.LocalTimetoLocalTime(java.lang.String s)Parse a string and return a LocalTime representing its value.java.time.LocalTimetoLocalTimeBin(byte[] bytes)Returns the SQL Time object matching the given bytes withOid.TIME.java.time.OffsetDateTimetoOffsetDateTime(byte[] bytes)Parse an array of bytes and return a OffsetDateTime representing its value.java.time.OffsetDateTimetoOffsetDateTime(java.lang.String s)Parse a string and return a OffsetDateTime representing its value.java.time.OffsetDateTimetoOffsetDateTime(java.sql.Time t)Deprecated.was used internally, and not used anymorejava.time.OffsetDateTimetoOffsetDateTimeBin(byte[] bytes)Returns the offset date time object matching the given bytes with Oid#TIMESTAMPTZ.java.time.OffsetTimetoOffsetTime(byte[] bytes)Parse an array of bytes and return a OffsetTime representing its value.java.time.OffsetTimetoOffsetTime(java.lang.String s)Parse a string and return a OffsetTime representing its value.java.time.OffsetTimetoOffsetTimeBin(byte[] bytes)Returns the offset time object matching the given bytes with Oid#TIMETZ or Oid#TIME.private TimestampUtils.ParsedBinaryTimestamptoParsedTimestampBin(java.util.TimeZone tz, byte[] bytes, boolean timestamptz)private TimestampUtils.ParsedBinaryTimestamptoParsedTimestampBinPlain(byte[] bytes)private static longtoPgSecs(long secs)Converts the given java seconds to postgresql seconds.private TimestampUtils.ParsedBinaryTimestamptoProlepticParsedTimestampBin(byte[] bytes)java.lang.StringtoString(java.time.LocalDate localDate)java.lang.StringtoString(java.time.LocalDateTime localDateTime)FormatsLocalDateTimeto be sent to the backend, thus it adds time zone.java.lang.StringtoString(java.time.LocalTime localTime)java.lang.StringtoString(java.time.OffsetDateTime offsetDateTime)java.lang.StringtoString(java.time.OffsetTime offsetTime)java.lang.StringtoString(java.util.Calendar cal, java.sql.Date x)java.lang.StringtoString(java.util.Calendar cal, java.sql.Date x, boolean withTimeZone)java.lang.StringtoString(java.util.Calendar cal, java.sql.Time x)java.lang.StringtoString(java.util.Calendar cal, java.sql.Time x, boolean withTimeZone)java.lang.StringtoString(java.util.Calendar cal, java.sql.Timestamp x)java.lang.StringtoString(java.util.Calendar cal, java.sql.Timestamp x, boolean withTimeZone)java.lang.StringtoStringOffsetDateTime(byte[] value)Convertstimestamptzto string taking client time zone (timeZoneProvider) into account.java.lang.StringtoStringOffsetTimeBin(byte[] value)Convertstimetzto string taking client time zone (timeZoneProvider) into account.java.sql.TimetoTime(java.util.Calendar cal, byte[] bytes)java.sql.TimetoTime(java.util.Calendar cal, java.lang.String s)java.sql.TimetoTimeBin(java.util.TimeZone tz, byte[] bytes)Returns the SQL Time object matching the given bytes withOid.TIMEorOid.TIMETZ.java.sql.TimestamptoTimestamp(java.util.Calendar cal, byte[] bytes)Parse an array of bytes and return a timestamp representing its value.java.sql.TimestamptoTimestamp(java.util.Calendar cal, java.lang.String s)Parse a string and return a timestamp representing its value.java.sql.TimestamptoTimestampBin(java.util.TimeZone tz, byte[] bytes, boolean timestamptz)Returns the SQL Timestamp object matching the given bytes withOid.TIMESTAMPorOid.TIMESTAMPTZ.java.time.OffsetDateTimewithClientOffsetSameInstant(java.time.OffsetDateTime input)PostgreSQL does not store the time zone in the binary representation of timestamptz.java.time.OffsetTimewithClientOffsetSameInstant(java.time.OffsetTime input)PostgreSQL does not store the time zone in the binary representation of timetz.
-
-
-
Field Detail
-
ONEDAY
private static final int ONEDAY
Number of milliseconds in one day.- See Also:
- Constant Field Values
-
ZEROS
private static final char[] ZEROS
-
NUMBERS
private static final char[][] NUMBERS
-
GMT_ZONES
private static final java.util.HashMap<java.lang.String,java.util.TimeZone> GMT_ZONES
-
MAX_NANOS_BEFORE_WRAP_ON_ROUND
private static final int MAX_NANOS_BEFORE_WRAP_ON_ROUND
- See Also:
- Constant Field Values
-
ONE_MICROSECOND
private static final java.time.Duration ONE_MICROSECOND
-
MAX_TIME
private static final java.time.LocalTime MAX_TIME
-
MAX_TIME_NANOS
private static final long MAX_TIME_NANOS
-
MAX_OFFSET_DATETIME
private static final java.time.OffsetDateTime MAX_OFFSET_DATETIME
-
MAX_LOCAL_DATETIME
private static final java.time.LocalDateTime MAX_LOCAL_DATETIME
-
MIN_LOCAL_DATE
private static final java.time.LocalDate MIN_LOCAL_DATE
-
MIN_LOCAL_DATETIME
private static final java.time.LocalDateTime MIN_LOCAL_DATETIME
-
MIN_OFFSET_DATETIME
private static final java.time.OffsetDateTime MIN_OFFSET_DATETIME
-
PG_EPOCH_DIFF
private static final java.time.Duration PG_EPOCH_DIFF
-
DEFAULT_TIME_ZONE_FIELD
private static final java.lang.reflect.Field DEFAULT_TIME_ZONE_FIELD
-
UTC_TIMEZONE
private static final java.util.TimeZone UTC_TIMEZONE
-
INFINITY
private static final byte[] INFINITY
-
NEGATIVE_INFINITY
private static final byte[] NEGATIVE_INFINITY
-
MAX_OFFSET
private static final byte[] MAX_OFFSET
-
prevDefaultZoneFieldValue
private java.util.TimeZone prevDefaultZoneFieldValue
-
defaultTimeZoneCache
private java.util.TimeZone defaultTimeZoneCache
-
sbuf
private final java.lang.StringBuilder sbuf
-
calendarWithUserTz
private final java.util.Calendar calendarWithUserTz
-
calCache
private java.util.Calendar calCache
-
calCacheZone
private java.time.ZoneOffset calCacheZone
-
usesDouble
private final boolean usesDouble
True if the backend uses doubles for time values. False if long is used.
-
timeZoneProvider
private final Provider<java.util.TimeZone> timeZoneProvider
-
lock
private final ResourceLock lock
-
-
Constructor Detail
-
TimestampUtils
public TimestampUtils(boolean usesDouble, Provider<java.util.TimeZone> timeZoneProvider)
-
-
Method Detail
-
getCalendar
private java.util.Calendar getCalendar(java.time.ZoneOffset offset)
-
parseBackendTimestamp
private static TimestampUtils.ParsedTimestamp parseBackendTimestamp(byte[] s) throws java.sql.SQLException
Load date/time information into the provided calendar returning the fractional seconds.- Throws:
java.sql.SQLException
-
parseDate
TimestampUtils.ParsedTimestamp parseDate(byte[] dateBytes)
-
toTimestamp
public java.sql.Timestamp toTimestamp(java.util.Calendar cal, java.lang.String s) throws java.sql.SQLExceptionParse a string and return a timestamp representing its value.- Parameters:
cal- calendar to be used to parse the input strings- The ISO formatted date string to parse.- Returns:
- null if s is null or a timestamp of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toTimestamp
public java.sql.Timestamp toTimestamp(java.util.Calendar cal, byte[] bytes) throws java.sql.SQLExceptionParse an array of bytes and return a timestamp representing its value.- Parameters:
cal- calendar to be used to parse the input bytesbytes- The ISO formatted date to parse.- Returns:
- null if bytes is null or a timestamp of the parsed bytes.
- Throws:
java.sql.SQLException- if there is a problem parsing bytes.
-
toLocalTime
public java.time.LocalTime toLocalTime(java.lang.String s) throws java.sql.SQLExceptionParse a string and return a LocalTime representing its value.- Parameters:
s- The ISO formatted time string to parse.- Returns:
- null if s is null or a LocalTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toOffsetTimeBin
public java.time.OffsetTime toOffsetTimeBin(byte[] bytes) throws PSQLExceptionReturns the offset time object matching the given bytes with Oid#TIMETZ or Oid#TIME.- Parameters:
bytes- The binary encoded TIMETZ/TIME value.- Returns:
- The parsed offset time object.
- Throws:
PSQLException- If binary format could not be parsed.
-
toOffsetTime
public java.time.OffsetTime toOffsetTime(java.lang.String s) throws java.sql.SQLExceptionParse a string and return a OffsetTime representing its value.- Parameters:
s- The ISO formatted time string to parse.- Returns:
- null if s is null or a OffsetTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toOffsetTime
public java.time.OffsetTime toOffsetTime(byte[] bytes) throws java.sql.SQLExceptionParse an array of bytes and return a OffsetTime representing its value.- Parameters:
bytes- The ISO time formatted array of bytes time to parse.- Returns:
- null if bytes are null or a OffsetTime of the parsed string .
- Throws:
java.sql.SQLException- if there is a problem parsing bytes.
-
toLocalDateTime
public java.time.LocalDateTime toLocalDateTime(java.lang.String s) throws java.sql.SQLException- Parameters:
s- The ISO formatted date string to parse.- Returns:
- null if s is null or a LocalDateTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toLocalDateTime
public java.time.LocalDateTime toLocalDateTime(byte[] bytes) throws java.sql.SQLExceptionParse an array of bytes and return a LocalDateTime representing its value.- Parameters:
bytes- The ISO formatted date array of bytes to parse.- Returns:
- null if s is null or a LocalDateTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing bytes.
-
toOffsetDateTime
@Deprecated public java.time.OffsetDateTime toOffsetDateTime(java.sql.Time t)
Deprecated.was used internally, and not used anymoreReturns the offset date time object matching the given bytes with Oid#TIMETZ. Not used internally anymore, function is here to retain compatibility with previous versions- Parameters:
t- the time value- Returns:
- the matching offset date time
-
toOffsetDateTime
public java.time.OffsetDateTime toOffsetDateTime(java.lang.String s) throws java.sql.SQLExceptionParse a string and return a OffsetDateTime representing its value.- Parameters:
s- The ISO formatted date string to parse.- Returns:
- null if s is null or a OffsetDateTime of the parsed string s.
- Throws:
java.sql.SQLException- if there is a problem parsing s.
-
toOffsetDateTime
public java.time.OffsetDateTime toOffsetDateTime(byte[] bytes) throws java.sql.SQLExceptionParse an array of bytes and return a OffsetDateTime representing its value.- Parameters:
bytes- The ISO formatted date string to parse.- Returns:
- null if bytes is null or an OffsetDateTime of the parsed array of bytes.
- Throws:
java.sql.SQLException- if there is a problem parsing bytes.
-
toOffsetDateTimeBin
public java.time.OffsetDateTime toOffsetDateTimeBin(byte[] bytes) throws PSQLExceptionReturns the offset date time object matching the given bytes with Oid#TIMESTAMPTZ.- Parameters:
bytes- The binary encoded local date time value.- Returns:
- The parsed local date time object.
- Throws:
PSQLException- If binary format could not be parsed.
-
toTime
public java.sql.Time toTime(java.util.Calendar cal, java.lang.String s) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
toTime
public java.sql.Time toTime(java.util.Calendar cal, byte[] bytes) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
toDate
public java.sql.Date toDate(java.util.Calendar cal, java.lang.String s) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
toDate
public java.sql.Date toDate(java.util.Calendar cal, byte[] dateBytes) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
toLocalDate
public java.time.LocalDate toLocalDate(byte[] dateBytes) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
setupCalendar
private java.util.Calendar setupCalendar(java.util.Calendar cal)
-
getSharedCalendar
public java.util.Calendar getSharedCalendar(java.util.TimeZone timeZone)
Get a shared calendar, applying the supplied time zone or the default time zone if null.- Parameters:
timeZone- time zone to be set for the calendar- Returns:
- The shared calendar.
-
nanosExceed499
private static boolean nanosExceed499(int nanos)
Returns true when microsecond part of the time should be increased when rounding to microseconds- Parameters:
nanos- nanosecond part of the time- Returns:
- true when microsecond part of the time should be increased when rounding to microseconds
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Timestamp x)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Timestamp x, boolean withTimeZone)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Date x)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Date x, boolean withTimeZone)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Time x)
-
toString
public java.lang.String toString(java.util.Calendar cal, java.sql.Time x, boolean withTimeZone)
-
appendDate
private static void appendDate(java.lang.StringBuilder sb, java.util.Calendar cal)
-
appendDate
private static void appendDate(java.lang.StringBuilder sb, int year, int month, int day)
-
appendTime
private static void appendTime(java.lang.StringBuilder sb, java.util.Calendar cal, int nanos)
-
appendTime
private static void appendTime(java.lang.StringBuilder sb, int hours, int minutes, int seconds, int nanos)Appends time part to theStringBuilderin PostgreSQL-compatible format. The function truncatesnanosto microseconds. The value is expected to be rounded beforehand.- Parameters:
sb- destinationhours- hoursminutes- minutesseconds- secondsnanos- nanoseconds
-
appendTimeZone
private static void appendTimeZone(java.lang.StringBuilder sb, java.util.Calendar cal)
-
appendTimeZone
private static void appendTimeZone(java.lang.StringBuilder sb, int offset)
-
appendEra
private static void appendEra(java.lang.StringBuilder sb, java.util.Calendar cal)
-
toString
public java.lang.String toString(java.time.LocalDate localDate)
-
toString
public java.lang.String toString(java.time.LocalTime localTime)
-
toString
public java.lang.String toString(java.time.OffsetTime offsetTime)
-
toStringOffsetTimeBin
public java.lang.String toStringOffsetTimeBin(byte[] value) throws PSQLExceptionConvertstimetzto string taking client time zone (timeZoneProvider) into account.- Parameters:
value- binary representation oftimetz- Returns:
- string representation of
timetz - Throws:
PSQLException
-
withClientOffsetSameInstant
public java.time.OffsetTime withClientOffsetSameInstant(java.time.OffsetTime input)
PostgreSQL does not store the time zone in the binary representation of timetz. However, we want to preserve the output ofgetString()in both binary and text formats So we try a client time zone when serializingOffsetTimeto string.- Parameters:
input- input offset time- Returns:
- adjusted offset time (it represents the same instant as the input one)
-
toString
public java.lang.String toString(java.time.OffsetDateTime offsetDateTime)
-
toStringOffsetDateTime
public java.lang.String toStringOffsetDateTime(byte[] value) throws PSQLExceptionConvertstimestamptzto string taking client time zone (timeZoneProvider) into account.- Parameters:
value- binary representation oftimestamptz- Returns:
- string representation of
timestamptz - Throws:
PSQLException
-
withClientOffsetSameInstant
public java.time.OffsetDateTime withClientOffsetSameInstant(java.time.OffsetDateTime input)
PostgreSQL does not store the time zone in the binary representation of timestamptz. However, we want to preserve the output ofgetString()in both binary and text formats So we try a client time zone when serializingOffsetDateTimeto string.- Parameters:
input- input offset date time- Returns:
- adjusted offset date time (it represents the same instant as the input one)
-
toString
public java.lang.String toString(java.time.LocalDateTime localDateTime)
FormatsLocalDateTimeto be sent to the backend, thus it adds time zone. Do not use this method inResultSet.getString(int)- Parameters:
localDateTime- The local date to format as a String- Returns:
- The formatted local date
-
appendDate
private static void appendDate(java.lang.StringBuilder sb, java.time.LocalDate localDate)
-
appendTime
private static void appendTime(java.lang.StringBuilder sb, java.time.LocalTime localTime)
-
appendTimeZone
private static void appendTimeZone(java.lang.StringBuilder sb, java.time.ZoneOffset offset)
-
appendEra
private static void appendEra(java.lang.StringBuilder sb, java.time.LocalDate localDate)
-
skipWhitespace
private static int skipWhitespace(byte[] bytes, int start)
-
firstNonDigit
private static int firstNonDigit(byte[] bytes, int start)
-
number
private static int number(byte[] bytes, int start, int end)
-
toDateBin
public java.sql.Date toDateBin(java.util.TimeZone tz, byte[] bytes) throws PSQLExceptionReturns the SQL Date object matching the given bytes withOid.DATE.- Parameters:
tz- The timezone used.bytes- The binary encoded date value.- Returns:
- The parsed date object.
- Throws:
PSQLException- If binary format could not be parsed.
-
getDefaultTz
private java.util.TimeZone getDefaultTz()
-
hasFastDefaultTimeZone
public boolean hasFastDefaultTimeZone()
-
toTimeBin
public java.sql.Time toTimeBin(java.util.TimeZone tz, byte[] bytes) throws PSQLExceptionReturns the SQL Time object matching the given bytes withOid.TIMEorOid.TIMETZ.- Parameters:
tz- The timezone used when received data isOid.TIME, ignored if data already containsOid.TIMETZ.bytes- The binary encoded time value.- Returns:
- The parsed time object.
- Throws:
PSQLException- If binary format could not be parsed.
-
toLocalTimeBin
public java.time.LocalTime toLocalTimeBin(byte[] bytes) throws PSQLExceptionReturns the SQL Time object matching the given bytes withOid.TIME.- Parameters:
bytes- The binary encoded time value.- Returns:
- The parsed time object.
- Throws:
PSQLException- If binary format could not be parsed.
-
toTimestampBin
public java.sql.Timestamp toTimestampBin(java.util.TimeZone tz, byte[] bytes, boolean timestamptz) throws PSQLExceptionReturns the SQL Timestamp object matching the given bytes withOid.TIMESTAMPorOid.TIMESTAMPTZ.- Parameters:
tz- The timezone used when received data isOid.TIMESTAMP, ignored if data already containsOid.TIMESTAMPTZ.bytes- The binary encoded timestamp value.timestamptz- True if the binary is in GMT.- Returns:
- The parsed timestamp object.
- Throws:
PSQLException- If binary format could not be parsed.
-
toParsedTimestampBinPlain
private TimestampUtils.ParsedBinaryTimestamp toParsedTimestampBinPlain(byte[] bytes) throws PSQLException
- Throws:
PSQLException
-
toParsedTimestampBin
private TimestampUtils.ParsedBinaryTimestamp toParsedTimestampBin(java.util.TimeZone tz, byte[] bytes, boolean timestamptz) throws PSQLException
- Throws:
PSQLException
-
toProlepticParsedTimestampBin
private TimestampUtils.ParsedBinaryTimestamp toProlepticParsedTimestampBin(byte[] bytes) throws PSQLException
- Throws:
PSQLException
-
toLocalDateTimeBin
public java.time.LocalDateTime toLocalDateTimeBin(byte[] bytes) throws PSQLExceptionReturns the local date time object matching the given bytes withOid.TIMESTAMPorOid.TIMESTAMPTZ.- Parameters:
bytes- The binary encoded local date time value.- Returns:
- The parsed local date time object.
- Throws:
PSQLException- If binary format could not be parsed.
-
toLocalDateBin
public java.time.LocalDate toLocalDateBin(byte[] bytes) throws PSQLExceptionReturns the local date time object matching the given bytes withOid.DATEorOid.TIMESTAMP.- Parameters:
bytes- The binary encoded local date value.- Returns:
- The parsed local date object.
- Throws:
PSQLException- If binary format could not be parsed.
-
guessTimestamp
private long guessTimestamp(long millis, java.util.TimeZone tz)Given a UTC timestampmillisfinds another point in time that is rendered in given time zonetzexactly as "millis in UTC".For instance, given 7 Jan 16:00 UTC and tz=GMT+02:00 it returns 7 Jan 14:00 UTC == 7 Jan 16:00 GMT+02:00 Note that is not trivial for timestamps near DST change. For such cases, we rely on
Calendarto figure out the proper timestamp.- Parameters:
millis- source timestamptz- desired time zone- Returns:
- timestamp that would be rendered in
tzlikemillisin UTC
-
isSimpleTimeZone
private static boolean isSimpleTimeZone(java.lang.String id)
-
convertToDate
public java.sql.Date convertToDate(long millis, java.util.TimeZone tz)Extracts the date part from a timestamp.- Parameters:
millis- The timestamp from which to extract the date.tz- The time zone of the date.- Returns:
- The extracted date.
-
convertToTime
public java.sql.Time convertToTime(long millis, java.util.TimeZone tz)Extracts the time part from a timestamp. This method ensures the date part of output timestamp looks like 1970-01-01 in given timezone.- Parameters:
millis- The timestamp from which to extract the time.tz- timezone to use.- Returns:
- The extracted time.
-
timeToString
public java.lang.String timeToString(java.util.Date time, boolean withTimeZone)Returns the given time value as String matching what the current postgresql server would send in text mode.- Parameters:
time- time valuewithTimeZone- whether timezone should be added- Returns:
- given time value as String
-
toJavaSecs
private static long toJavaSecs(long secs)
Converts the given postgresql seconds to java seconds. Reverse engineered by inserting varying dates to postgresql and tuning the formula until the java dates matched. SeetoPgSecs(long)for the reverse operation.- Parameters:
secs- Postgresql seconds.- Returns:
- Java seconds.
-
toPgSecs
private static long toPgSecs(long secs)
Converts the given java seconds to postgresql seconds. SeetoJavaSecs(long)for the reverse operation. The conversion is valid for any year 100 BC onwards.- Parameters:
secs- Postgresql seconds.- Returns:
- Java seconds.
-
toBinDate
public void toBinDate(java.util.TimeZone tz, byte[] bytes, java.sql.Date value) throws PSQLExceptionConverts the SQL Date to binary representation forOid.DATE.- Parameters:
tz- The timezone used.bytes- The binary encoded date value.value- value- Throws:
PSQLException- If binary format could not be parsed.
-
parseBackendTimeZone
public static java.util.TimeZone parseBackendTimeZone(java.lang.String timeZone)
Converts backend's TimeZone parameter to java format. Notable difference: backend's gmt-3 is GMT+03 in Java.- Parameters:
timeZone- time zone to use- Returns:
- java TimeZone
-
floorDiv
private static long floorDiv(long x, long y)
-
floorMod
private static long floorMod(long x, long y)
-
-