timestamp_diff
Use the timestamp_diff() function to calculate the numerical difference between two timestamps in a specified unit.
Syntax
timestamp_diff (<timestamp1>, <timestamp2>, <part>)Parameters
timestamp1
timestamp
Yes
The first timestamp object in the comparison.
timestamp2
timestamp
Yes
The second timestamp object, which is subtracted from the first.
part
string
Yes
The unit in which the difference is expressed. Supported values are DAY, HOUR, MINUTE, SECOND, MILLISECOND, and MICROSECOND.
Returns
The timestamp_diff() function returns a numerical value (integer or float) representing the difference between the two timestamps in the specified unit.
Usage notes
The function calculates the difference by subtracting
timestamp2fromtimestamp1.If
timestamp1is chronologically greater (later) thantimestamp2, the function returns a positive value.If
timestamp1is chronologically less (earlier) thantimestamp2, the function returns a negative value.If the calculated difference in the specified unit results in a fractional value between 0 and 1 (exclusive of 1, inclusive of 0), the function returns 0.
Supported values for the
partparameter include:DAY,HOUR,MINUTE,SECOND,MILLISECOND, andMICROSECOND.
Examples
Example 1: Difference in seconds between current_time() and_time
Goal: Calculate the difference in seconds between the current query execution time and the event's timestamp.
XQL code:
Explanation: The query computes the number of full seconds that have passed between the event's _time and the current_time() of the query execution.
Output:
101
Oct 26th 2023 10:00:00
Oct 26th 2023 12:00:00
7200
102
Oct 26th 2023 10:05:30
Oct 26th 2023 12:00:00
6870
103
Oct 26th 2023 10:15:15
Oct 26th 2023 12:00:00
6285
Example 2: Difference in minutes between _time and a static timestamp
Goal: Calculate the difference in minutes between the event timestamp and a specific static timestamp, demonstrating truncation.
XQL code:
Explanation: The query calculates the difference in minutes. Note that partial minutes (values between 0 and 1) are truncated to 0.
Output:
101
Oct 26th 2023 10:00:00
Oct 26th 2023 10:05:00
-5
102
Oct 26th 2023 10:05:30
Oct 26th 2023 10:05:00
0
103
Oct 26th 2023 10:15:15
Oct 26th 2023 10:05:00
10
Example 3: Difference in hours, yielding a negative result
Goal: Calculate the difference in hours where the first timestamp is earlier than the second, resulting in a negative value.
XQL code:
Explanation: The query demonstrates that when timestamp1 is earlier than timestamp2, the result is negative. Calculations are truncated to full hours.
Output:
101
Oct 26th 2023 10:00:00
Oct 27th 2023 10:00:00
-24
102
Oct 26th 2023 10:05:30
Oct 27th 2023 10:00:00
-23
103
Oct 26th 2023 10:15:15
Oct 27th 2023 10:00:00
-23
Example 4: Using timestamp_diff() in a filter stage
Goal: Filter events based on a calculated time difference relative to the current time.
XQL code:
Explanation: This query filters for events that occurred more than 1 hour prior to the current execution time.
Output:
101
Oct 26th 2023 10:00:00
102
Oct 26th 2023 10:05:30
103
Oct 26th 2023 10:15:15
Example 5: Difference in milliseconds between _time and a closely past static timestamp
Goal: Calculate a fine-grained difference in milliseconds between two close timestamps.
XQL code:
Explanation: The query calculates the exact difference in milliseconds, useful for precise time measurements.
Output:
101
Oct 26th 2023 10:00:00
Oct 26th 2023 10:00:00
0
102
Oct 26th 2023 10:05:30
Oct 26th 2023 10:00:00
330000
103
Oct 26th 2023 10:15:15
Oct 26th 2023 10:00:00
915000
Related articles
Functions:
current_time,parse_timestamp
Last updated
Was this helpful?
