UNIX_TIMESTAMP
Description
The UNIX_TIMESTAMP
function is used to obtain the UNIX timestamp of the current time or a specified time, which is the total number of seconds from 00:00:00 UTC on January 1, 1970, to the specified time. If no parameters are specified, it returns the UNIX timestamp of the current time by default.
Syntax
Parameter Description
expr
: Can be of type date, timestamp_ltz, or string, representing the time to be converted to a UNIX timestamp. If this parameter is not specified, the current time is used by default.fmt
: String type, representing the format of the input time. If this parameter is not specified, the system's default time format will be used for parsing.
Return Result
Returns a value of type bigint, representing the UNIX timestamp.
Usage Example
- Get the UNIX timestamp of the current time:
- Convert string type time to UNIX timestamp:
- Convert timestamp type time to UNIX timestamp:
- Convert date type time to UNIX timestamp:
Precautions
- When the input time format is incorrect or unrecognizable, the function will return NULL.
- If the specified time exceeds the time range supported by the system, the function will return NULL.
- When using this function, it is recommended to explicitly specify the format of the input time to avoid parsing errors caused by different system default time formats.