0

I have a problem that seems really easy but I can't figure it out.

I want to achieve the following: Time_as_string - time_now = minutes left until time as string.

I scrape a time from a website as a string, for example: '15:30'.

I want to subtract the current time from this to show how many minutes are left untill the scraped time string.

I tried many things like strftime(), converting to unix timestamp, googling solutions etc. I can make a time object from the string through strftime() but I can't subtract it from the current time.

What is the best way to achieve this?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Martei
  • 5
  • 6
  • what about the date? – Padraic Cunningham Apr 11 '15 at 15:58
  • local time may be non-monotonous and ambiguous. Convert the time to UTC first if you don't want an hour off errors. See [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Apr 11 '15 at 18:00

3 Answers3

1
from datetime import datetime

s = "15:30"
t1 = datetime.strptime(s,"%H:%M")

diff = t1 - datetime.strptime(datetime.now().strftime("%H:%M"),"%H:%M")

print(diff.total_seconds() / 60)
94.0
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • `diff.seconds` ignores `diff.days` -- it is relevant for negative differences. Unnecessary `datetime -> str -> datetime` conversions are ugly. – jfs Apr 11 '15 at 18:16
  • @J.F.Sebastian, the OP has no dates just times so I presumed all times are before t1, who is to say 15:30 or any other time is not three weeks before? – Padraic Cunningham Apr 11 '15 at 18:20
  • (1) OP wants *"minutes left until time"* i.e., `t1` should be in the future (your answer tries to find the reverse). (2) the negative timedelta is represented as `.days=-1, .seconds=large seconds number`. Use `diff.total_seconds()` instead. – jfs Apr 11 '15 at 18:29
  • @J.F.Sebastian, yes my mistake. I had to reverse the logic. – Padraic Cunningham Apr 11 '15 at 18:38
0

If '15:30' belongs to today:

#!/usr/bin/env python3
from datetime import datetime, timedelta

now = datetime.now()
then = datetime.combine(now, datetime.strptime('15:30', '%H:%M').time())
minutes = (then - now) // timedelta(minutes=1)

If there could be midnight between now and then i.e., if then is tomorrow; you could consider a negative difference (if then appears to be in the past relative to now) to be an indicator of that:

while then < now:
    then += timedelta(days=1)
minutes = (then - now) // timedelta(minutes=1)

On older Python version, (then - now) // timedelta(minutes=1) doesn't work and you could use (then - now).total_seconds() // 60 instead.

The code assumes that the utc offset for the local timezone is the same now and then. See more details on how to find the difference in the presence of different utc offsets in this answer.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
-1

The easiest way is probably to subtract two datetimes from each other and use total_seconds():

>>> d1 = datetime.datetime(2000, 1, 1, 20, 00)
>>> d2 = datetime.datetime(2000, 1, 1, 16, 30)
>>> (d1 - d2).total_seconds()
12600.0

Note that this won't work if the times are in different timezones (I just picked January 1, 2000 to make it a datetime). Otherwise, construct two datetimes in the same timezones (or UTC), subtract those and use total_seconds() again to get the difference (time left) in seconds.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180