Skip to content

Instantly share code, notes, and snippets.

@yasudacloud
Last active July 8, 2025 14:51
Show Gist options
  • Select an option

  • Save yasudacloud/def733ac45c3dc8dd7c525a42545a99d to your computer and use it in GitHub Desktop.

Select an option

Save yasudacloud/def733ac45c3dc8dd7c525a42545a99d to your computer and use it in GitHub Desktop.
import struct
import datetime
# 現在時刻を4バイトに
def current_time_to_byte():
current_time_interval_since_epoch = datetime.datetime.now(datetime.timezone.utc).timestamp()
timestamp_uint32 = int(current_time_interval_since_epoch) & 0xFFFFFFFF
timestamp_data = struct.pack('<I', timestamp_uint32)
return timestamp_data
# 4バイトを文字列に(デバッグ用)
def byte_to_datetime_string(four_bytes: bytes) -> str:
unpacked_uint32_value = struct.unpack('<I', four_bytes)[0]
seconds_from_epoch = unpacked_uint32_value
dt_object = datetime.datetime.fromtimestamp(seconds_from_epoch, tz=datetime.timezone.utc)
return dt_object.strftime("%Y-%m-%d %H:%M:%S (UTC)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment