0

I have a path files which are named by time (201803061500) etc. What I need is a time conversion, because I use while loop to open a few of them there is an error when I want files from for example (...1555 to ... 1615) and Python sees an obivous problem because after 1555 is 1560, but I want him to convert that to time so after (...1555 will be ... 1600) any ideas how to use it?

Btw. Time conversion must be contain 4 digits, so it cannot be 16:00/16-00 etc. it must be 1600, because it goes as an input to my pathfile. Any ideas?

UPDATE - I did this, but this code is rubbish and I think my problem might be solved by one command.

Start_time_hours = input('Enter start time (hh): ' )
Start_time_minutes = input('Enter start time (mm): ')

if Start_time_hours >= 24:
    print ("Values from 00 to 23 only!")
if Start_time_minutes >= 60:
    x = Start_time_hours + 1
    y = Start_time_minutes - Start_time_minutes
    if y == 0:
        print "Ok"
        print x, y

if Start_time_minutes <= 55:
        print Start_time_hours, Start_time_minutes
Hiddenguy
  • 547
  • 13
  • 33
  • Could you add some of your own code, what have you tried so far ? – Gwendal Grelier Mar 06 '18 at 08:23
  • Have you tried datetime.strptime() ? – Ove Mar 06 '18 at 08:23
  • @Ove Sure, but in my loop I need to add 5 minutes every loop and since `datetime.strptime()` is a string I cannot do that, plus when I put this format to my pathfile an error occurs, because there is also year etc. And as I have mentioned I need only (HHMM - hours minutes). – Hiddenguy Mar 06 '18 at 08:27
  • 2
    The return value of `datetime.strptime()` is *not* a string. It's a datetime object, and you can add timedelta objects to it. – Ove Mar 06 '18 at 08:29
  • @Hiddenguy As Ove suggests, use `timedelta` (5 minutes) to add to to your `datetime` object. Once you add the `timedelta` You can extract the hours and minutes part from the result. – patex1987 Mar 06 '18 at 08:34

2 Answers2

0

One easy way to handle unformated time is the datetime. You can first strip your strings and then do whatever you want !

from datetime import datetime, timedelta
from time import strtime

datetime_object = datetime.strptime(file_name, '%Y%m%d%H%M')

print(datetime_object) # returns '2018-03-06 15:00:00'

delta = timedelta(minutes=5)
next_time = datetime_object + delta 
print(next_time) # returns '2018-03-06 15:05:00'

Finally you can get your string back by using time.strftime() function

new_string = next_time.strftime('%Y%m%d%H%M')
print(new_string) # returns '201803061505'

Source datetime: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior Source time: https://docs.python.org/2/library/time.html

Gwendal Grelier
  • 526
  • 1
  • 7
  • 21
  • `from datetime import datetime Start_time = input('Enter start time (hh): ' ) Time_range = str(Start_time) x = datetime.strptime(Time_range, '%H%M') print x` Well it gives me that format `1900-01-01 14:00:00` which is not what I wanted plus in that case I cannot add another 5 minutes in my loop. – Hiddenguy Mar 06 '18 at 08:37
  • That's normal, you need to reconvert your `datetime_object`back to a string after you've added the 5 minutes. I've edited my post to make it clearer – Gwendal Grelier Mar 06 '18 at 08:50
0
Start_time_hours += (Start_time_minutes / 60)
Start_time_minutes %= 60
Start_time_minutes += 5

Those three lines solved my problem, datetime also works, but if you put those variables to input pathfile, you'll get an error. That's why I've choosen this solution.

Hiddenguy
  • 547
  • 13
  • 33