Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting up long number
#1
Hi there,

I have some timecodes in the format:
144345.20
how can convert this to a normal timecode that can then be used for calculating time?

To clarify what this is, it is not seconds, it is hhmmss all bunched together, so you have first two digits are the hours the next two minutes and the last four are seconds and milliseconds. That's an awful lot a wheel reinvention.

Regards
iFunk
Reply
#2
If that's a string, you can use datetime.strptime().
If it's not, convert it to a string first.
Reply
#3
That sounds excellent, I can't figure out how to use it though I just keep getting attribute error or name error and that it doesn't exist.
Reply
#4
If the number is a number, you could also try

time_stamp = 144345.20
string_time = str(time_stamp)    # Convert number to a string
hours = string_time[0:2]
minutes = string_time[3:5]
sec = string_time[4:]
print("{} hours {} minutes {} seconds".format(hours, minutes, sec))
resulting in:

Output:
14 hours 34 minutes 45.2 seconds
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
This is how I think I will end up doing it. what I am trying to do is write a script that will let me know how long something has happened, so I am trying to get it into a format where I can subtract the start time from the end time to give a run time, but I can't work out how to do it. In my mind I would convert to seconds do calculation and then convert the result back into hours, minutes, seconds and milliseconds, but I'm not sure what sort of terminology to put into google to find that info.
Reply
#6
If that is your goal, than you should follow Stranac's advice and check out the Datetime module and the timedelta function, it is made for just that sort of thing.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
>>> # pip install python-dateutil
>>> from dateutil import parser
>>> dt = parser.parse('144345.20)
>>> print(dt)
2016-12-07 14:43:45.200000
>>> dt.hour
14
Quote:so I am trying to get it into a format where I can subtract the start time from the end time to give a run time, but I can't work out how to do it. 

Something like this?
import time

def time_code():
    start = time.clock()
    time.sleep(5)
    end = time.clock()
    print('Code time {:.2f} seconds'.format(end - start))

time_code() # Code time 5.00 seconds
There also timeit that is used to measure code.
Reply
#8
Here's a great site for seeing what you can do with python dates
http://pleac.sourceforge.net/pleac_pytho...times.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  float("{:.2f}" return long number? korenron 2 989 Jul-13-2022, 05:35 AM
Last Post: korenron
  Sequence Generator - Long number krux_rap 3 2,279 Aug-19-2020, 06:37 PM
Last Post: buran
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,258 Mar-31-2020, 06:40 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020