python3 why does my comparison change the formatting? - python

I have 1 time that is formatted like this
start_time 01:13:05
I have a second time that looks like it
Current time 01:13:10
When I calculating the difference, it produces the correct answer but chops a zero off
code:
new_start_time = datetime.strptime(start_time, '%H:%M:%S')
new_current_time = datetime.strptime(str(current_time), '%H:%M:%S')
elapsed_time = new_current_time - new_start_time
produces:
elapsed time 0:00:10
The 10 is correct but what happened to the zero? How do I hack it back on? I need it.

Add:
elapsed_split = str(elapsed_time).split(":")
elapsed_time = int(elapsed_split[0]).zfill(2) + elapsed_split[1:]

Related

python 3 datetime difference in microseconds giving wrong answer for a long operation

I'm doing a delete operation of 3000 elements from a binary search tree of size 6000 ( sorted therefore one sided tree). I need to calculate the time taken for completing all the deletes
I did this
bst2 = foo.BinarySearchTree() #init
insert_all_to_tree(bst2,insert_lines) #insert 6000 elements
start = datetime.now() #start time
for idx, line in enumerate(lines):
bst2.delete(line) #deleting
if (idx%10 == 0):
print("deleted ", (idx+1), "th element - ", line)
end = datetime.now() #completion time
duration = end - start
print(duration.microseconds) #duration in microseconds
I got the answer 761716 microseconds which is less than even a minute when my actual code ran for about 5 hours. I expected something in the ranges of 10^9 - 10^10. I even checked the max integer allowed in python to see if it's related to that but apparently that's not the problem.
Why I'm I getting a wrong answer for the duration?
datetime.now() returns a datetime, so doing math with it doesn't work out. You want to either use time.time() (Python < v3.3), time.perf_counter() (Python v3.3 until v3.7) or time.perf_counter_ns() (Python > v3.7).
time.time() and time.perf_counter() both return float, and time.perf_counter_ns() returns int.

Is there a way to format a timedelta object to be hours-minutes-seconds.MILLISECONDS?

I have something like this:
import datetime
a = datetime.datetime.now()
do_something_for_a_long_time()
b = datetime.datetime.now()
c = b - a
print("Doing <something> took {c}".format(c))
The problem is that this works well but we want the seconds value to be in the form ., not microseconds?
I was able to isolate the milliseconds attribute from that timedelta object, it it seems it only has microseconds available.
Given your initial code example, you could use something like this:
# From your example.
c = b - a
# Get the hours, minutes, and seconds.
minutes, seconds = divmod(c.seconds, 60)
hours, minutes = divmod(minutes, 60)
# Round the microseconds to millis.
millis = round(c.microseconds/1000, 0)
print(f"Doing <something> took {hours}:{minutes:02}:{seconds:02}.{millis}")
which results in
# c = datetime.timedelta(seconds=7, microseconds=319673)
Doing <something> took 0:00:07.320
Take a look at Python’s built-in functions round() and divmod() and please poke around this and this related thread; also, please read through this and this thread to learn more about formatting timedelta objects.
just chop off the last three digits of the microseconds if you don't want to manually round off:
def format_time():
t = datetime.datetime.now()
s = t.strftime('%Y-%m-%d %H:%M:%S.%f')
return s[:-3] `def format_time():
t = datetime.datetime.now()
s = t.strftime('%Y-%m-%d %H:%M:%S.%f')
return s[:-3]

Get the time in seconds in this form d.ddd

I want to get the elapsed time in the form of seconds in this form d.ddddddd, e.g., 5.656 seconds. When using time.time(), the value becomes very large, something like 1485831617.19 which has 10 values. How can modify the following way:
start = time.time()
time.sleep(2)
elapsed = time.time()
pretty = '{:.3f}'.format(elapsed)
print pretty
This still print something like 1485832865.471. Thank you for help in advance.
Try something like this:
pretty = "%.3f" % (elapsed)

Python - Print index of for loop only once every five minutes

I'm trying to get a for loop to print the value of 'i' every 5 minutes
import threading
def f(i):
print(i)
threading.Timer(600, f).start()
for i in range(1,1000000000000):
f(i=i)
However, this method results in the code printing the value of i instantly since it calls 'f' as soon as it finds 'i'.
I know this is not the first time someone will ask, nor the last, but I can't get it to work on a for loop nested within a function.
I'm fairly new to Python and I'd appreciate any help.
How about just keeping track of how long has passed in the loop?
from timeit import default_timer as timer
start = timer()
freq = 5 * 60 # Time in seconds
last_time = 0.0
for i in range(int(1e8)):
ctime = timer()
if ctime - last_time > freq:
print(i)
last_time = ctime
I imagine you can make this more efficient by only checking the time every N iterations rather than every time. You may also want to look into using progressbar2 for a ready-made solution.
I prefer using datetime, as I think it's more intuitive and looks a bit cleaner. Otherwise, using more or less the same approach as Paul:
from datetime import datetime, timedelta
print_interval = timedelta(minutes=5)
# Initialize the next print time. Using now() will print the first
# iteration and then every interval. To avoid printing the first
# time, just add print_interval here (i.e. uncomment).
next_print = datetime.now() # + print_interval
for i in range(int(1e8)):
now = datetime.now()
if now >= next_print:
next_print = now + print_interval
print(i)
Also note that before python 3.x xrange would be preferable over range.

Manipulating time in python

In the code shown below, I need to manipulate the time var in python to display a date/time stamp in python to represent that delay.
For example, when the user enters the delay time in hours, I need to set the jcarddeliver var to update itself with the value of the current date/time + delay.
Also it should update the date var as well. For example, if the date is 24 Feb and time is 15:00 hrs and the delay time is 10 hrs, the jcarddeliver date should change to 25 Feb.
jcarddate = time.strftime("%a %m/%d/%y", time.localtime())
jcardtime = time.strftime("%H:%M:%S", time.localtime())
delay = raw_input("enter the delay: ")
jcarddeliver = ??
I just hope I am making sense.
You could try the datetime module, e.g.
import datetime
now = datetime.datetime.now()
delay = float (raw_input ("enter delay (s): "))
dt = datetime.timedelta (seconds=delay)
then = now + dt
print now
print then
The result of time.time() is a floating point value of the number of seconds since the Epoch. You can add seconds to this value and use time.localtime(), time.ctime() and other functions to get the result in various forms:
>>> now = time.time()
>>> time.ctime(now)
'Fri Sep 04 16:19:59 2009' # <-- this is local time
>>> then = now + (10.0 * 60.0 * 60.0) # <-- 10 hours in seconds
>>> time.ctime(then)
'Sat Sep 05 02:19:59 2009'
" i need to set the jcarddeliver var to update itself with the value of the current date/time + delay"
How about reformulating this to
jcarddeliver should be the current date-time plus the delay.
The "update itself" isn't perfectly sensible.
Try the following:
Try the most obvious way of computing "current date-time plus the delay"
print the result.
Try using localtime() on this result. What do you get?
Try this:
now = time.time()
then = now + 365*24*3600
print time.strftime('%Y-%m-%d', time.localtime(then))

Categories

Resources