-
-
Save yasudacloud/def733ac45c3dc8dd7c525a42545a99d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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