What was midnight yesterday as an epoch time?
Asked Answered
Q

8

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.

Quaff answered 25/10, 2013 at 16:13 Comment(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.Camilla
L
70

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)):
    # ...
Lyrebird answered 25/10, 2013 at 16:21 Comment(6)
How can you tell when midnight is without specifying the timezone?Checkers
@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.Lyrebird
In this case it's not an issue as everything runs in UTC.Quaff
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.Checkers
instead of datetime.fromtimestamp, couldn't you just do your midnight.timestamp()Rhodic
@hounded: You can of course compare UNIX timestamps, but datetime objects are way more versatile, not to mention debug friendly.Lyrebird
J
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
Joanajoane answered 30/12, 2018 at 17:36 Comment(2)
You must use // instead of %Matsu
Mastisa - many thanks for the correction; I was about to make it myself.Joanajoane
T
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
Turfman answered 25/10, 2013 at 16:36 Comment(2)
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.Quaff
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).Turfman
P
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()
Palliate answered 24/9, 2020 at 6:14 Comment(0)
M
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://mcmap.net/q/81048/-convert-python-datetime-to-epoch-with-strftime for the latter two expressions.)

>>> (midnight_yesterday-arrow.get(1970,1,1)).total_seconds()
1502755200.0
Mareah answered 29/8, 2017 at 16:29 Comment(0)
M
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
Matsu answered 1/1, 2019 at 10:10 Comment(0)
S
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)

Shortsighted answered 13/3, 2019 at 18:36 Comment(0)
B
0

For python3:

from datetime import datetime, timedelta

now = datetime.now()
today = datetime(now.year,now.month,now.day)
yesterday = today - timedelta(days=1)

then, for epoch timestamps:

today_epoch = today.timestamp()
yesterday_epoch = yesterday.timestamp()

The function datetime.today() still returns the current time like now() does.

Bicolor answered 19/7, 2024 at 21:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.