TIME stores the time of day in UTC.
The TIMETZ data type stores a time of day with a time zone offset from UTC.
Variants
TIME has two variants:
-
TIME, which presents allTIMEvalues in UTC. -
TIMETZ, which convertsTIMEvalues with a specified time zone offset from UTC. Ordering forTIMETZis done in terms of epoch. Time zones with lesser values are ranked higher if times are equal. For example,'2:00-1' > '2:00+0'and'12:00-1' > '1:00+0'. Like PostgreSQL, we implement theTIMETZvariant for . We also implement theTIMETZvariant for compatibility with ORMs, like .
TIMETZ into a TIME at a specified timezone, or to convert a TIME into a TIMETZ at a specified timezone.
Aliases
In CockroachDB, the following are aliases:| Alias | Long Version |
|---|---|
TIME | TIME WITHOUT TIME ZONE |
TIMETZ | TIME WITH TIME ZONE |
Syntax
TIME
A constant value of type TIME can be expressed using an , or a string literal type TIME or type TIME. When it is unambiguous, a simple unannotated can also be automatically interpreted as type TIME.
The string format for TIME is HH:MM:SS.SSSSSS. For example: TIME '05:40:00.000001'. The fractional portion is optional and is rounded to microseconds (i.e., six digits after the decimal) for compatibility with the PostgreSQL wire protocol.
A date of
0000-01-01 is displayed for all TIME/TIMETZ values, but is not stored in the database. To print without a date, you can cast the type to a STRING.A time zone offset of +00:00 is also displayed for all TIME and values, but is not stored in the database.TIMETZ
To express a TIMETZ value with a time zone offset from UTC, you can add an offset to a TIME value. For example, TIMETZ '10:10:10.555555-05:00' offsets from UTC by -5.
If no time zone is specified for a TIMETZ value, the timezone is used. For example, if you for a session using SET TIME ZONE -2, and you define the TIMETZ as TIMETZ '10:10:10.55', the value will be displayed with an offset of -2 from UTC.
TIMETZ is not affected by session-scoped offsets (unlike ). Time zone offsets only apply to values inserted after the offset has been set, and do not affect existing TIMETZ values, or TIMETZ values with a time zone offset specified.
Size
ATIME column supports values up to 8 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata.
A TIMETZ column supports values up to 12 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata.
Precision
CockroachDB supports precision levels from 0 (seconds) to 6 (microseconds) forTIME/TIMETZ values. Precision in time values specifies the number of fractional digits retained in the seconds field. For example, specifying a TIME value as TIME(3) truncates the time precision to milliseconds. By default, TIME/TIMETZ values have a precision of 6 (microseconds).
You can use an statement to change the precision level of a TIME-typed column. If there is already a non-default precision level specified for the column, the precision level can only be changed to an equal or greater precision level. For an example, see Create a table with a TIME-typed column, with precision.
Examples
Create a table with a TIME-typed column
The SQL shell displays the date and time zone due to the Go SQL driver it uses. Other client drivers may behave similarly. In such cases, however, the date and time zone are not relevant and are not stored in the database.
TIME values:
Create a table with a TIME-typed column, with precision
If a non-default precision level has already been specified, you cannot change the precision to a lower level.
time_val column, which is of type TIME(5), cannot be changed to a precision level below 5:
Supported casting & conversion
TIME/TIMETZ values can be to any of the following data types:
| Type | Details |
|---|---|
INTERVAL | Converts to the span of time since midnight (00:00) |
STRING | Converts to format 'HH:MM:SS.SSSSSS' (microsecond precision) |
CockroachDB displays
TIME '24:00:00' and TIMETZ '24:00:00' as 0000-01-01 00:00:00. To display the proper stored value (24:00:00), you can .
