Splunk Search

How to subtract two time fields?

cotyp
Path Finder

How would I go about subtracting EndTime from BeginTime?

alt text

0 Karma
1 Solution

493669
Super Champion

Try this run anywhere search:

|makeresults|eval EndTime="2/14/2018 9:28:19", BeginTime="2/6/2018 14:53:45"|eval EndTime=strptime(EndTime,"%m/%d/%Y %H:%M:%S"), BeginTime=strptime(BeginTime,"%m/%d/%Y %H:%M:%S")|eval days=round((EndTime-BeginTime)/86400)

View solution in original post

adonio
Ultra Champion

hello there,
try this:

... your search ...
|eval end_time_epoch = strptime(EndTime, "%m/%d/%Y %H:%M:%S")
|eval begin_time_epoch = strptime(BeginTime, "%m/%d/%Y %H:%M:%S")
| eval duration = end_time_epoch - begin_time_epoch

hope it helps

cotyp
Path Finder

How would I make the epoch time human readable? Results to display in a manner such as, 8d 15 hrs 20 minutes?

0 Karma

adonio
Ultra Champion

try this at the end of your query:

  | eval "duration_Days+HHMMSS" = tostring(duration, "duration")

493669
Super Champion

Try this run anywhere search:

|makeresults|eval EndTime="2/14/2018 9:28:19", BeginTime="2/6/2018 14:53:45"|eval EndTime=strptime(EndTime,"%m/%d/%Y %H:%M:%S"), BeginTime=strptime(BeginTime,"%m/%d/%Y %H:%M:%S")|eval days=round((EndTime-BeginTime)/86400)

cotyp
Path Finder

how would you go about getting results in minutes?

0 Karma

493669
Super Champion

to get results in min divide the difference(in sec.) by 60

...|eval minutes=round((EndTime-BeginTime)/60)
0 Karma

493669
Super Champion

if you want duration in day hr and min then try this run anywhere search:

|makeresults|eval EndTime="2/14/2018 9:28:19", BeginTime="2/6/2018 14:53:45"|eval EndTime=strptime(EndTime,"%m/%d/%Y %H:%M:%S"), BeginTime=strptime(BeginTime,"%m/%d/%Y %H:%M:%S")| eval stringSecs = tostring((EndTime-BeginTime), "duration")| eval stringSecss = replace(stringSecs,"(\d+)\+(\d+)\:(\d+)\:.*","\1d \2h \3min ")

cotyp
Path Finder

thank you!

0 Karma

493669
Super Champion

Glad to help you:) Please accept the answer as well.

0 Karma
Get Updates on the Splunk Community!

Detecting Remote Code Executions With the Splunk Threat Research Team

REGISTER NOWRemote code execution (RCE) vulnerabilities pose a significant risk to organizations. If ...

Observability | Use Synthetic Monitoring for Website Metadata Verification

If you are on Splunk Observability Cloud, you may already have Synthetic Monitoringin your observability ...

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...