to_epoch
Use the to_epoch() function to convert a timestamp value into the Unix epoch timestamp format.
Syntax
to_epoch (<timestamp>, <time unit>)Parameters
timestamp
timestamp
Yes
The timestamp value to convert. This must be a TIMESTAMP object, not a string.
time unit
string
Yes
The granularity of the returned integer value. Supported values are SECONDS, MILLIS, or MICROS.
Returns
The to_epoch() function returns an integer representing the Unix epoch timestamp.
Usage notes
The first parameter must be a
TIMESTAMPobject. If you have a string representation of a timestamp, you must first convert it to aTIMESTAMPobject using functions likeparse_epoch()orto_timestamp().The
<time unit>parameter specifies the granularity of the returned integer value (seconds, milliseconds, or microseconds).If no
<time unit>is configured,SECONDSis used as the default.
Examples
Example 1: Converting _time (timestamp field) to epoch in seconds
Goal: Convert the _time field, which is already a TIMESTAMP object, to its Unix epoch representation in seconds.
XQL code:
Explanation: The _time field is converted to an epoch integer in seconds using the "SECONDS" unit.
Output:
101
2023-10-26 10:00:00 UTC
1698304800
102
2023-10-26 10:05:30 UTC
1698305130
103
2023-10-26 10:15:15 UTC
1698305715
Example 2: Converting _time (timestamp field) to epoch in milliseconds
Goal: Convert the _time field to its Unix epoch representation in milliseconds.
XQL code:
Explanation: The _time field is converted to an epoch integer in milliseconds using the "MILLIS" unit.
Output:
101
2023-10-26 10:00:00 UTC
1698304800000
102
2023-10-26 10:05:30 UTC
1698305130000
103
2023-10-26 10:15:15 UTC
1698305715000
Example 3: Converting _time (timestamp field) to epoch in microseconds
Goal: Convert the _time field to its Unix epoch representation in microseconds.
XQL code:
Explanation: The _time field is converted to an epoch integer in microseconds using the "MICROS" unit.
Output:
101
2023-10-26 10:00:00 UTC
1698304800000000
102
2023-10-26 10:05:30 UTC
1698305130000000
103
2023-10-26 10:15:15 UTC
1698305715000000
Example 4: Converting a string timestamp to epoch
Goal: Convert a string that represents a timestamp into epoch time by first creating a TIMESTAMP object.
XQL code:
Explanation: The query first uses parse_timestamp() to create a TIMESTAMP object from a string literal. Then, to_epoch() converts that object into epoch seconds.
Output:
101
"2023-10-26 10:00:00 UTC"
2023-10-26 10:00:00 UTC
1698304800
102
"2023-10-26 10:00:00 UTC"
2023-10-26 10:00:00 UTC
1698304800
103
"2023-10-26 10:00:00 UTC"
2023-10-26 10:00:00 UTC
1698304800
Related articles
Stages:
alterFunctions:
parse_epoch,to_timestamp
Last updated
Was this helpful?
