Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the Seconds of the time using python

Tags:

python

time

this is my code:

last_time = get_last_time()
now = time.time() - last_time
minute = 
seconds = 
print 'Next time you add blood is '+minute+':'+seconds

Because recovery blood every 5 minutes so only need minute and second

thanks

like image 550
zjm1126 Avatar asked May 19 '11 10:05

zjm1126


People also ask

How do I print time in seconds in Python?

Python time time() MethodPythom time method time() returns the time as a floating point number expressed in seconds since the epoch, in UTC.

What does %% time mean in Python?

%%time is a magic command. It's a part of IPython. %%time prints the wall time for the entire cell whereas %time gives you the time for first line only. Using %%time or %time prints 2 values: CPU Times.

How do you find the time difference between seconds in Python?

To get the difference between two-time, subtract time1 from time2. A result is a timedelta object. The timedelta represents a duration which is the difference between two-time to the microsecond resolution. To get a time difference in seconds, use the timedelta.


2 Answers

This is basic time arithmetics...if you know that a minute has 60 seconds then you could have found that yourself:

minute = int(now / 60)
seconds = int(now % 60)
like image 67
Andreas Jung Avatar answered Oct 05 '22 05:10

Andreas Jung


I believe the difference between two time objects returns a timedelta object. This object has a .total_seconds() method. You'll need to factor these into minutes+seconds yourself:

minutes = total_secs % 60
seconds = total_secs - (minutes * 60)

When you don't know what to do with a value in Python, you can always try it in an interactive Python session. Use dir(obj) to see all of any object's attributes and methods, help(obj) to see its documentation.

Update: I just checked and time.time() doesn't return a time object, but a floating point representing seconds since Epoch. What I said still applies, but you get the value of total_secs in a different way:

total_secs = round(time.time() - last_time)

So in short:

last_time = get_last_time()
time_diff = round(time.time() - last_time)
minute = time_diff / 60
seconds = time_diff % 60  # Same as time_diff - (minutes * 60)
print 'Next time you add blood is '+minute+':'+seconds
like image 42
Zecc Avatar answered Oct 05 '22 03:10

Zecc