i want to write a python code that calculates time - python

If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?
This is what I have tried:
>>> import datetime
>>> t=datetime.time(6,52)
>>> print (t)
06:52:00
>>> b=t+datetime.timedelta (8 hours,15 minutes)
File "<stdin>", line 1
b=t+datetime.timedelta (8 hours,15 minutes)
^
SyntaxError: invalid syntax
````````````````````````````````````````````````````

So let's walk it back a few steps. When you tried b=t+datetime.timedelta (8 hours,15 minutes), what you were trying to do was increment your time object by 8 minutes and 15 seconds, using the timedelta function. In Python, functions take arguments, and timedelta, like any other function, has specific kinds of values you can pass for arguments. You can find them here, because since datetime is a library, it has nice documentation for everything. It looks like you were using IDLE which also gives you a peek at type hints:
So now we know that timedelta takes any one of days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0.
In our case, we want to add 8 minutes and 15 seconds to our original datetime object.
That would mean setting minutes=8 and seconds=15.
So when we call the timedelta function, to avoid that syntax error, we want to invoke the function like such:
b=t+datetime.timedelta(minutes=8, seconds=15)
And there you have it! Just remember what is and is not valid syntax in Python. Python doesn't know a thing about what you mean when you give it plain English.

Related

Call function multiple times with a loop

I have made a function which produces a map, which I will turn into a gif by plotting 24 versions of the map representing the 24 hours in a day. I can call my map the following way.
get_gif(0.0, '00:00:00', '00.png')
to print the next map I will call the following:
get_gif(1.0, '01:00:00', '01.png')
And so on until up to my final call which will look like this:
get_gif(23.0, '23:00:00', '23.png')
How would I make a loop so that I can make all 24 calls in one go? My first argument is a float between 0.0 to 23.0 (the hours of the day), then a string for the time which I am printing in the map, then another string which is the file name.
For python3.6 and above:
for hour in range(24):
get_gif(float(hour), f"{hour:02}:00:00", f"{hour:02}.png")
What you need is a string formatter. In python that would be something like
'{0}.png'.format(n)
In python the '{0}'.format(n) will replace the {0} with the number n. The variable n can be updated with a for loop. Unfortunately it seems that your file contain things like '01.png' so you cannot do
'{0}.png'.format(1)
Because the name will become 1.png. For this reason you can use an if clause and use
'0{0}.png'.format(n)
If n is smaller then 10.

Converting user input to time and substracting it from main time

I am doing some project for myself and i am stuck with an assignment.
I have strict time that's 30min
I have a user that gives an input like: 24008
I need to convert an user input to time(2min:40sec:08milisec) and substract it from main time.
I tried time.strftime('%M:%S:%f', 1800) to show main Time (30min) like 30:00:00 but i don't seem to get the datetime or time imports and how do they work. Same with user input.
Can anyone with a kind heart would guide me to a right path on how to get this logic done and by what function?
I can't share a code because i don't have any working logic for this one.
A datetime.time object would probably be the best data structure to use for this
Your initial value of 30 minutes would be defined like this
import datetime
strict_time = datetime.time(minutes=30)
If the user input you gave as an example will always be the input format then it becomes a little hard to parse as python's datetime module's strptime behaviour does not handle 2-digit millisecond inputs and 1 digit minute inputs. If the input format is exactly the same (5 digits with 1 minute digit, 2 second digits and 2 millisecond digits) then the following would work
user_input = '24008'
input_time = datetime.timedelta(
minutes=int(user_input[0]),
seconds=int(user_input[1:2]),
microseconds=int(user_input[3:4])
)
new_time = strict_time - input_time

Use current time of the day as an integer in python

I am looking to create an if then statement that involves the current time of the day. For example I want something like if it is past 2pm then do this function.
I have tried using the time module but I can't seem to find a way to get just the time of day without the extra stuff like the date. Any help?
Here is a start, and I think it'll be enough for you to get to the answer and use it how you need.
import time
print(time.strftime("%Y-%m-%d %H:%M"))
print(time.strftime("I can format the date and time in many ways. Time is: %H:%M"))
Output (when I ran it):
2017-06-21 10:40
I can format the date and time in many ways. Time is: 10:40

Compare if datetime.timedelta is between two values

I have a datetime.timedelta time object in python (e.g. 00:02:00) I want to check if this time is less than 5 minutess and greater then 1 minute.
I'm not sure how to construct a timedelta object and I'm also not sure if this is the right format to compare times. Would anyone know the most efficient way to do this?
So if you start with a string that's rigorously and precisely in the format 'HH:MM:SS', timedelta doesn't directly offer a string-parsing function, but it's not hard to make one:
import datetime
def parsedelta(hhmmss):
h, m, s = hhmmss.split(':')
return datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
If you need to parse many different variants you'll be better off looking for third-party packages like dateutil.
Once you do have timedelta instance, the check you request is easy, e.g:
onemin = datetime.timedelta(minutes=1)
fivemin = datetime.timedelta(minutes=5)
if onemin < parsedelta('00:02:00') < fivemin:
print('yep')
will, as expected, display yep.

PQt4 QDatetime: Why do I get the same answer when using addSecs() with two different seconds values?

I'm using Python 2.6.6 and PyQt4. I have a start QDateTime object and I am iteratively adding 60 seconds to create a list of every minute within a given time span. I have discovered that there are several cases where adding two different seconds values to the QDateTime object produces the same time.
Here's an example of the problem:
from PyQt4 import QtCore
start = QtCore.QDateTime.fromString("2010-11-01 00:00", "yyyy-MM-dd hh:mm")
print start.addSecs(522540).toString("yyyy-MM-dd hh:mm")
print start.addSecs(526140).toString("yyyy-MM-dd hh:mm")
And the resulting output:
2010-11-07 01:09
2010-11-07 01:09
I've been banging my head on the keyboard trying to figure this out. What am I doing incorrectly?
it probably depends on your locale settings:
seems DST in the United States and other countries ended on 2010-11-07...
so i'd bet it's a result of that.
if you get any strange values from doing calculations with dates, always check if there hasn't been DST change or a leap year and consider different locales. sadly time isn't always as linear as it seems.

Categories