Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raldone01/5819c989a1cb9e611a2b424b73af6c14 to your computer and use it in GitHub Desktop.
Save raldone01/5819c989a1cb9e611a2b424b73af6c14 to your computer and use it in GitHub Desktop.
This script shows you how far a msb truncated unix timestamp gets you.
import datetime
def unix_to_date(timestamp):
"""Converts a Unix timestamp to a timezone-aware, human-readable date."""
return datetime.datetime.fromtimestamp(timestamp, datetime.UTC).strftime('%Y-%m-%d %H:%M:%S')
# Initialize a Unix timestamp with all bits unset (value 0)
current_value = 0
# Get the current time as a Unix timestamp
current_timestamp = datetime.datetime.now(datetime.UTC).timestamp()
highest_bit = int(current_timestamp).bit_length() - 1
current_timestamp = 1<<highest_bit
print(f"Offset: {unix_to_date(current_timestamp)} unix: {current_timestamp} highest bit: {int(current_timestamp).bit_length() - 1}")
# Iterate over all 64 bits (LSB to MSB)
for i in range(64):
# Set the i-th bit
current_value |= (1 << i)
# Count of bits set so far: i+1
set_bit_count = i + 1
# Convert the timestamp to a readable date, if within valid range
try:
end_date = unix_to_date(current_value)
end_date_offset = unix_to_date(current_value + current_timestamp)
except (OverflowError, ValueError):
end_date = "Out of range"
end_date_offset = "Out of range"
# Print the set bit count and the corresponding date
print(f"{set_bit_count} Unix normal: {end_date} Unix offset: {end_date_offset}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment