There are two questions:
- Find whether you need "13:00 tomorrow" or "13:00 the day after tomorrow" depending on the current time relative to
20:00
- Convert the result e.g., "13:00 tomorrow" to POSIX time
The first one is simple:
#!/usr/bin/env python
import datetime as DT
current_time = DT.datetime.now()
one_or_two = 1 if current_time.time() < DT.time(20, 0) else 2
target_date = current_time.date() + DT.timedelta(days=one_or_two)
target_time = DT.datetime.combine(target_date, DT.time(13, 0))
Note: 00:00
is considered to be less than 20:00
. You might want to use time intervals instead e.g., 20:00-8:00
vs. 8:00-20:00
, to find out whether the current time is in between.
The second question is essentially the same as How do I convert local time to UTC in Python? In the general case, there is no exact answer e.g., the same local time may occur twice or it may be missing completely—what answer to use depends on the specific application. See more details in my answer to python converting string in localtime to UTC epoch timestamp.
To get the correct result taking into account possible DST transitions or other changes in the local UTC offset between now and the target time (e.g., "the day after tomorrow"):
import pytz # $ pip install pytz
import tzlocal # $ pip install tzlocal
epoch = DT.datetime(1970,1,1, tzinfo=pytz.utc)
local_timezone = tzlocal.getlocalzone()
timezone_aware_dt = local_timezone.localize(target_time, is_dst=None)
posix_time = (timezone_aware_dt - epoch).total_seconds()
Note: is_dst=None
is used to assert that the given local time exists and unambiguous e.g., there is no DST transitions at 13:00 in your local time.
If time.mktime()
has access to a historical timezone database on your platform (or you just don't care about changes in the local utc offset) then you could find the "epoch" time using only stdlib:
import time
unix_time = time.mktime(target_time.timetuple())
You can read more details on when it fails and how to workaround it in Find if 24 hrs have passed between datetimes - Python.
Or more than you probably wanted to know about finding POSIX timestamp in Python can be found in Converting datetime.date to UTC timestamp in Python.