topic Problem with Date Time Manipulation for date in the year 1970 in Splunk Search
https://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/519918#M146409
<P>I have an index that has the fields start date and end date. I need to find the difference between the two timestamps, convert it into days, and put in different duration buckets. </P><P>Following is an example of the data:</P><P>ID START_DATE END_DATE</P><P><SPAN>1 1970-03-12 00:00:00.0 2020-06-17 00:00:00.0</SPAN></P><P><SPAN>2 2015-02-01 00:00:00.0 2020-01-02 00:00:00.0</SPAN></P><P><SPAN>and so on.</SPAN></P><P><SPAN>My query looks like:</SPAN></P><P><SPAN>index={something}<BR />| where START_DATE!="" AND END_DATE!=""<BR />| eval difftime=strptime(END_DATE,"%Y-%m-%d %H:%M:%S.%3N")-strptime(START_DATE,"%Y-%m-%d %H:%M:%S.%3N")<BR />| eval daydiff = round(difftime/86400)<BR />| eval Label=case(<BR />daydiff <= 30, "<=30 Days",<BR />daydiff > 30 AND daydiff <= 90, ">30 Days AND <= 90 Days",<BR />daydiff > 90 AND daydiff <= 365, ">90 Days AND <= 12 Months",<BR />daydiff > 365 AND daydiff <= 730, ">12 Months AND <= 24 Months",<BR />daydiff > 730 AND daydiff <= 1095, ">24 Months AND <= 36 Months",<BR />daydiff > 1095, ">36 Months")<BR />| stats count(ID) as Counts by Label<BR />| eval SortLabel = case(Label="<=30 Days",1,Label=">30 Days AND <= 90 Days",2,Label=">90 Days AND <= 12 Months",3,Label=">12 Months AND <= 24 Months",4,Label=">12 Months AND <= 24 Months",5,Label=">24 Months AND <= 36 Months",6)<BR />| sort SortLabel<BR />| table Label Counts</SPAN></P><P> </P><P><STRONG>Problem: </STRONG>When the start date is in 1970, strptime isn't returning anything at all (which I think is a known issue), which is giving me wrong counts.</P><P>A workaround which I thought was to add an if statement wherever I'm doing the conversion, and hardcode it to 0. But that won't work if, let's say, the start date and end date are both in 1970. In that case, both would be 0, and the count for the first label would increase whereas the count for the appropriate duration bucket should increase. </P><P>Is there a way to do this? Is there any other function to get the UNIX time Or, is there a better way to do this?</P><P>Alternatively, can I find the difference between the two times directly somehow? </P>Wed, 16 Sep 2020 13:16:39 GMTMohammadYusuf2020-09-16T13:16:39ZProblem with Date Time Manipulation for date in the year 1970
https://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/519918#M146409
<P>I have an index that has the fields start date and end date. I need to find the difference between the two timestamps, convert it into days, and put in different duration buckets. </P><P>Following is an example of the data:</P><P>ID START_DATE END_DATE</P><P><SPAN>1 1970-03-12 00:00:00.0 2020-06-17 00:00:00.0</SPAN></P><P><SPAN>2 2015-02-01 00:00:00.0 2020-01-02 00:00:00.0</SPAN></P><P><SPAN>and so on.</SPAN></P><P><SPAN>My query looks like:</SPAN></P><P><SPAN>index={something}<BR />| where START_DATE!="" AND END_DATE!=""<BR />| eval difftime=strptime(END_DATE,"%Y-%m-%d %H:%M:%S.%3N")-strptime(START_DATE,"%Y-%m-%d %H:%M:%S.%3N")<BR />| eval daydiff = round(difftime/86400)<BR />| eval Label=case(<BR />daydiff <= 30, "<=30 Days",<BR />daydiff > 30 AND daydiff <= 90, ">30 Days AND <= 90 Days",<BR />daydiff > 90 AND daydiff <= 365, ">90 Days AND <= 12 Months",<BR />daydiff > 365 AND daydiff <= 730, ">12 Months AND <= 24 Months",<BR />daydiff > 730 AND daydiff <= 1095, ">24 Months AND <= 36 Months",<BR />daydiff > 1095, ">36 Months")<BR />| stats count(ID) as Counts by Label<BR />| eval SortLabel = case(Label="<=30 Days",1,Label=">30 Days AND <= 90 Days",2,Label=">90 Days AND <= 12 Months",3,Label=">12 Months AND <= 24 Months",4,Label=">12 Months AND <= 24 Months",5,Label=">24 Months AND <= 36 Months",6)<BR />| sort SortLabel<BR />| table Label Counts</SPAN></P><P> </P><P><STRONG>Problem: </STRONG>When the start date is in 1970, strptime isn't returning anything at all (which I think is a known issue), which is giving me wrong counts.</P><P>A workaround which I thought was to add an if statement wherever I'm doing the conversion, and hardcode it to 0. But that won't work if, let's say, the start date and end date are both in 1970. In that case, both would be 0, and the count for the first label would increase whereas the count for the appropriate duration bucket should increase. </P><P>Is there a way to do this? Is there any other function to get the UNIX time Or, is there a better way to do this?</P><P>Alternatively, can I find the difference between the two times directly somehow? </P>Wed, 16 Sep 2020 13:16:39 GMThttps://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/519918#M146409MohammadYusuf2020-09-16T13:16:39ZRe: Problem with Date Time Manipulation for date in the year 1970
https://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/519960#M146417
<P>If the date starts with 1970, make it 1971, then subtract 365*24*60*60 from the parsed date</P>Wed, 16 Sep 2020 15:34:46 GMThttps://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/519960#M146417ITWhisperer2020-09-16T15:34:46ZRe: Problem with Date Time Manipulation for date in the year 1970
https://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/523176#M147562
<P>Thanks. I used a similar logic to replace the years if they are before 1971 with 1971 and subtract the seconds in years multiplied by the difference between 1971 and the given year</P>Tue, 06 Oct 2020 13:11:07 GMThttps://community.splunk.com/t5/Splunk-Search/Problem-with-Date-Time-Manipulation-for-date-in-the-year-1970/m-p/523176#M147562MohammadYusuf2020-10-06T13:11:07Z