What was midnight yesterday as an epoch time?
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.
python datetime
add a comment |
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.
python datetime
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 '13 at 16:15
add a comment |
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.
python datetime
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.
python datetime
python datetime
edited Aug 5 '14 at 7:00
Martijn Pieters♦
717k13925052319
717k13925052319
asked Oct 25 '13 at 16:13
t0mmytt0mmyt
1831312
1831312
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 '13 at 16:15
add a 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.
– Gray
Oct 25 '13 at 16:15
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 '13 at 16:15
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 '13 at 16:15
add a comment |
5 Answers
5
active
oldest
votes
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)):
# ...
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
@sdanzig: By default, thedatetime
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 '13 at 16:29
1
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 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 '13 at 16:34
add a comment |
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
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 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, like23: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 '13 at 17:02
add a comment |
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
add a comment |
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
add a comment |
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
You must use // instead of %
– Mastisa
Jan 1 at 10:10
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19594747%2fwhat-was-midnight-yesterday-as-an-epoch-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)):
# ...
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
@sdanzig: By default, thedatetime
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 '13 at 16:29
1
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 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 '13 at 16:34
add a comment |
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)):
# ...
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
@sdanzig: By default, thedatetime
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 '13 at 16:29
1
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 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 '13 at 16:34
add a comment |
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)):
# ...
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)):
# ...
edited Mar 29 '18 at 17:48
answered Oct 25 '13 at 16:21
Martijn Pieters♦Martijn Pieters
717k13925052319
717k13925052319
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
@sdanzig: By default, thedatetime
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 '13 at 16:29
1
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 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 '13 at 16:34
add a comment |
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
@sdanzig: By default, thedatetime
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 '13 at 16:29
1
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 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 '13 at 16:34
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
How can you tell when midnight is without specifying the timezone?
– sdanzig
Oct 25 '13 at 16:28
@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 '13 at 16:29
@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 '13 at 16:29
1
1
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 at 16:30
In this case it's not an issue as everything runs in UTC.
– t0mmyt
Oct 25 '13 at 16:30
1
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 '13 at 16:34
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 '13 at 16:34
add a comment |
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
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 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, like23: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 '13 at 17:02
add a comment |
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
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 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, like23: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 '13 at 17:02
add a comment |
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
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
answered Oct 25 '13 at 16:36
chepnerchepner
255k34243337
255k34243337
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 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, like23: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 '13 at 17:02
add a comment |
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 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, like23: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 '13 at 17:02
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 at 16:45
I thought about that approach, I was actually just going to mod 86400, but then I remembered leap seconds.
– t0mmyt
Oct 25 '13 at 16:45
1
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 '13 at 17:02
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 '13 at 17:02
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Aug 29 '17 at 16:29
Bill BellBill Bell
15.7k42642
15.7k42642
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 1 at 10:10
MastisaMastisa
1,0962922
1,0962922
add a comment |
add a comment |
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
You must use // instead of %
– Mastisa
Jan 1 at 10:10
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
add a comment |
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
You must use // instead of %
– Mastisa
Jan 1 at 10:10
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
add a comment |
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
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
edited Jan 2 at 5:50
Mastisa
1,0962922
1,0962922
answered Dec 30 '18 at 17:36
ggemmillggemmill
44
44
You must use // instead of %
– Mastisa
Jan 1 at 10:10
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
add a comment |
You must use // instead of %
– Mastisa
Jan 1 at 10:10
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
You must use // instead of %
– Mastisa
Jan 1 at 10:10
You must use // instead of %
– Mastisa
Jan 1 at 10:10
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
Mastisa - many thanks for the correction; I was about to make it myself.
– ggemmill
Jan 1 at 16:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19594747%2fwhat-was-midnight-yesterday-as-an-epoch-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 '13 at 16:15