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 (1)
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!

Reduce and Transform Your Firewall Data with Splunk Data Management

Managing high-volume firewall data has always been a challenge. Noisy events and verbose traffic logs often ...

Automatic Discovery Part 1: What is Automatic Discovery in Splunk Observability Cloud ...

If you’ve ever deployed a new database cluster, spun up a caching layer, or added a load balancer, you know it ...

Real-Time Fraud Detection: How Splunk Dashboards Protect Financial Institutions

Financial fraud isn't slowing down. If anything, it's getting more sophisticated. Account takeovers, credit ...