<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Why am I unable to change this datetime string to a time formatted field? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260849#M78265</link>
    <description>&lt;P&gt;Ah! Looks like it was fixed in 2008 R2. So now there are 2 different timestamp formats in the logs. &lt;BR /&gt;
e.g.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Message=The system time has changed to ?2015?-?12?-?13T13:28:07.492000000Z from ?2015?-?12?-?13T13:18:04.893874600Z.

and

Message=The system time has changed to 2015-12-12T09:09:14.198Z from 2015-12-12T09:09:14.198Z.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So here is my fixed rex (only to the second - decided not to bother with milliseconds):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.*to\D+(?&amp;lt;StartYear&amp;gt;\d+)\D+(?&amp;lt;StartMonth&amp;gt;\d+)\D+(?&amp;lt;StartDay&amp;gt;\d+)T(?&amp;lt;StartTime&amp;gt;[^.]+).* from\D+(?&amp;lt;EndYear&amp;gt;\d+)\D+(?&amp;lt;EndMonth&amp;gt;\d+)\D+(?&amp;lt;EndDay&amp;gt;\d+)T(?&amp;lt;EndTime&amp;gt;[^.]+)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is the full search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="WinEventLog:System" "system time has changed" | rex field=Message ".*to\D+(?&amp;lt;StartYear&amp;gt;\d+)\D+(?&amp;lt;StartMonth&amp;gt;\d+)\D+(?&amp;lt;StartDay&amp;gt;\d+)T(?&amp;lt;StartTime&amp;gt;[^.]+).* from\D+(?&amp;lt;EndYear&amp;gt;\d+)\D+(?&amp;lt;EndMonth&amp;gt;\d+)\D+(?&amp;lt;EndDay&amp;gt;\d+)T(?&amp;lt;EndTime&amp;gt;[^.]+)" | strcat StartYear "-" StartMonth "-" StartDay "T" StartTime StartTime | strcat EndYear "-" EndMonth "-" EndDay "T" EndTime EndTime | eval StartUnix=strptime(StartTime, "%Y-%m-%dT%H:%M:%S") | eval EndUnix=strptime(EndTime, "%Y-%m-%dT%H:%M:%S") | eval TotalTime=EndUnix - StartUnix | table _time host StartTime EndTime TotalTime
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 14 Dec 2015 17:00:46 GMT</pubDate>
    <dc:creator>matthewhaswell</dc:creator>
    <dc:date>2015-12-14T17:00:46Z</dc:date>
    <item>
      <title>Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260839#M78255</link>
      <description>&lt;P&gt;I have a date timestamp coming in as a string in this format&lt;BR /&gt;
&lt;CODE&gt;‎2015‎-‎10‎-‎07T19:49:34.676416100Z&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;With Regex, I get one Field called &lt;STRONG&gt;Date_old&lt;/STRONG&gt; like this: &lt;CODE&gt;2015-10-07&lt;/CODE&gt;&lt;BR /&gt;
and one called &lt;STRONG&gt;Time_old&lt;/STRONG&gt; like this: &lt;CODE&gt;19:49:34.676416100&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Now I want to be able to calculate with this time. So I also have a &lt;STRONG&gt;DateTime_old&lt;/STRONG&gt; field like this: &lt;CODE&gt;2015-10-07 19:49:34.676416100&lt;/CODE&gt;&lt;BR /&gt;
I want to compare this &lt;STRONG&gt;DateTime_old&lt;/STRONG&gt; with another same formatted Timestamp in the same Log (&lt;STRONG&gt;DateTime_new&lt;/STRONG&gt; extracted)&lt;/P&gt;

&lt;P&gt;But I can't change this string to a Time-formatted field.&lt;BR /&gt;
I already tried things like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval createDt = strptime(DateTime_old,"%Y-%m-%d %H:%M:%S")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;or&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval epochtime=strptime(DateTime_old, "%Y-%m-%d")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;or&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval A_reformatted_date=strftime(strptime(Date_old,"%Y-%m-%d"),"%m/%d/%y")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and nothing worked. I never get anything in my new eval field. I can check with a table, that my &lt;STRONG&gt;Time_old&lt;/STRONG&gt;, &lt;STRONG&gt;Date_old&lt;/STRONG&gt; and &lt;STRONG&gt;DateTime_old&lt;/STRONG&gt; Fields are correct, but I never get any value in my new field.&lt;BR /&gt;
Do you have any idea what I can change or try?&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 20:03:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260839#M78255</guid>
      <dc:creator>lukas_loder</dc:creator>
      <dc:date>2015-10-07T20:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260840#M78256</link>
      <description>&lt;P&gt;Your strptime format string has to match the value in the field.  Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval createDt = strptime(DateTime_old,"%Y-%m-%d %H:%M:%S.%9N")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Once you've converted the times into epoch format it's easy to compare them or make calculations.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 20:20:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260840#M78256</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2015-10-07T20:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260841#M78257</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;

&lt;P&gt;Unfortunately it doesn't work&lt;/P&gt;

