com.mysql.cj.util

## Class TimeUtil

- java.lang.Object
- - com.mysql.cj.util.TimeUtil

    public class TimeUtil
    extends Object

Time zone conversion routines and other time related methods

### Field Summary

Fields
`static DateTimeFormatter` `DATE_FORMATTER`
`static Pattern` `DATE_LITERAL_NO_DELIMITERS`
`static Pattern` `DATE_LITERAL_WITH_DELIMITERS`
`static DateTimeFormatter` `DATETIME_FORMATTER_NO_FRACT_NO_OFFSET`
`static DateTimeFormatter` `DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET`
`static DateTimeFormatter` `DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET`
`static DateTimeFormatter` `DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET`
`static DateTimeFormatter` `DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET`
`static DateTimeFormatter` `DATETIME_FORMATTER_WITH_OPTIONAL_MICROS`
`static Pattern` `DATETIME_LITERAL_SHORT12`
`static Pattern` `DATETIME_LITERAL_SHORT14`
`static Pattern` `DATETIME_LITERAL_WITH_DELIMITERS`
`static LocalDate` `DEFAULT_DATE`
`static LocalTime` `DEFAULT_TIME`
`static Pattern` `DURATION_LITERAL_NO_DAYS`
`static Pattern` `DURATION_LITERAL_WITH_DAYS`
`(package private) static TimeZone` `GMT_TIMEZONE`
`protected static Method` `systemNanoTimeMethod`
`static DateTimeFormatter` `TIME_FORMATTER_NO_FRACT_NO_OFFSET`
`static DateTimeFormatter` `TIME_FORMATTER_NO_FRACT_WITH_OFFSET`
`static DateTimeFormatter` `TIME_FORMATTER_WITH_NANOS_NO_OFFSET`
`static DateTimeFormatter` `TIME_FORMATTER_WITH_NANOS_WITH_OFFSET`
`static DateTimeFormatter` `TIME_FORMATTER_WITH_OPTIONAL_MICROS`
`static Pattern` `TIME_LITERAL_SHORT2`
`static Pattern` `TIME_LITERAL_SHORT4`
`static Pattern` `TIME_LITERAL_SHORT6`
`static Pattern` `TIME_LITERAL_WITH_DELIMITERS`
`private static String` `TIME_ZONE_MAPPINGS_RESOURCE`
`private static Properties` `timeZoneMappings`

### Constructor Summary

Constructors
Constructor and Description
`TimeUtil()`

### Method Summary

