TO_POSIXTIME (SQL)
Synopsis
TO_POSIXTIME(date_string[,format])
Description
The TO_POSIXTIME function converts date and time strings in various formats to a %PosixTime timestamp, with data type %Library.PosixTimeOpens in a new tab. TO_POSIXTIME returns a %PosixTime timestamp as a calculated value based on the number of elapsed seconds from the arbitrary starting point of 1970-01-01 00:00:00, encoded as a 64-bit signed integer. The actual number of elapsed seconds (and fractional seconds) from this date is the Unix®timestamp, a numeric value. InterSystems IRIS encodes the Unix® timestamp to generate the %PosixTime timestamp. Because a %PosixTime timestamp value is encoded, 1970-01-01 00:00:00 is represented as 1152921504606846976. Dates prior to 1970-01-01 00:00:00 have a negative integer value. Refer to the %PosixTime data type for further details.
TO_POSIXTIME does not convert timezones; a local date and time is converted to a local %PosixTime timestamp; a UTC date and time is converted to a UTC %PosixTime timestamp.
The earliest date supported by %PosixTime is 0001-01-01 00:00:00, which has a logical value of -6979664624441081856. The last date supported is 9999-12-31 23:59:59.999999, which has a logical value of 1406323805406846975. These limits correspond to the ODBC date format display limits. These values can be further limited using the %Library.PosixTimeOpens in a new tab MINVAL and MAXVAL parameters. You can use the IsValid()Opens in a new tab method to determine if a numeric value is a valid %PosixTime value.
A %PosixTime value always encodes a precision of 6 decimal digits of fractional seconds. A date_string with fewer digits of precision is zero-padded to 6 digits before %PosixTime conversion; a date_string with more than 6 digits of precision is truncated to 6 digits before %PosixTime conversion.
If date_string omits components of the timestamp, TO_POSIXTIME supplies the missing components. If both date_string and format omit the year, yyyy defaults to the current year; if only date_string omits the year, it defaults to 00, which is expanded to a four-digit year according to the year format element. If a day or month value is omitted, dd defaults to 01; mm-dd defaults to 01-01. A missing time component defaults to 00. Fractional seconds are supported, but must be explicitly specified; no fractional seconds are provided by default.
TO_POSIXTIME supports conversion of two-digit years to four digits. TO_POSIXTIME supports conversion of 12-hour clock time to 24-hour clock time. It provides range validation of date and time element values, including leap year validation. Range validation violations generate an SQLCODE -400 error.
This function can also be invoked from ObjectScript using the TOPOSIXTIME()Opens in a new tab method call:
$SYSTEM.SQL.Functions.TOPOSIXTIME(date_string,format)
The TO_POSIXTIME function can be used in data definition when supplying a default value to a field. For example:
CREATE TABLE mytest
(ID NUMBER(12,0) NOT NULL,
End_Year DATE DEFAULT TO_POSIXTIME('12-31-2018','MM-DD-YYYY') NOT NULL)
TO_POSIXTIME can be used with the CREATE TABLE or ALTER TABLE ADD COLUMN statements. Only a literal value for date_string can be used in this context. For further details, refer to the CREATE TABLE command.
%PosixTime Representation
%PosixTime encodes 6 digits of precision for fractional seconds, regardless of the precision of the date_string. The ODBC and Display modes truncate trailing zeros of precision.
-
Logical Mode: an encoded 64-bit (19 characters) signed integer.
-
ODBC Mode: YYYY–MM–DD HH:MM:SS.FFFFFF. Refer to the %PosixTime LogicalToOdbc()Opens in a new tab method.
-
Display Mode: uses the default date/time formats (dformat -1 and tformat -1) for the current locale, as described in $ZDATETIME. Refer to the %PosixTime LogicalToDisplay()Opens in a new tab method.