Now I work almost fully remotely, eliminating the need to spend time commuting. It's great because I can use that time elsewhere, but I'm worried that I'll have less opportunity to move and I can't have time to listen to the podcast. To solve both of them, I started to walk to a supermarket (opening at 8 am!) A little away every morning, but I heard today a podcast called PythonBytes I've been talking about crazy tips from my eyes, so I'll introduce them.
The people talking on the podcast also said, "Well, can you do that?" "I didn't know," but I was surprised to hear that. For those who want to hear directly, 27 of Episode # 190 It's about a minute.
timedelta
is a feature included in the python standard library and is included in the datetime
module. When the datetime
s are subtracted from each other, timedelta
type data is returned.
Call datetime.now ()
twice to get the current time and try to get the difference.
>>> from datetime import datetime, timedelta
>>> d0 = datetime.now()
>>> d1 = datetime.now()
>>> td = d1 - d0
>>> td
datetime.timedelta(seconds=1, microseconds=508102)
The result of the difference is data of type timedelta
, in which case the difference between the two times is 1 second + 508102 microseconds = 1.508102 seconds. Each value can be obtained as an attribute value.
>>> td.days
0
>>> td.seconds
1
>>> td.microseconds
508102
Now consider a slightly larger difference.
>>> d0 = datetime(2020,7,31,22,5,0, 113459)
>>> d1 = datetime(2020,8,3,5,55,26, 93450)
>>> d1 - d0
datetime.timedelta(days=2, seconds=28225, microseconds=979991)
From 22:05: 0.113459 on July 31st to 5:55: 26.093450 on August 3rd. Just subtract the two date times and you'll see that the difference is 2 days and 28225.979991 seconds. In this example, the difference between the dates across the months is taken, and if you do it by hand, it will be troublesome and easy to make a mistake, but it is very helpful that datetime is doing it.
Although it can be calculated easily like this, "2 days and 28225.979991 seconds" is subtly difficult to understand. How many hours is this? The hurdle goes up as soon as I think. The first thing that comes to mind is to take out the attribute value and calculate it.
>>> td.days * 24 + (td.seconds + td.microseconds / 1000000 ) / 60 / 60
55.8405499975
Or you can use the total_seconds ()
method to make it a little easier.
>>> td.total_seconds() / 60 / 60
55.840549997500005
There is a slight error here.
In any case, it's not a difficult calculation, but it's a shame that I can't use it even though I have a special type called timedelta.
But actually, I can. That is the main subject of today, but the solution is very simple: "** Divide by the unit timedelta you want to calculate **".
For example, in the above case
>>> td / timedelta (hours=1)
55.8405499975
Oh, isn't it amazing? I will write a little about why this happens.
Although timedelta has three attribute values, days, seconds, and microseconds, the constructor also supports other time units with keyword arguments.
>>> timedelta(minutes=1)
datetime.timedelta(seconds=60)
>>> timedelta(hours=1)
datetime.timedelta(seconds=3600)
>>> timedelta(weeks=1)
datetime.timedelta(days=7)
As you can see, if specified in a different unit, it will be normalized to days, seconds, microseconds and saved as an attribute of timedelta.
And the division between timedeltas will calculate the ratio of the two time intervals, and the result will be a float (floating point type). Therefore, if it is in "hours", you can get the desired value by dividing by timedelta (hours = 1)
.
By applying this, for example, when you think "How many weeks is 5/6 to 8/19?"
>>> (datetime (2020, 8,19) - datetime (2020, 5, 6)) / timedelta(weeks=1)
15.0
Can be calculated in one shot.
It's been a long time since datetime was introduced in Python, but I've never used timedelta like this before. According to the podcast, this isn't a trick, it seems to be the usage intended by the author of timedelta himself, but if that's the case, I'd like you to make an example in the document or make an effort to spread it a little more ( Lol)
Recommended Posts