gsql_ts_to_epoch_seconds_legacy()

Converts a timestamp in canonical string format to Unix epoch time (seconds since Jan 1, 1970). If the timestamp is invalid or earlier than 1970-01-01 00:00:00, the function returns a user-specified value (0 by default), ensuring backward compatibility and preventing exceptions.

We also have another timestamp function, gsql_ts_to_epoch_seconds(), which converts a timestamp to Unix epoch time (seconds since Jan 1st, 1970) and throws an exception instead of returning a user-specified value, if the input is not valid. For broader date range support, consider gsql_ts_to_epoch_seconds_signed().

Syntax

gsql_ts_to_epoch_seconds_legacy(timestamp, defaultTimestamp = 0)

Parameters

timestamp

Required. The timestamp to convert to Unix epoch time. The timestamp parameter should be in one of the following formats:

  • "%Y-%m-%d %H:%M:%S"

  • "%Y/%m/%d %H:%M:%S"

  • "%Y-%m-%dT%H:%M:%S.000z"

    • Text after the dot . is ignored

defaultTimestamp

Optional. If the timestamp is invalid (e.g., before Jan 1st, 1970, or an invalid date format), it returns a user-specified value. The default is 0.

Return type

uint64_t

  • Returns the epoch seconds for valid timestamps between 1970-01-01 00:00:00 and 9999-12-31 23:59:59.

  • Returns defaultTimestamp for abnormal times (pre-1970 or invalid).

Example

// Example with a valid date after 1970
gsql_ts_to_epoch_seconds_legacy("2022-06-30 22:08:28") -> 1656652105

// Example with a date before 1970 (returns default 0)
gsql_ts_to_epoch_seconds_legacy("1969-12-31 23:59:59") -> 0

// Example with a date before 1970 and a custom default timestamp
gsql_ts_to_epoch_seconds_legacy("1969-12-31 23:59:59", 999) -> 999

// Example with an invalid date (returns default 0) gsql_ts_to_epoch_seconds_legacy("2022-02-30 00:00:00") -> 0

Use this function if you want to maintain legacy behavior (prior to the availability of gsql_ts_to_epoch_signed), avoid exceptions for invalid or pre-1970 dates, and ensure unsigned integer output.