&lt;P&gt;my DateTime_old looks like this&lt;BR /&gt;
2015‎-‎10‎-‎07 20:24:57.879337800 &lt;/P&gt;

&lt;P&gt;I check it with the table, the DateTime_old is always correct, but the createDt is empty&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval createDt = strptime(DateTime_old,"%Y-%m-%d %H:%M:%S.%9N") | table DateTime_old createDt
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is my complete search string:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;EventCode=1 "Keywords=Time" | rex field=Message "The system time has changed to ‎(?P&amp;lt;Date_old&amp;gt;\\d+‎\\-‎\\d+‎\\-‎\\d+)T(?P&amp;lt;Time_old&amp;gt;\\d+:\\d+:\\d+\\.\\d+)\\w+\\s+\\w+\\s+‎(?P&amp;lt;Date_new&amp;gt;\\d+‎\\-‎\\d+‎\\-‎\\d+)T(?P&amp;lt;Time_new&amp;gt;\\d+:\\d+:\\d+\\.\\d+)" | eval DateTime_old = Date_old +" "+ Time_old | eval createDt = strptime(DateTime_old,"%Y-%m-%d %H:%M:%S.%9N") | table DateTime_old createDt
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;thanks for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 20:32:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260841#M78257</guid>
      <dc:creator>lukas_loder</dc:creator>
      <dc:date>2015-10-07T20:32:36Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260842#M78258</link>
      <description>&lt;P&gt;It definitely works as shown in this run-anywhere example.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | head 1 | eval DateTime_old="2015-10-07 20:24:57.879337800" | eval createDt=strptime(DateTime_old,"%Y-%m-%d %H:%M:%S.%9N") | table DateTime_old createDt

DateTime_old    createDt
2015-10-07 20:24:57.879337800   1444263897.879337
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Interestingly, the same search fails if I copy-and-paste your DateTime_old string, but works find if typed by hand.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 21:45:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260842#M78258</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2015-10-07T21:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260843#M78259</link>
      <description>&lt;P&gt;Thanks for your hint!&lt;/P&gt;

&lt;P&gt;I also tried copy my value. And it didn't work. I figured out, that there is somethingbetween "2015" and the "-". But I have no idea what. &lt;/P&gt;

&lt;P&gt;Now I'm doing it with substr and it works fine. Strange....&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;DateTime_old = substr(Date_old,1,4) +"-"+ substr(Date_old,8,2) +"-"+ substr(Date_old,13,2) +" "+ Time_old
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 06:03:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260843#M78259</guid>
      <dc:creator>lukas_loder</dc:creator>
      <dc:date>2015-10-08T06:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260844#M78260</link>
      <description>&lt;P&gt;eval createDt = strptime(DateTime_old,"%Y-%m-%d %H:%M:%S") eval createDt = strptime(DateTime_old,"%Y-%m-%d %H:%M:%S")&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:31:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260844#M78260</guid>
      <dc:creator>srinathhh</dc:creator>
      <dc:date>2020-09-29T07:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260845#M78261</link>
      <description>&lt;P&gt;I figured out, that there is something between "2015" and the "-". But I have no idea what.&lt;/P&gt;

&lt;P&gt;Now I'm doing it with substr and it works fine. Strange....&lt;/P&gt;

&lt;P&gt;DateTime_old = substr(Date_old,1,4) +"-"+ substr(Date_old,8,2) +"-"+ substr(Date_old,13,2) +" "+ Time_old&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:31:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260845#M78261</guid>
      <dc:creator>lukas_loder</dc:creator>
      <dc:date>2020-09-29T07:31:56Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260846#M78262</link>
      <description>&lt;P&gt;Glad to have helped.  Please accept an answer.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 11:37:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260846#M78262</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2015-10-08T11:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260847#M78263</link>
      <description>&lt;P&gt;SOLUTION:&lt;/P&gt;

&lt;P&gt;I figured out, that there is somethingbetween "2015" and the "-". But I have no idea what.&lt;/P&gt;

&lt;P&gt;Now I'm doing it with substr and it works fine.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;DateTime_old = substr(Date_old,1,4) +"-"+ substr(Date_old,8,2) +"-"+ substr(Date_old,13,2) +" "+ Time_old
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 12:20:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260847#M78263</guid>
      <dc:creator>lukas_loder</dc:creator>
      <dc:date>2015-10-08T12:20:01Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260848#M78264</link>
      <description>&lt;P&gt;I think I hit this weirdness as well - this is from Windows system event logs isn't it? I wanted to check how well (or not) our NTP system was working.&lt;/P&gt;

&lt;P&gt;I used this search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="WinEventLog:System" "system time has changed" | rex field=Message ".*to (?&amp;lt;StartTime&amp;gt;[^.]+).*from (?&amp;lt;EndTime&amp;gt;[^.]+)\."  | eval StartUnix=strptime(StartTime, "%Y-%m-%dT%H:%M:%S") | eval EndUnix=strptime(EndTime, "%Y-%m-%dT%H:%M:%S") | table _time host StartTime EndTime StartUnix EndUnix
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;However, like Lukas, the strptime wasn't doing the conversion.&lt;/P&gt;