All Methods Static Methods Concrete Methods
`static Duration` `adjustNanosPrecision(Duration x, int fsp, boolean serverRoundFracSecs)`
`static LocalDateTime` `adjustNanosPrecision(LocalDateTime x, int fsp, boolean serverRoundFracSecs)` Return a new LocalDateTime object which value is adjusted according to known DATE, DATETIME or TIMESTAMP field precision.
`static LocalTime` `adjustNanosPrecision(LocalTime x, int fsp, boolean serverRoundFracSecs)`
`static Timestamp` `adjustNanosPrecision(Timestamp ts, int fsp, boolean serverRoundFracSecs)` Return a new Timestamp object which value is adjusted according to known DATE, DATETIME or TIMESTAMP field precision.
`static String` `formatNanos(int nanos, int fsp)` Return a string representation of a fractional seconds part.
`static String` `formatNanos(int nanos, int fsp, boolean truncateTrailingZeros)` Return a string representation of a fractional seconds part.
`private static String` `getCanonicalDate(String s)`
`private static String` `getCanonicalDateTime(String s)`
`private static String` `getCanonicalTime(String s)`
`static String` `getCanonicalTimeZone(String timezoneStr, ExceptionInterceptor exceptionInterceptor)` Returns the 'official' Java timezone name for the given timezone
`static long` `getCurrentTimeNanosOrMillis()`
`static String` `getDurationString(Duration x)`
`static SimpleDateFormat` `getSimpleDateFormat(SimpleDateFormat cachedSimpleDateFormat, String pattern, TimeZone tz)` Get SimpleDateFormat with a default Calendar which TimeZone is replaced with the provided one.
`static SimpleDateFormat` `getSimpleDateFormat(String pattern, Calendar cal)` Get SimpleDateFormat where a default Calendar is replaced with a clone of the provided one.
`static Boolean` `hasFractionalSeconds(Time t)`
`private static void` `loadTimeZoneMappings(ExceptionInterceptor exceptionInterceptor)` Loads a properties file that contains all kinds of time zone mappings.
`static boolean` `nanoTimeAvailable()`
`static Object` `parseToDateTimeObject(String s, MysqlType targetMysqlType)`
`static Time` `truncateFractionalSeconds(Time time)`
`static Timestamp` `truncateFractionalSeconds(Timestamp timestamp)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### GMT_TIMEZONE

    static final TimeZone GMT_TIMEZONE

#### DEFAULT_DATE

    public static final LocalDate DEFAULT_DATE

#### DEFAULT_TIME

    public static final LocalTime DEFAULT_TIME

#### DATE_FORMATTER

    public static final DateTimeFormatter DATE_FORMATTER

#### TIME_FORMATTER_NO_FRACT_NO_OFFSET

    public static final DateTimeFormatter TIME_FORMATTER_NO_FRACT_NO_OFFSET

#### TIME_FORMATTER_WITH_NANOS_NO_OFFSET

    public static final DateTimeFormatter TIME_FORMATTER_WITH_NANOS_NO_OFFSET

#### TIME_FORMATTER_NO_FRACT_WITH_OFFSET

    public static final DateTimeFormatter TIME_FORMATTER_NO_FRACT_WITH_OFFSET

#### TIME_FORMATTER_WITH_NANOS_WITH_OFFSET

    public static final DateTimeFormatter TIME_FORMATTER_WITH_NANOS_WITH_OFFSET

#### TIME_FORMATTER_WITH_OPTIONAL_MICROS

    public static final DateTimeFormatter TIME_FORMATTER_WITH_OPTIONAL_MICROS

#### DATETIME_FORMATTER_NO_FRACT_NO_OFFSET

    public static final DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_NO_OFFSET

#### DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET

    public static final DateTimeFormatter DATETIME_FORMATTER_WITH_MILLIS_NO_OFFSET

#### DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET

    public static final DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_NO_OFFSET

#### DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET

    public static final DateTimeFormatter DATETIME_FORMATTER_NO_FRACT_WITH_OFFSET

#### DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET

    public static final DateTimeFormatter DATETIME_FORMATTER_WITH_NANOS_WITH_OFFSET

#### DATETIME_FORMATTER_WITH_OPTIONAL_MICROS

    public static final DateTimeFormatter DATETIME_FORMATTER_WITH_OPTIONAL_MICROS

#### DATE_LITERAL_WITH_DELIMITERS

    public static final Pattern DATE_LITERAL_WITH_DELIMITERS

#### DATE_LITERAL_NO_DELIMITERS

    public static final Pattern DATE_LITERAL_NO_DELIMITERS

#### TIME_LITERAL_WITH_DELIMITERS

    public static final Pattern TIME_LITERAL_WITH_DELIMITERS

#### TIME_LITERAL_SHORT6

    public static final Pattern TIME_LITERAL_SHORT6

#### TIME_LITERAL_SHORT4

    public static final Pattern TIME_LITERAL_SHORT4

#### TIME_LITERAL_SHORT2

    public static final Pattern TIME_LITERAL_SHORT2

#### DATETIME_LITERAL_WITH_DELIMITERS

    public static final Pattern DATETIME_LITERAL_WITH_DELIMITERS

#### DATETIME_LITERAL_SHORT14

    public static final Pattern DATETIME_LITERAL_SHORT14

#### DATETIME_LITERAL_SHORT12

    public static final Pattern DATETIME_LITERAL_SHORT12

#### DURATION_LITERAL_WITH_DAYS

    public static final Pattern DURATION_LITERAL_WITH_DAYS

#### DURATION_LITERAL_NO_DAYS

    public static final Pattern DURATION_LITERAL_NO_DAYS

#### TIME_ZONE_MAPPINGS_RESOURCE

    private static final String TIME_ZONE_MAPPINGS_RESOURCE

See Also:
  Constant Field Values

#### timeZoneMappings

    private static Properties timeZoneMappings

#### systemNanoTimeMethod

    protected static final Method systemNanoTimeMethod

### Constructor Detail

#### TimeUtil

    public TimeUtil()

### Method Detail

#### nanoTimeAvailable

    public static boolean nanoTimeAvailable()

#### getCurrentTimeNanosOrMillis

    public static long getCurrentTimeNanosOrMillis()

#### getCanonicalTimeZone

    public static String getCanonicalTimeZone(String timezoneStr,
                                              ExceptionInterceptor exceptionInterceptor)

Returns the 'official' Java timezone name for the given timezone

Parameters:
  `timezoneStr` - the 'common' timezone name
  `exceptionInterceptor` - exception interceptor
Returns:
  the Java timezone name for the given timezone

#### adjustNanosPrecision

    public static Timestamp adjustNanosPrecision(Timestamp ts,
                                                 int fsp,
                                                 boolean serverRoundFracSecs)

Return a new Timestamp object which value is adjusted according to known DATE, DATETIME or TIMESTAMP field precision.

Parameters:
  `ts` - an original Timestamp object, not modified by this method
  `fsp` - value in the range from 0 to 6 specifying fractional seconds precision
  `serverRoundFracSecs` - Flag indicating whether rounding or truncation occurs on server when inserting a TIME, DATE, or TIMESTAMP value with a fractional seconds part into a column having the same type but fewer fractional digits: true means rounding, false means truncation. The proper value should be detected by analyzing sql_mode server variable for TIME_TRUNCATE_FRACTIONAL presence.
Returns:
  A new Timestamp object cloned from the original one and then rounded or truncated according to required fsp value

#### adjustNanosPrecision

    public static LocalDateTime adjustNanosPrecision(LocalDateTime x,
                                                     int fsp,
                                                     boolean serverRoundFracSecs)

Return a new LocalDateTime object which value is adjusted according to known DATE, DATETIME or TIMESTAMP field precision.

Parameters:
  `x` - an original LocalDateTime object, not modified by this method
  `fsp` - value in the range from 0 to 6 specifying fractional seconds precision
  `serverRoundFracSecs` - Flag indicating whether rounding or truncation occurs on server when inserting a TIME, DATE, or TIMESTAMP value with a fractional seconds part into a column having the same type but fewer fractional digits: true means rounding, false means truncation. The proper value should be detected by analyzing sql_mode server variable for TIME_TRUNCATE_FRACTIONAL presence.
Returns:
  A new LocalDateTime object cloned from the original one and then rounded or truncated according to required fsp value

#### adjustNanosPrecision

    public static LocalTime adjustNanosPrecision(LocalTime x,
                                                 int fsp,
                                                 boolean serverRoundFracSecs)

#### adjustNanosPrecision

    public static Duration adjustNanosPrecision(Duration x,
                                                int fsp,
                                                boolean serverRoundFracSecs)

#### formatNanos

    public static String formatNanos(int nanos,
                                     int fsp)

Return a string representation of a fractional seconds part. This method assumes that all Timestamp adjustments are already done before, thus no rounding is needed, only a proper "0" padding to be done.

Parameters:
  `nanos` - fractional seconds value
  `fsp` - required fractional part length
Returns:
  fractional seconds part as a string

#### formatNanos

    public static String formatNanos(int nanos,
                                     int fsp,
                                     boolean truncateTrailingZeros)

Return a string representation of a fractional seconds part. This method assumes that all Timestamp adjustments are already done before, thus no rounding is needed, only a proper "0" padding to be done.

Parameters:
  `nanos` - fractional seconds value
  `fsp` - required fractional part length
  `truncateTrailingZeros` - whether to remove trailing zero characters in a fractional part after formatting
Returns:
  fractional seconds part as a string

#### loadTimeZoneMappings

    private static void loadTimeZoneMappings(ExceptionInterceptor exceptionInterceptor)

Loads a properties file that contains all kinds of time zone mappings.

Parameters:
  `exceptionInterceptor` - exception interceptor

#### truncateFractionalSeconds

    public static Timestamp truncateFractionalSeconds(Timestamp timestamp)

#### truncateFractionalSeconds

    public static Time truncateFractionalSeconds(Time time)

#### hasFractionalSeconds

    public static Boolean hasFractionalSeconds(Time t)

#### getSimpleDateFormat

    public static SimpleDateFormat getSimpleDateFormat(SimpleDateFormat cachedSimpleDateFormat,
                                                       String pattern,
                                                       TimeZone tz)

Get SimpleDateFormat with a default Calendar which TimeZone is replaced with the provided one.

Note: The SimpleDateFormat object returned by this method contains a default Calendar with an altered TimeZone. It's safe to cache it between this method calls because the Calendar object itself is not altered.

Parameters:
  `cachedSimpleDateFormat` - existing SimpleDateFormat to use instead of creating a new one
  `pattern` - format pattern
  `tz` - `TimeZone` object replacing the default one
Returns:
  `SimpleDateFormat` object

#### getSimpleDateFormat

    public static SimpleDateFormat getSimpleDateFormat(String pattern,
                                                       Calendar cal)

Get SimpleDateFormat where a default Calendar is replaced with a clone of the provided one.

Note: Don't cache the SimpleDateFormat object returned by this method. Other methods could rely on assumption that the cached SimpleDateFormat has a default Calendar and that it is safe to change only it's time zone (see `getSimpleDateFormat(SimpleDateFormat, String, TimeZone)`.

Parameters:
  `pattern` - format pattern
  `cal` - `Calendar` object which clone is replacing the default Calendar
Returns:
  `SimpleDateFormat` object

#### parseToDateTimeObject

    public static Object parseToDateTimeObject(String s,
                                               MysqlType targetMysqlType)

#### getCanonicalDate

    private static String getCanonicalDate(String s)

#### getCanonicalTime

    private static String getCanonicalTime(String s)

#### getCanonicalDateTime

    private static String getCanonicalDateTime(String s)

#### getDurationString

    public static String getDurationString(Duration x)

Copyright © 2016, 2023, Oracle and/or its affiliates.