machinery
machinery

Reputation: 6290

Calculate unix timestamps for midnight

I have an interval of Unix timestamps (since 1970) starting from 1593262800000 (2020-06-27) to 1594142579000 (2020-07-07). Now, I would like to calculate all timestamps for midnight in this interval. That means midnight at 2020-06-27, midnight at 2020-06-28 and so on.

How can I do this?

Upvotes: 0

Views: 716

Answers (3)

niaei
niaei

Reputation: 2419

You might consider using Ephem or astropy. Both are used by astronomers.

The best approach would be to calculate JD of Dusk and Dawn twilight time and average them. After that, you can convert from JD to Time again. (This would be the farthest point in time from sunset and sunrise.)

Check this.

Edit: With this approach, the midnight time will change by location.

Upvotes: 0

user13914826
user13914826

Reputation:

Try this:

import datetime
sec_per_day = 24*60*60
timestamp1 = datetime.datetime(2020,6,27).timestamp()
timestamp2 = datetime.datetime(2020,7,07).timestamp()
timestamps = list(range(timestamp1, timestamp2, sec_per_day))
print(timestamps)

Upvotes: 1

Thruston
Thruston

Reputation: 1557

Like this?

import datetime

ms_per_day = 24 * 3600 * 1000

a = 1593262800000
b = 1594142579000

ms_until_midnight = ms_per_day - a % ms_per_day 

for e in range(a + ms_until_midnight, b, ms_per_day):
    print(e, datetime.datetime.utcfromtimestamp(e/1000))

output:

1593302400000 2020-06-28 00:00:00
1593388800000 2020-06-29 00:00:00
1593475200000 2020-06-30 00:00:00
1593561600000 2020-07-01 00:00:00
1593648000000 2020-07-02 00:00:00
1593734400000 2020-07-03 00:00:00
1593820800000 2020-07-04 00:00:00
1593907200000 2020-07-05 00:00:00
1593993600000 2020-07-06 00:00:00
1594080000000 2020-07-07 00:00:00  

Upvotes: 2

Related Questions