Getting Data In

How to convert date_* fields to epoch time after a join to calculate time difference?

singhbc
Path Finder

I have a search that uses "join" which uses two sourcetypes to search the events and then joins them using a common field called ID. It works good to show me that for an ID, a service was called in sourcetype=A with timestamp in date_year (month, hour, minute and second) and ERROR was flagged in sourcetype=B with timestamp _time.

What i need to do now is to subtract the _time from the date_hour, date_minute and date_second to get the difference. What is the best way to do that? Should I convert the date_year, date_month and so on to epoch time first? If yes how?

1 Solution

somesoni2
Revered Legend

You can use strptime function with eval/where to get the corresponding epoch time value from date_* fields.

try something like this

your current search so far | eval timeA=strptime(date_year."-".date_month."-".date_mday." ".date_hour.":".date_minute.":".date_second,"%Y-%b-%d %H:%M:%S") | eval duration=_time-timeA

View solution in original post

somesoni2
Revered Legend

You can use strptime function with eval/where to get the corresponding epoch time value from date_* fields.

try something like this

your current search so far | eval timeA=strptime(date_year."-".date_month."-".date_mday." ".date_hour.":".date_minute.":".date_second,"%Y-%b-%d %H:%M:%S") | eval duration=_time-timeA

singhbc
Path Finder

date_month is a string, how to convert it to integer value?

0 Karma

somesoni2
Revered Legend

just updated my search to handle string date_month (replace %m with %b)

0 Karma

singhbc
Path Finder

Thanks so lot! This is perfect!

0 Karma

somesoni2
Revered Legend

Not sure about the requirement here. If you have date_* fields in your sourcetype="A" then you should have a field _time as well, in epoch format. Whether subtraction of two _time (you would have rename the field to something else in order to have both time value from both the sourcetypes) values would not suffice?

0 Karma

singhbc
Path Finder

the date_* fields are for the timestamp for when a "service" was called for an ID, and the time is for when the ERROR occurred for the same ID and they are about 10+ seconds apart, after the "join" is done, and after the "join" is done, if i look at the events, it only shows the timestamp for the ERROR along with the event, whereas the `dateshows only in the field list on the left side. and I have confirmed that thedate_` corresponds to the timestamp for the service in sourcetype="A" in a separate standalone search for that service. hope I am able to clarify.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

Step into “Hunt the Insider: An Splunk ES Premier Mystery” to catch a cybercriminal ...

After a whole week of being on call, you fell asleep on your keyboard, and you hit a sequence of buttons that ...