Splunk Search

Difficult date time conversion

timbitsandbytes
Engager

Well it's a difficult conversion for me, anyway.

Here's the field: dateTime=Fri Jan 18 17:11:55 GMT+00:00 2013

  • I want to convert it to seconds since the epoch so I can do a date comparison.
  • I don't think there's a way for Splunk to recognize the tz offset as "+00:00" so first I transform that to "+0000".

eval dtFormatted=replace(dateTime, ":", "") |
eval dtSeconds=strptime(dtFormatted, "%a %b %d %H%M%S %Z%:z %Y")

In my search results "dtFormatted" is discovered and I've verified it's properly formatted but "dtSeconds" is not discovered.

What am I doing wrong? Why can't I convert this string to seconds?

Tags (1)
0 Karma
1 Solution

jonuwz
Influencer

Try this :

... | rex mode=sed field=dateTime "s/(\+[0-9:]{5})/ \1/" 
    | eval dtSeconds=strptime(dateTime, "%a %b %d %T %Z %:z %Y")

The problem was that %Z expects a space after the time zone, In your format %Z was being set to GMT+0000 instead of GMT, and %Y was null.

View solution in original post

jonuwz
Influencer

Try this :

... | rex mode=sed field=dateTime "s/(\+[0-9:]{5})/ \1/" 
    | eval dtSeconds=strptime(dateTime, "%a %b %d %T %Z %:z %Y")

The problem was that %Z expects a space after the time zone, In your format %Z was being set to GMT+0000 instead of GMT, and %Y was null.

timbitsandbytes
Engager

I see - I would not have caught that. But your sed/regex works great. Thanks very much!

0 Karma

timbitsandbytes
Engager

Yes, thanks. Unfortunately when I copied the search string (on a network that doesn't have access to the Internet) I miscopied it. It's been corrected above.

0 Karma

jonuwz
Influencer

Shouldn't you have dtFormatted as the 1st argument to strptime ?

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...