1

I am attempting to compare the current time (A) to a certain time (B) on the current day. Subsequently, based on the result of that comparison, I want to return the POSIX time for a specific time, either tomorrow (C) or the day after tomorrow (D).

To illustrate, an example:

Suppose we have the current time (A) to be 12:00. I want to compare that to (B), which is 20:00 today.

Since A < B, I want to return 13:00 tomorrow, formatted as a POSIX time.

Another example:

Now suppose the current time (A) is 21:00. I still want to compare that to (B), which is 20:00 today.

Since A > B, I want to return 13:00 the day after tomorrow, again formatted as a POSIX time.

I've been trying to make this work using the timeand datetime libraries, but when using time I have a hard time finding B and when using datetime I can't find a way to return C and D as a POSIX time.

Have I correctly identified which libraries I should use and if so, what am I missing?

Cronax
  • 355
  • 1
  • 5
  • 17

2 Answers2

2

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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0
import datetime

A = datetime.datetime.now()
# A = A.replace(hour=21, minute=5)  # This line simlulates "after 21:00"
B = A.replace(hour=20, minute=0, second=0, microsecond=0)  # 20:00 today

if A < B:
    print A.replace(hour=13, minute=0, second=0, microsecond=0) + datetime.timedelta(days=1)  # 13:00 tomorrow
else:
    print A.replace(hour=13, minute=0, second=0, microsecond=0) + datetime.timedelta(days=2)  # 13:00 the day after

in Python 3 you can then return

X.timestamp()

in Python 2 you can use

def timestamp(dt):
    return (dt - datetime.datetime(1970, 1, 1)).total_seconds()

timestamp(X)
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • Very close, but I think that the final step should be to add 1 or 2 days to `B.replace(hour=13)`, not `A.replace(hours=13)`. That will give you the time `13:00:00`, not whatever minute, second and microsecond values happen to be in `A`. – mhawke May 18 '16 at 11:23
  • Giving this a whirl now, I did note there seems to be an extra `)` in the Python 2 part. – Cronax May 18 '16 at 12:15
  • This is almost working, but not quite. Unless I change it to A = A... it doesn't update A, meaning it doesn't work. I'll accept the answer as soon as everything works as written. – Cronax May 18 '16 at 12:40
  • I don't follow... `A = A...`? Where do you need `A` to be updated? – Nils Werner May 18 '16 at 15:51
  • the `timestamp()` function is wrong if the input is the local time (as in the question) unless the local timezone is utc . – jfs May 18 '16 at 21:33