Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining "days" in python when the timedelta.days is less than one

sorry in advance if this is dense. I am trying to find the days since I last posted a tweet. The problem I am running into is when the dates are different, e.g., today and yesterday, but not enough hours have passed to be a full "day."

# "created_at" is part of the Twitter API, returned as UTC time. The 
# timedelta here is to account for the fact I am on the west coast, USA 
lastTweetAt =  result.created_at + timedelta(hours=-8)

# get local time
rightNow = datetime.now()

# subtract the two datetimes (which gives me a timedelta)
dt = rightNow - lastTweetAt

# print the number of days difference
print dt.days

The problem is that if I posted a tweet at say 5 PM yesterday and am running the script at 8 AM today, only 15 hours have passed, which is 0 days. But obviously I want to say it's been 1 day since my last tweet if it was yesterday. And a kludge of adding "+1" isn't going to help because if I have tweeted today, I want the result to be 0.

Is there a better approach than using timedelta to get the difference?


Solution provided by Matti Lyra

The answer is to call .date() on the datetimes so they are converted to coarser date objects (without timestamps). The correct code looks like:

# "created_at" is part of the Twitter API, returned as UTC time.
# the -8 timedelta is to account for me being on the west coast, USA
lastTweetAt =  result.created_at + timedelta(hours=-8)

# get UTC time for right now
rightNow = datetime.now()

# truncate the datetimes to date objects (which have dates, but no timestamp)
# and subtract them (which gives me a timedelta)
dt = rightNow.date() - lastTweetAt.date()

# print the number of days difference
print dt.days
like image 842
psxndc Avatar asked Jan 31 '13 21:01

psxndc


People also ask

How do you get days from Timedelta?

To get the number of days in a time delta, just use the timedelta. days .

How does python calculate Timedelta?

To get a time difference in seconds, use the timedelta. total_seconds() methods. Multiply the total seconds by 1000 to get the time difference in milliseconds. Divide the seconds by 60 to get the difference in minutes.

How does python compare to Timedelta?

You can also find the difference between two datetime objects to get a timedelta object and perform the comparison based on the positive or negative value returned by the timedelta. total_seconds() function. if seconds < 0: print('First date is less than the second date.


2 Answers

How about dealing just with the "date" part of your datetimes?

The part after outputing "0" in the following code:

>>> a = datetime.datetime.now()
>>> b = datetime.datetime.now() - datetime.timedelta(hours=20)
>>> (a-b).days
0
>>> b.date() - a.date()
datetime.timedelta(-1)
>>> (b.date() - a.date()).days
-1
like image 102
BorrajaX Avatar answered Oct 22 '22 00:10

BorrajaX


You can use datetime.date() to compare the two dates (note: NOT dates with times), this truncates the datetime to have an resolution of days not hours.

...
# subtract the two datetimes (which gives me a timedelta)
dt = rightNow.date() - lastTweetAt.date()
...

The docs are always your friend

http://docs.python.org/2/library/datetime#datetime.datetime.date

like image 21
Matti Lyra Avatar answered Oct 21 '22 22:10

Matti Lyra