Splunk Search

How to deal with varied time formats?

yuanliu
SplunkTrust
SplunkTrust

I have some data containing timestamps with varied formats, e.g., sometimes "%m/%d/%y %H:%M", sometimes use "%m/%d/%Y %H:%M:%S".  Is there a format to extract these variants in one strptime()?

Currently I use

 

| eval good_time = strptime(bad_ts, "%m/%d/%y %H:%M"),
 good_time = if(isnull(good_time), strptime(bad_ts, "%m/%d/%Y %H:%M:%S"), good_time)

 

 

Labels (2)
Tags (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Unfortunately, strptime accepts only one format string and format strings do not have an 'OR' operator.

I like your workaround, though.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Unfortunately, strptime accepts only one format string and format strings do not have an 'OR' operator.

I like your workaround, though.

---
If this reply helps you, Karma would be appreciated.

yuanliu
SplunkTrust
SplunkTrust

@richgalloway wrote:

Unfortunately, strptime accepts only one format string and format strings do not have an 'OR' operator.

This would suggest that the indexer is using a more sophisticated method, because I set one of these bad time fields as TIMESTAMP_FIELDS without specifying time format, and the indexed events get the correct time. (I am really glad it does, because sourcetype would not allow me to use two formats.)

0 Karma

richgalloway
SplunkTrust
SplunkTrust

It's more than a suggestion.  Indexers do indeed have a different method for interpreting timestamps.  It's the datetime.xml file and it's a collection of regular expressions that describe just about every date/time format you can think of - and if you think of a new one you can add it to the file.

That's index-time, however.  At search time, we have much more limited options.

---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...