30

I'm trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure out whether that event happened between midnight and midnight of yesterday.

t = time.time() # is now
t2 = 1234567890 # some arbitrary time from my log

24 hours ago is t - 86400, but how can I round that up and down to midnight. I'm having real trouble finding a way to get timestamps in and out of datetime or then manipulating a datetime to set the time.

1
  • 1
    Sounds like you may have to convert it to regular time somewhere in there, since dividers like days/midnight/etc are not a part of epoch times.
    – Gray
    Oct 25, 2013 at 16:15

7 Answers 7

69

In the Middle of the Night

Generating the last midnight is easy:

from datetime import datetime, time

midnight = datetime.combine(datetime.today(), time.min)

That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.

Yesterday

With a timedelta() you can calculate the previous midnight:

from datetime import timedelta

yesterday_midnight = midnight - timedelta(days=1)

That Was Yesterday

Now test if your timestamp is in between these two points:

timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
if yesterday_midnight <= timestamp < midnight:
    # this happened between 00:00:00 and 23:59:59 yesterday

All Together Now

Combined into one function:

from datetime import datetime, time, timedelta

def is_yesterday(timestamp):
    midnight = datetime.combine(datetime.today(), time.min)
    yesterday_midnight = midnight - timedelta(days=1)
    return yesterday_midnight <= timestamp < midnight:

if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
    # ...
6
  • 1
    How can you tell when midnight is without specifying the timezone?
    – sdanzig
    Oct 25, 2013 at 16:28
  • 1
    @sdanzig: By default, the datetime module is not timezone aware. It can return local or UTC times, as provided by the OS. This code uses local time.
    – Martijn Pieters
    Oct 25, 2013 at 16:29
  • 1
    In this case it's not an issue as everything runs in UTC.
    – t0mmyt
    Oct 25, 2013 at 16:30
  • 1
    If everything runs UTC, then not only is it fine, but there's a smart developer behind it. Just to be aware though, the concept of "yesterday" will only apply to what England considers yesterday. If you wanted to do something based on the timing at the site where the logs were written, for instance, you'd have to specify the timezone accordingly.
    – sdanzig
    Oct 25, 2013 at 16:34
  • 1
    @hounded: You can of course compare UNIX timestamps, but datetime objects are way more versatile, not to mention debug friendly.
    – Martijn Pieters
    May 29, 2020 at 20:44
5

Midnight at the start of today is:

midnight = (int(time.time() // 86400)) * 86400

so yesterday's midnight is:

midnight = (int(time.time() // 86400)) * 86400 - 86400
2
  • You must use // instead of %
    – mastisa
    Jan 1, 2019 at 10:10
  • Mastisa - many thanks for the correction; I was about to make it myself.
    – ggemmill
    Jan 1, 2019 at 16:34
2

Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):

days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day

Then, you subtract the leftover seconds from your original timestamp, which produces midnight of the current day.

t -= remaining_seconds

Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.

tomorrow_t = t + 24 * 3600
days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
t = tomorrow_t - remaining_seconds
2
  • I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
    – t0mmyt
    Oct 25, 2013 at 16:45
  • 1
    These timestamps are not affected by leap seconds. When a leap second occurs, the UNIX epoch timestamp simply repeats, so you are guaranteed to have 86400 seconds per day. Repeating a timestamp is just another arbitrary label for the leap second, like 23:59:60 is an arbitrary label for the leap second between 23:59:59 and 00:00:00 (or whenever leap seconds get applied).
    – chepner
    Oct 25, 2013 at 17:02
2

To get the specific timezone's midnight timestamp:

from datetime import datetime
import pytz

TZ = "Asia/Shanghai"
datetime.now(pytz.timezone(TZ)).replace(hour=0, minute=0, second=0, microsecond=0).timestamp()
0

In my estimation, many date and time manipulations are easier to do, and to understand, using the arrow library. This is one of them.

Create an arbitrary date and time.

>>> import arrow
>>> arbitrary = arrow.get(2017,8,16,11,5)

Calculate midnight_yesterday: first, midnight of arbitrary as its 'day' floor; then shift this back by one day. Display the result.

>>> midnight_yesterday = arbitrary.floor('day').shift(days=-1)
>>> midnight_yesterday 
<Arrow [2017-08-15T00:00:00+00:00]>

Use timestamp for the desired overall result, for Python 3.3+.

>>> midnight_yesterday.datetime.timestamp()
1502755200.0

Or use this expression for Python 2.7. (Credit: https://stackoverflow.com/a/11743262/131187 for the latter two expressions.)

>>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
1502755200.0
0

You can use this code:

import time

seconds_of_day = 24 * 60 * 60  # 86400
last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
yesterday_last_midnight = last_midnight - seconds_of_day
0
import time

start_str = time.strftime( "%m/%d/%Y" ) + " 00:00:00"
end_str = time.strftime( "%m/%d/%Y ") + " 23:59:59"
start_ts = int( time.mktime( time.strptime( start_str, "%m/%d/%Y %H:%M:%S" ) ) )
end_ts = int( time.mktime( time.strptime( end_str, "%m/%d/%Y %H:%M:%S" ) ) )

print (start_ts) # timestamp today at 00:00:00
print (end_ts) # timestamp today at 23:59:59
# 1552435200
# 1552521599

Source Python get unix epoch for today’s midnight and today’s 23:59:59 (start of day, end of day)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.