How to convert date and time from a line in a text file?
<p>I was wondering if there is a way to convert date and time from a pre-set value in a line from a text file. </p>

<p>For the date conversion, I need the day of the year. I found a way to get the current day of the year with this code:</p>

<pre><code>from datetime import datetime
day_of_year = datetime.now().timetuple().tn_yday
print day_of_year
</code></pre>

<p>But will need it for past years also (ex; 1991, 1995, 2004).</p>

<p>For the time conversion, I  need the seconds of the day. The code I have that converts hours, minutes, and seconds to seconds of the day is:</p>

<pre><code>def get_sec(s):
    x = s.split(':')
    return int(x[0]) * 3600 + int(x[1]) * 60 + int(x[2])
    print get_sec('17:36:00) //gives me an output of 63360
</code></pre>

<p>But I can't be inputting the values for hour, minutes, seconds each time since it has to read whatever value from the line in the text file.</p>

<p>The sample text file I have, lets call it datetime.txt, is:</p>

<pre><code>1.a  Date Installed :    1991-01-19T00:00Z
     Date Removed   :    1993-02-08T00:00Z

1.b  Date Installed :    1993-02-09T00:00Z
     Date Removed   :    1994-01-12T00:00Z

1.c  Date Installed :    1994-01-12T00:00Z
     Date Removed   :    1994-02-16T17:36Z
</code></pre>

<p>To make sense of the sample text file, <strong>for the time</strong>, the 2 characters after the 'T' represents hours and the 2 characters after the colon ':' represents minutes. <strong>For the date</strong>, the 2 characters between the dash '-' represents months, and the 2 characters before the 'T' represents days. So for example 1.a, <strong>time</strong> 00 is hours, 00 is minutes. <strong>date</strong> 01 is months (January), 19 is days.</p>

<p>The current code I have now is:</p>

<pre><code>with open('datetime.txt', 'r') as dt:
for line in dt:
    header = line.split(':')[0]
    if 'Date Installed' in header:
        year = line.split(':')[1].strip()[2:4]
        day_of_year = line.split(':')[1].strip()[5:7] + line.split(':')[1].strip()[8:10]
        sec_of_day = line.split(':')[1].strip()[14:16]
        print year,
        print day_of_year,
        print sec_of_day
</code></pre>

<p>The indexes I added is to tell which characters from the line in the text file to print, start to finish.</p>

<p>I've been stuck at this part of my script for a while now. Still new to Python so not quiet familiar with everything yet.</p>

<p>So how would I implement or add to my current code the two conversions for day and time? The purpose of this is so I can run a general code that will run and give me the same output, format and conversion, with other text files with different values.</p>

<p>Any help in the right direction is appreciated. </p>

<p><strong>Note</strong>
I am not sure if this question is a duplicate to something else. I wasn't really quiet sure how to word my question properly, so I do apologize if it does end up being a duplicate. And also, this is not for school. Just for my own personal use at work to run different text files and extract/print specific parts of lines.</p>