Getting Data In

Can you help me come up with a brutal but clever Date and Time Parsing?

morethanyell
Builder

I'm working with a date and time field that's causing a headache. I need to parse it to epoch but using strptime(MyInconsistentDateTimeField, "%d/%m/%Y %l:%M:%S") would only work with some of the values in this field. Say I have a date of "January 2, 2019 9:10:10 a.m." below are the possible formats of my field.

MyInconsistentDateTimeField
02/01/2019 09:10:10
2/01/2019 09:10:10
02/1/2019 09:10:10
2/1/2019 09:10:10
02/01/2019 09:10
2/01/2019 09:10
02/1/2019 09:10
2/1/2019 09:10
02/01/2019 9:10:10
2/01/2019 9:10:10
02/1/2019 9:10:10
2/1/2019 9:10:10
02/01/2019 9:10
2/01/2019 9:10
02/1/2019 9:10
2/1/2019 9:10

As you can see, only the first item can be parsed using | eval parsedDate = strptime(MyInconsistentDateTimeField, "%d/%m/%y %l:%M:%S") How do I brute force parsing this one? I'm thinking of separating every element of the date using regex, treat them, and then concatenate later on before parsing (e.g. day, month, year, hour, minute would be separate fields). But I don't know how to do it.

Thanks in advance!

p.s. It's okay to ignore the 10 seconds in this date.

0 Karma
1 Solution

samhays
Path Finder

Your solution seems okay to me... maybe something like this:

| rex field=MyInconsistentDateTimeField "(?<d>[^//]*)/(?<m>[^//]*)/(?<y>[^\s]*)\s+(?<h>[^:]*):(?<min>[^:]*)(:(?<sec>.*))?"
| eval y = substr(y, len(y)-1,2)
| fillnull value="00" sec
| eval newTime=printf("%02d/%02d/%02d %02d:%02d:%02d", d,m, y,h,min,sec)
| eval parsed=strptime(newTime,"%d/%m/%y %l:%M:%S")

There may be a better way - but this I think this'd work to normalize the field.

View solution in original post

somesoni2
Revered Legend

Try like this: (everything before | rex field=MyInconsistentDateTimeField ... is to generate sample data)

| gentimes start=-1 | eval MyInconsistentDateTimeField="02/01/2019 09:10:10#2/01/2019 09:10:10#02/1/2019 09:10:10#2/1/2019 09:10:10#02/01/2019 09:10#2/01/2019 09:10#02/1/2019 09:10#2/1/2019 09:10#02/01/2019 9:10:10#2/01/2019 9:10:10#02/1/2019 9:10:10#2/1/2019 9:10:10#02/01/2019 9:10#2/01/2019 9:10#02/1/2019 9:10#2/1/2019 9:10" | table MyInconsistentDateTimeField | makemv MyInconsistentDateTimeField delim="#" | mvexpand MyInconsistentDateTimeField | eval orig=MyInconsistentDateTimeField  
| rex field=MyInconsistentDateTimeField  mode=sed "s/(^|\s|\/|\:)(\d)(\/|$|:)/\1[0]\2\3/g s/(\[|\])//g s/(\s\d\d\:\d\d)$/\1:00/" | eval epoch=strptime(MyInconsistentDateTimeField, "%m/%d/%Y %H:%M:%S")
0 Karma

samhays
Path Finder

Your solution seems okay to me... maybe something like this:

| rex field=MyInconsistentDateTimeField "(?<d>[^//]*)/(?<m>[^//]*)/(?<y>[^\s]*)\s+(?<h>[^:]*):(?<min>[^:]*)(:(?<sec>.*))?"
| eval y = substr(y, len(y)-1,2)
| fillnull value="00" sec
| eval newTime=printf("%02d/%02d/%02d %02d:%02d:%02d", d,m, y,h,min,sec)
| eval parsed=strptime(newTime,"%d/%m/%y %l:%M:%S")

There may be a better way - but this I think this'd work to normalize the field.

morethanyell
Builder

Works like a charm. Thank you so, so, very much.

0 Karma

samhays
Path Finder

Glad to help and glad it worked out!

0 Karma

auraria1
Path Finder

I think you may be on the right track by breaking down the values.

| rex field=MyInconsistentDateTimeField "^(?[\d]+)\/.*"

| rex field=MyInconsistentDateTimeField "^[\d]+\/(?[\d]+)\/.*"

| rex field=MyInconsistentDateTimeField "^[\d]+\/[\d]+\/(?[\d]+)\/.*"

| rex field=MyInconsistentDateTimeField ".(?[\d]+):."

| rex field=MyInconsistentDateTimeField ".:(?[\d]+)."

| fields MyInconsistentDateTimeField, day1, month1, year1, hour1, minute1

check that and see if it grabbed the right values, from that you can build on it as you need.

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, ...