&lt;P&gt;Copying and pasting the text from Splunk into Notepad++ then actually the Message line is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Message=The system time has changed to ?2015?-?12?-?13T13:28:07.492000000Z from ?2015?-?12?-?13T13:18:04.893874600Z.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Notice the hidden control codes around the date fields? What on earth were Microsoft thinking? Anyway I have a solution using the rex command.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=Message ".*to \D(?&amp;lt;StartYear&amp;gt;\d+)\D-\D(?&amp;lt;StartMonth&amp;gt;\d+)\D-\D(?&amp;lt;StartDay&amp;gt;\d+)T(?&amp;lt;StartTime&amp;gt;.*)Z from \D(?&amp;lt;EndYear&amp;gt;\d+)\D-\D(?&amp;lt;EndMonth&amp;gt;\d+)\D-\D(?&amp;lt;EndDay&amp;gt;\d+)T(?&amp;lt;EndTime&amp;gt;.*)Z"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The full search string I used is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="WinEventLog:System" "system time has changed" | rex field=Message ".*to \D(?&amp;lt;StartYear&amp;gt;\d+)\D-\D(?&amp;lt;StartMonth&amp;gt;\d+)\D-\D(?&amp;lt;StartDay&amp;gt;\d+)T(?&amp;lt;StartTime&amp;gt;.*)Z from \D(?&amp;lt;EndYear&amp;gt;\d+)\D-\D(?&amp;lt;EndMonth&amp;gt;\d+)\D-\D(?&amp;lt;EndDay&amp;gt;\d+)T(?&amp;lt;EndTime&amp;gt;.*)Z" | strcat StartYear "-" StartMonth "-" StartDay "T" StartTime StartTime | strcat EndYear "-" EndMonth "-" EndDay "T" EndTime EndTime | eval StartUnix=strptime(StartTime, "%Y-%m-%dT%H:%M:%S.%9N") | eval EndUnix=strptime(EndTime, "%Y-%m-%dT%H:%M:%S.%9N") | eval TotalTime=EndUnix - StartUnix | table _time host StartTime EndTime StartUnix EndUnix TotalTime
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Dec 2015 10:37:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260848#M78264</guid>
      <dc:creator>matthewhaswell</dc:creator>
      <dc:date>2015-12-14T10:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: Why am I unable to change this datetime string to a time formatted field?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260849#M78265</link>
      <description>&lt;P&gt;Ah! Looks like it was fixed in 2008 R2. So now there are 2 different timestamp formats in the logs. &lt;BR /&gt;
e.g.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Message=The system time has changed to ?2015?-?12?-?13T13:28:07.492000000Z from ?2015?-?12?-?13T13:18:04.893874600Z.

and

Message=The system time has changed to 2015-12-12T09:09:14.198Z from 2015-12-12T09:09:14.198Z.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So here is my fixed rex (only to the second - decided not to bother with milliseconds):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.*to\D+(?&amp;lt;StartYear&amp;gt;\d+)\D+(?&amp;lt;StartMonth&amp;gt;\d+)\D+(?&amp;lt;StartDay&amp;gt;\d+)T(?&amp;lt;StartTime&amp;gt;[^.]+).* from\D+(?&amp;lt;EndYear&amp;gt;\d+)\D+(?&amp;lt;EndMonth&amp;gt;\d+)\D+(?&amp;lt;EndDay&amp;gt;\d+)T(?&amp;lt;EndTime&amp;gt;[^.]+)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is the full search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="WinEventLog:System" "system time has changed" | rex field=Message ".*to\D+(?&amp;lt;StartYear&amp;gt;\d+)\D+(?&amp;lt;StartMonth&amp;gt;\d+)\D+(?&amp;lt;StartDay&amp;gt;\d+)T(?&amp;lt;StartTime&amp;gt;[^.]+).* from\D+(?&amp;lt;EndYear&amp;gt;\d+)\D+(?&amp;lt;EndMonth&amp;gt;\d+)\D+(?&amp;lt;EndDay&amp;gt;\d+)T(?&amp;lt;EndTime&amp;gt;[^.]+)" | strcat StartYear "-" StartMonth "-" StartDay "T" StartTime StartTime | strcat EndYear "-" EndMonth "-" EndDay "T" EndTime EndTime | eval StartUnix=strptime(StartTime, "%Y-%m-%dT%H:%M:%S") | eval EndUnix=strptime(EndTime, "%Y-%m-%dT%H:%M:%S") | eval TotalTime=EndUnix - StartUnix | table _time host StartTime EndTime TotalTime
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Dec 2015 17:00:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-am-I-unable-to-change-this-datetime-string-to-a-time/m-p/260849#M78265</guid>
      <dc:creator>matthewhaswell</dc:creator>
      <dc:date>2015-12-14T17:00:46Z</dc:date>
    </item>
  </channel>
</rss>

