If your data is in raw binary format like bytearray(b'Y\xd5\xb2\xb0') instead of ASCII, we need to interpret each byte as a direct value rather than a character. Here's how to handle this format:
Assumptions:
- Each byte represents a single field in the format:
DD MM YY HH MM SS
- The bytes need to be interpreted as raw values (not ASCII characters)
- The year is still in 2-digit format (0-63 for 2000-2063)
Here's the updated code:
def raw_bytes_to_timestamp(date_bytes):
"""
Convert 6-byte raw date array (DD MM YY HH MM SS) to 32-bit timestamp.
Args:
date_bytes: 6-byte array with raw values for each field
Returns:
32-bit integer with packed timestamp
"""
if len(date_bytes) != 6:
raise ValueError(f"Expected 6 bytes, got {len(date_bytes)}")
# Extract raw values
day = date_bytes[0]
month = date_bytes[1]
year = date_bytes[2]
hours = date_bytes[3]
minutes = date_bytes[4]
seconds = date_bytes[5]
# Validate ranges (optional but recommended)
if not (0 <= day <= 31 and
0 <= month <= 12 and
0 <= year <= 63 and
0 <= hours <= 23 and
0 <= minutes <= 59 and
0 <= seconds <= 59):
raise ValueError("Invalid date/time values")
# Pack into 32-bit integer
timestamp = (
(year & 0x3F) << 26 | # 6 bits for year
(month & 0x0F) << 22 | # 4 bits for month
(day & 0x1F) << 17 | # 5 bits for day
(hours & 0x1F) << 12 | # 5 bits for hour
(minutes & 0x3F) << 6 | # 6 bits for minutes
(seconds & 0x3F) # 6 bits for seconds
)
return timestamp
# Example usage with your data
raw_data = bytearray(b'Y\xd5\xb2\xb0') # 89, 213, 178, 176
# Map to DD MM YY HH MM SS format (assuming little-endian order)
# Let's assume the correct mapping is:
# [89, 213, 178, 176] -> [DD, MM, YY, HH, MM, SS] needs clarification
# For demonstration, let's assume the correct mapping is:
# raw_data[0] = seconds
# raw_data[1] = minutes
# raw_data[2] = hours
# raw_data[3] = day
# (we need 6 bytes total, so this example won't work directly)
# Correct example with 6 bytes:
correct_raw_data = bytearray([21, 5, 25, 5, 11, 44]) # 21-05-25 05:11:44
timestamp = raw_bytes_to_timestamp(correct_raw_data)
print(f"Input (raw): {correct_raw_data}")
print(f"Timestamp: {timestamp} (0x{timestamp:08X})")
# Verify unpacking
def unpack_timestamp(timestamp):
seconds = timestamp & 0x3F
minutes = (timestamp >> 6) & 0x3F
hours = (timestamp >> 12) & 0x1F
day = (timestamp >> 17) & 0x1F
month = (timestamp >> 22) & 0x0F
year = ((timestamp >> 26) & 0x3F) + 2000
return {
'year': year,
'month': month,
'day': day,
'hours': hours,
'minutes': minutes,
'seconds': seconds
}
print("\nUnpacked values:")
for key, value in unpack_timestamp(timestamp).items():
print(f"{key}: {value}")
Important Notes:
- Your example
bytearray(b'Y\xd5\xb2\xb0') has only 4 bytes, but we need 6 bytes for the DD MM YY HH MM SS format.
- If your actual data format is different (e.g., different byte order or fewer fields), you'll need to adjust the byte extraction logic.
- The example above assumes a 6-byte input where each byte directly maps to a field.
If your actual format is different, please provide:
- The exact number of bytes
- The correct mapping of bytes to fields (e.g., which byte represents which field)
- Any specific range constraints for each field
For example, if your 4-byte format is [seconds, minutes, hours, day] and you need to derive month/year from other sources, let me know!