unixtime-anniversaries
The snippet can be accessed without any authentication.
Authored by
clonejo
unixtime-anniversaries.py 4.17 KiB
#!/usr/bin/python3
# Calculate unix timestamps that are multiples of powers of 2 and 10 (or n).
#
# ## Gotchas
# - Displayed times are localtime, unfortunately. Timestamps should be OK
# though.
#
# ```sh
# ./unixtime-anniversaries.py
# ```
#
# ```
# now: 1600348680 2020-09-17 15:18:00.999827
# base 10000000 (10**7):
# last anniversary: 1600000000 = 10**7 * 160 = 2020-09-13 14:26:40
# next anniversary: 1610000000 = 10**7 * 161 = 2021-01-07 07:13:20
# base 100000000 (10**8):
# last anniversary: 1600000000 = 10**8 * 16 = 2020-09-13 14:26:40
# next anniversary: 1700000000 = 10**8 * 17 = 2023-11-14 23:13:20
# base 1000000000 (10**9):
# last anniversary: 1000000000 = 10**9 * 1 = 2001-09-09 03:46:40
# next anniversary: 2000000000 = 10**9 * 2 = 2033-05-18 05:33:20
#
# base 8388608 (2**23):
# last anniversary: 1593835520 = 2**23 * 190 = 2020-07-04 06:05:20
# next anniversary: 1602224128 = 2**23 * 191 = 2020-10-09 08:15:28
# base 16777216 (2**24):
# last anniversary: 1593835520 = 2**24 * 95 = 2020-07-04 06:05:20
# next anniversary: 1610612736 = 2**24 * 96 = 2021-01-14 09:25:36
# base 33554432 (2**25):
# last anniversary: 1577058304 = 2**25 * 47 = 2019-12-23 00:45:04
# next anniversary: 1610612736 = 2**25 * 48 = 2021-01-14 09:25:36
# base 67108864 (2**26):
# last anniversary: 1543503872 = 2**26 * 23 = 2018-11-29 16:04:32
# next anniversary: 1610612736 = 2**26 * 24 = 2021-01-14 09:25:36
# base 134217728 (2**27):
# last anniversary: 1476395008 = 2**27 * 11 = 2016-10-13 23:43:28
# next anniversary: 1610612736 = 2**27 * 12 = 2021-01-14 09:25:36
# base 268435456 (2**28):
# last anniversary: 1342177280 = 2**28 * 5 = 2012-07-13 13:01:20
# next anniversary: 1610612736 = 2**28 * 6 = 2021-01-14 09:25:36
# base 536870912 (2**29):
# last anniversary: 1073741824 = 2**29 * 2 = 2004-01-10 14:37:04
# next anniversary: 1610612736 = 2**29 * 3 = 2021-01-14 09:25:36
# base 1073741824 (2**30):
# last anniversary: 1073741824 = 2**30 * 1 = 2004-01-10 14:37:04
# next anniversary: 2147483648 = 2**30 * 2 = 2038-01-19 04:14:08
#
#
# ```sh
# ./unixtime-anniversaries.py | grep next | sort >> README.md
# ```
#
# ```
# next anniversary: 1602224128 = 2**23 * 191 = 2020-10-09 08:15:28
# next anniversary: 1610000000 = 10**7 * 161 = 2021-01-07 07:13:20
# next anniversary: 1610612736 = 2**24 * 96 = 2021-01-14 09:25:36
# next anniversary: 1610612736 = 2**25 * 48 = 2021-01-14 09:25:36
# next anniversary: 1610612736 = 2**26 * 24 = 2021-01-14 09:25:36
# next anniversary: 1610612736 = 2**27 * 12 = 2021-01-14 09:25:36
# next anniversary: 1610612736 = 2**28 * 6 = 2021-01-14 09:25:36
# next anniversary: 1610612736 = 2**29 * 3 = 2021-01-14 09:25:36
# next anniversary: 1700000000 = 10**8 * 17 = 2023-11-14 23:13:20
# next anniversary: 2000000000 = 10**9 * 2 = 2033-05-18 05:33:20
# next anniversary: 2147483648 = 2**30 * 2 = 2038-01-19 04:14:08
# ```
from datetime import datetime
def anniversaries(now, interval_str):
interval = eval(interval_str)
last_anniversary_factor = int(now.timestamp()) // interval
last_anniversary = datetime.fromtimestamp(last_anniversary_factor * interval)
print(f"base {interval} ({interval_str}):")
print(
f" last anniversary: "
f"{int(last_anniversary.timestamp())} = "
f"{interval_str} * {last_anniversary_factor:>3} = "
f"{last_anniversary}"
)
next_anniversary_factor = int(now.timestamp()) // interval + 1
next_anniversary = datetime.fromtimestamp(next_anniversary_factor * interval)
print(
f" next anniversary: "
f"{int(next_anniversary.timestamp())} = "
f"{interval_str} * {next_anniversary_factor:>3} = "
f"{next_anniversary}"
)
now = datetime.now()
print(f"now: {int(now.timestamp())} {now}")
anniversaries(now, "10**7")
anniversaries(now, "10**8")
anniversaries(now, "10**9")
print()
anniversaries(now, "2**23")
anniversaries(now, "2**24")
anniversaries(now, "2**25")
anniversaries(now, "2**26")
anniversaries(now, "2**27")
anniversaries(now, "2**28")
anniversaries(now, "2**29")
anniversaries(now, "2**30")
Please register or sign in to comment