unix_timestamp function

Returns the UNIX timestamp of current or specified time.

Syntax

unix_timestamp([expr [, fmt] ] )

Arguments

  • expr: An optional DATE, TIMESTAMP, or a STRING expression in a valid datetime format.

  • fmt: An optional STRING expression specifying the format if expr is a STRING.

Returns

A BIGINT.

If no argument is provided the default is the current timestamp. fmt is ignored if expr is a DATE or TIMESTAMP. If expr is a STRING fmt is used to translate the string to a TIMESTAMP before computing the unix timestamp.

The default fmt value is 'yyyy-MM-dd HH:mm:ss'.

See Datetime patterns for valid date and time format patterns.

If fmt or expr are invalid the function raises an error.

Note

If spark.sql.ansi.enabled is false the function returns NULL instead of an error for malformed timestamps.

Examples

> SELECT unix_timestamp();
 1476884637
> SELECT unix_timestamp('2016-04-08', 'yyyy-MM-dd');
 1460041200