I'll help you convert that string into a 32-bit integer following the C structure's bit layout.
Here's a Python solution:
python
def string_to_timestamp(date_string):
"""
Convert date string (SSMMHHDDMMYYYY) to 32-bit timestamp.
Args:
date_string: String in format "13211128012025"
(seconds, minutes, hours, day, month, year)
Returns:
32-bit integer with packed timestamp
"""
# Parse the string
seconds = int(date_string[0:2]) # 13
minutes = int(date_string[2:4]) # 21
hours = int(date_string[4:6]) # 11
day = int(date_string[6:8]) # 28
month = int(date_string[8:10]) # 01
year = int(date_string[10:14]) # 2025
# Convert year to 6-bit format (20YY -> 0-63)
year_6bit = year - 2000
# Pack the bits according to the C structure
# The bit layout from LSB to MSB:
# seconds: 6 bits (0-5)
# minutes: 6 bits (6-11)
# hour: 5 bits (12-16)
# day: 5 bits (17-21)
# month: 4 bits (22-25)
# year: 6 bits (26-31)
timestamp = (
(year_6bit & 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
date_string = "13211128012025"
timestamp = string_to_timestamp(date_string)
print(f"Timestamp: {timestamp}")
print(f"Timestamp (hex): 0x{timestamp:08X}")
print(f"Timestamp (binary): 0b{timestamp:032b}")
# Verify by unpacking
def unpack_timestamp(timestamp):
"""Unpack timestamp to verify correctness"""
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
}
# Verify the conversion
unpacked = unpack_timestamp(timestamp)
print("\nVerification:")
for key, value in unpacked.items():
print(f"{key}: {value}")
This code:
Parses the string to extract each component (seconds, minutes, hours, day, month, year)
Converts the year from 4-digit format to the 6-bit format (subtracting 2000)
Packs all components into a 32-bit integer using bitwise operations, following the exact bit layout from your C structure
Includes a verification function to unpack and check the result
The bit masks (0x3F, 0x1F, 0x0F) ensure each field only uses its allocated bits, preventing overflow into adjacent fields.
User
What if the date "string" were a byte array?
Assistant
If the date is a byte array instead of a string, you'll need to handle it slightly differently. Here are a few common scenarios:
python
User
Okay, now assume the read data is reversed. So year is first read and seconds last.
Assistant
Here's the updated code to handle the reversed byte order where year comes first and seconds last:
python
User
We can expect the read data to be twelve bytes.
Basically yymmddhhmmss
Two digits per information. E.g the year yy is 25 for 2025.
The day dd is 13 for the 13th of the month. The minutes mm is 07 for 7 minutes of the hour and so on.
Assistant
Here's the code to handle the 12-byte format with two digits per field:
python
User
So i got the format wrong.
It is actually: ddmmyyhhmmss
Assistant
Here's the updated code for the correct format (DDMMYYHHMMSS):
python
User
Lets say we get this data bytearray(b'210525051144')
What will be the date-time and return value?
Assistant
Let me trace through the conversion with your example data:
python
date_bytes = bytearray(b'210525051144')
# Parsing DDMMYYHHMMSS format:
# DD = 21 (day)
# MM = 05 (month)
# YY = 25 (year 2025)
# HH = 05 (hours)
# MM = 11 (minutes)
# SS = 44 (seconds)
# So the date-time is: 2025-05-21 05:11:44
# Now let's calculate the packed timestamp:
day = 21 # 0x15 in hex, 0b10101 in binary
month = 5 # 0x05 in hex, 0b0101 in binary
year = 25 # 0x19 in hex, 0b011001 in binary
hours = 5 # 0x05 in hex, 0b00101 in binary
minutes = 11 # 0x0B in hex, 0b001011 in binary
seconds = 44 # 0x2C in hex, 0b101100 in binary
# Packing into 32-bit integer:
timestamp = (
(25 & 0x3F) << 26 | # year: 0b011001 shifted left 26 bits
(5 & 0x0F) << 22 | # month: 0b0101 shifted left 22 bits
(21 & 0x1F) << 17 | # day: 0b10101 shifted left 17 bits
(5 & 0x1F) << 12 | # hours: 0b00101 shifted left 12 bits
(11 & 0x3F) << 6 | # minutes: 0b001011 shifted left 6 bits
(44 & 0x3F) # seconds: 0b101100 (no shift)
)
# Let's verify:
def bytes_to_timestamp(date_bytes):
"""
Convert date byte array to 32-bit timestamp.
Args:
date_bytes: Byte array containing date/time data
Returns:
32-bit integer with packed timestamp
"""
# Scenario 1: ASCII bytes (most common for embedded devices)
# b'13211128012025' - each byte is an ASCII character
if len(date_bytes) == 14:
# Convert ASCII bytes to string first
date_string = date_bytes.decode('ascii')
# Parse as before
seconds = int(date_string[0:2])
minutes = int(date_string[2:4])
hours = int(date_string[4:6])
day = int(date_string[6:8])
month = int(date_string[8:10])
year = int(date_string[10:14])
# Scenario 2: BCD (Binary Coded Decimal) - each byte contains two digits
# b'\x13\x21\x11\x28\x01\x20\x25' - 7 bytes total
elif len(date_bytes) == 7:
seconds = (date_bytes[0] >> 4) * 10 + (date_bytes[0] & 0x0F)
minutes = (date_bytes[1] >> 4) * 10 + (date_bytes[1] & 0x0F)
hours = (date_bytes[2] >> 4) * 10 + (date_bytes[2] & 0x0F)
day = (date_bytes[3] >> 4) * 10 + (date_bytes[3] & 0x0F)
month = (date_bytes[4] >> 4) * 10 + (date_bytes[4] & 0x0F)
year = ((date_bytes[5] >> 4) * 10 + (date_bytes[5] & 0x0F)) * 100 + \
((date_bytes[6] >> 4) * 10 + (date_bytes[6] & 0x0F))
# Scenario 3: Raw binary values - each component is one byte
# b'\x0D\x15\x0B\x1C\x01\x19' - 6 bytes (year is already 0-63)
elif len(date_bytes) == 6:
seconds = date_bytes[0]
minutes = date_bytes[1]
hours = date_bytes[2]
day = date_bytes[3]
month = date_bytes[4]
year = date_bytes[5] + 2000 # Assuming year byte is already 0-63
else:
raise ValueError(f"Unexpected byte array length: {len(date_bytes)}")
# Convert year to 6-bit format if needed
if year >= 2000:
year_6bit = year - 2000
else:
year_6bit = year
# Pack the bits
timestamp = (
(year_6bit & 0x3F) << 26 |
(month & 0x0F) << 22 |
(day & 0x1F) << 17 |
(hours & 0x1F) << 12 |
(minutes & 0x3F) << 6 |
(seconds & 0x3F)
)
return timestamp
# Example usage for different byte array formats:
# ASCII bytes (most common)
ascii_bytes = b'13211128012025'
print("ASCII bytes example:")
timestamp = bytes_to_timestamp(ascii_bytes)
print(f"Timestamp: {timestamp} (0x{timestamp:08X})")
# BCD format
import struct
bcd_bytes = bytes([0x13, 0x21, 0x11, 0x28, 0x01, 0x20, 0x25])
print("\nBCD bytes example:")
timestamp = bytes_to_timestamp(bcd_bytes)
print(f"Timestamp: {timestamp} (0x{timestamp:08X})")
# Raw binary format
raw_bytes = bytes([13, 21, 11, 28, 1, 25]) # year 25 = 2025
print("\nRaw binary bytes example:")
timestamp = bytes_to_timestamp(raw_bytes)
print(f"Timestamp: {timestamp} (0x{timestamp:08X})")
# If you know the exact format, you can create a specific function:
def ascii_bytes_to_timestamp(date_bytes):
"""Convert ASCII byte array to timestamp"""
date_string = date_bytes.decode('ascii')
seconds = int(date_string[0:2])
minutes = int(date_string[2:4])
hours = int(date_string[4:6])
day = int(date_string[6:8])
month = int(date_string[8:10])
year = int(date_string[10:14]) - 2000
return (
(year & 0x3F) << 26 |
(month & 0x0F) << 22 |
(day & 0x1F) << 17 |
(hours & 0x1F) << 12 |
(minutes & 0x3F) << 6 |
(seconds & 0x3F)
)
The most common format from embedded devices is ASCII bytes, where each byte represents one ASCII digit character. If you're unsure about the format, you can check:
python
# Debug helper to understand your byte format
def analyze_bytes(date_bytes):
print(f"Length: {len(date_bytes)}")
print(f"Raw bytes: {date_bytes}")
print(f"Hex representation: {date_bytes.hex()}")
print(f"As ASCII (if valid): ", end="")
try:
print(date_bytes.decode('ascii'))
except:
print("Not valid ASCII")
print(f"Individual bytes: {[b for b in date_bytes]}")
# Use this to analyze your actual data
# analyze_bytes(your_byte_array)
Which format does your embedded device use? If you can share a sample of the actual byte data, I can provide a more specific solution.
def reversed_string_to_timestamp(date_string):
"""
Convert reversed date string (YYYYMMDDHHMMSS) to 32-bit timestamp.
Args:
date_string: String in format "20250128112113"
(year, month, day, hours, minutes, seconds)
Returns:
32-bit integer with packed timestamp
"""
# Parse the string in reversed order
year = int(date_string[0:4]) # 2025
month = int(date_string[4:6]) # 01
day = int(date_string[6:8]) # 28
hours = int(date_string[8:10]) # 11
minutes = int(date_string[10:12]) # 21
seconds = int(date_string[12:14]) # 13
# Convert year to 6-bit format (20YY -> 0-63)
year_6bit = year - 2000
# Pack the bits according to the C structure
timestamp = (
(year_6bit & 0x3F) << 26 |
(month & 0x0F) << 22 |
(day & 0x1F) << 17 |
(hours & 0x1F) << 12 |
(minutes & 0x3F) << 6 |
(seconds & 0x3F)
)
return timestamp
# For byte arrays with reversed order
def reversed_bytes_to_timestamp(date_bytes):
"""
Convert reversed date byte array to 32-bit timestamp.
Args:
date_bytes: Byte array with year first, seconds last
Returns:
32-bit integer with packed timestamp
"""
# ASCII bytes: b'20250128112113'
if len(date_bytes) == 14:
date_string = date_bytes.decode('ascii')
year = int(date_string[0:4])
month = int(date_string[4:6])
day = int(date_string[6:8])
hours = int(date_string[8:10])
minutes = int(date_string[10:12])
seconds = int(date_string[12:14])
# BCD format: b'\x20\x25\x01\x28\x11\x21\x13' - 7 bytes
elif len(date_bytes) == 7:
year = ((date_bytes[0] >> 4) * 10 + (date_bytes[0] & 0x0F)) * 100 + \
((date_bytes[1] >> 4) * 10 + (date_bytes[1] & 0x0F))
month = (date_bytes[2] >> 4) * 10 + (date_bytes[2] & 0x0F)
day = (date_bytes[3] >> 4) * 10 + (date_bytes[3] & 0x0F)
hours = (date_bytes[4] >> 4) * 10 + (date_bytes[4] & 0x0F)
minutes = (date_bytes[5] >> 4) * 10 + (date_bytes[5] & 0x0F)
seconds = (date_bytes[6] >> 4) * 10 + (date_bytes[6] & 0x0F)
# Raw binary: b'\x19\x01\x1C\x0B\x15\x0D' - 6 bytes (year already 0-63)
elif len(date_bytes) == 6:
year = date_bytes[0] + 2000
month = date_bytes[1]
day = date_bytes[2]
hours = date_bytes[3]
minutes = date_bytes[4]
seconds = date_bytes[5]
else:
raise ValueError(f"Unexpected byte array length: {len(date_bytes)}")
# Convert year to 6-bit format if needed
if year >= 2000:
year_6bit = year - 2000
else:
year_6bit = year
# Pack the bits
timestamp = (
(year_6bit & 0x3F) << 26 |
(month & 0x0F) << 22 |
(day & 0x1F) << 17 |
(hours & 0x1F) << 12 |
(minutes & 0x3F) << 6 |
(seconds & 0x3F)
)
return timestamp
# Example usage
reversed_string = "20250128112113" # Year first, seconds last
timestamp = reversed_string_to_timestamp(reversed_string)
print(f"String example: {reversed_string}")
print(f"Timestamp: {timestamp} (0x{timestamp:08X})")
# ASCII bytes example
reversed_ascii_bytes = b'20250128112113'
timestamp = reversed_bytes_to_timestamp(reversed_ascii_bytes)
print(f"\nASCII bytes example: {reversed_ascii_bytes}")
print(f"Timestamp: {timestamp} (0x{timestamp:08X})")
# Verify the result
def unpack_timestamp(timestamp):
"""Unpack timestamp to verify correctness"""
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:")
unpacked = unpack_timestamp(timestamp)
for key, value in unpacked.items():
print(f"{key}: {value}")
The main changes are:
Year is now parsed from positions 0-3 (4 digits)
Month from positions 4-5
Day from positions 6-7
Hours from positions 8-9
Minutes from positions 10-11
Seconds from positions 12-13
The bit packing into the 32-bit integer remains the same since the C structure defines how the bits are arranged in the final integer, regardless of the input string format.
def bytes_to_timestamp_12(date_bytes):
"""
Convert 12-byte date array (YYMMDDHHMMSS) to 32-bit timestamp.
Args:
date_bytes: 12-byte array with 2 ASCII digits per field
YY MM DD HH MM SS
Returns:
32-bit integer with packed timestamp
"""
if len(date_bytes) != 12:
raise ValueError(f"Expected 12 bytes, got {len(date_bytes)}")
# Convert bytes to string for easier parsing
date_string = date_bytes.decode('ascii')
# Parse each 2-digit field
year = int(date_string[0:2]) # YY (e.g., 25 for 2025)
month = int(date_string[2:4]) # MM
day = int(date_string[4:6]) # DD
hours = int(date_string[6:8]) # HH
minutes = int(date_string[8:10]) # MM
seconds = int(date_string[10:12]) # SS
# Year is already in 2-digit format (0-99)
# For the 6-bit field, we only need values 0-63
# This covers years 2000-2063
year_6bit = year
# Pack the bits according to the C structure
timestamp = (
(year_6bit & 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
# Alternative if the bytes are BCD or raw values instead of ASCII
def bytes_to_timestamp_12_raw(date_bytes):
"""
Convert 12-byte date array to timestamp (if bytes are raw values, not ASCII).
"""
if len(date_bytes) != 12:
raise ValueError(f"Expected 12 bytes, got {len(date_bytes)}")
# If each byte represents a single digit (0-9)
year = date_bytes[0] * 10 + date_bytes[1]
month = date_bytes[2] * 10 + date_bytes[3]
day = date_bytes[4] * 10 + date_bytes[5]
hours = date_bytes[6] * 10 + date_bytes[7]
minutes = date_bytes[8] * 10 + date_bytes[9]
seconds = date_bytes[10] * 10 + date_bytes[11]
# Pack the bits
timestamp = (
(year & 0x3F) << 26 |
(month & 0x0F) << 22 |
(day & 0x1F) << 17 |
(hours & 0x1F) << 12 |
(minutes & 0x3F) << 6 |
(seconds & 0x3F)
)
return timestamp
# Example usage
# ASCII bytes example: "250113072145" = year 25, month 01, day 13, hour 07, min 21, sec 45
date_bytes = b'250113072145'
timestamp = bytes_to_timestamp_12(date_bytes)
print(f"Input bytes: {date_bytes}")
print(f"Timestamp: {timestamp}")
print(f"Timestamp (hex): 0x{timestamp:08X}")
print(f"Timestamp (binary): 0b{timestamp:032b}")
# Verify by unpacking
def unpack_timestamp(timestamp):
"""Unpack timestamp to verify correctness"""
seconds = timestamp & 0x3F
minutes = (timestamp >> 6) & 0x3F
hours = (timestamp >> 12) & 0x1F
day = (timestamp >> 17) & 0x1F
month = (timestamp >> 22) & 0x0F
year = (timestamp >> 26) & 0x3F
return {
'year': 2000 + year, # Convert back to full year
'month': month,
'day': day,
'hours': hours,
'minutes': minutes,
'seconds': seconds
}
print("\nUnpacked values:")
unpacked = unpack_timestamp(timestamp)
for key, value in unpacked.items():
print(f"{key}: {value}")
# Test with another example
print("\n--- Another example ---")
date_bytes2 = b'251228112113' # 2025-12-28 11:21:13
timestamp2 = bytes_to_timestamp_12(date_bytes2)
print(f"Input bytes: {date_bytes2}")
print(f"Timestamp: {timestamp2} (0x{timestamp2:08X})")
unpacked2 = unpack_timestamp(timestamp2)
print("Unpacked:", unpacked2)
Key points:
The function expects exactly 12 bytes
Each field uses 2 ASCII digits (YY MM DD HH MM SS)
The year is already in 2-digit format (25 for 2025), which fits perfectly in the 6-bit field (0-63)
The 6-bit year field can represent years 2000-2063
If your embedded device sends raw byte values instead of ASCII characters, use the bytes_to_timestamp_12_raw function instead.
def bytes_to_timestamp_12(date_bytes):
"""
Convert 12-byte date array (DDMMYYHHMMSS) to 32-bit timestamp.
Args:
date_bytes: 12-byte array with 2 ASCII digits per field
DD MM YY HH MM SS
Returns:
32-bit integer with packed timestamp
"""
if len(date_bytes) != 12:
raise ValueError(f"Expected 12 bytes, got {len(date_bytes)}")
# Convert bytes to string for easier parsing
date_string = date_bytes.decode('ascii')
# Parse each 2-digit field
day = int(date_string[0:2]) # DD
month = int(date_string[2:4]) # MM
year = int(date_string[4:6]) # YY (e.g., 25 for 2025)
hours = int(date_string[6:8]) # HH
minutes = int(date_string[8:10]) # MM
seconds = int(date_string[10:12]) # SS
# Year is already in 2-digit format (0-99)
# For the 6-bit field, we only need values 0-63
# This covers years 2000-2063
year_6bit = year
# Pack the bits according to the C structure
timestamp = (
(year_6bit & 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
# Alternative if the bytes are raw values instead of ASCII
def bytes_to_timestamp_12_raw(date_bytes):
"""
Convert 12-byte date array to timestamp (if bytes are raw values, not ASCII).
"""
if len(date_bytes) != 12:
raise ValueError(f"Expected 12 bytes, got {len(date_bytes)}")
# If each byte represents a single digit (0-9)
day = date_bytes[0] * 10 + date_bytes[1]
month = date_bytes[2] * 10 + date_bytes[3]
year = date_bytes[4] * 10 + date_bytes[5]
hours = date_bytes[6] * 10 + date_bytes[7]
minutes = date_bytes[8] * 10 + date_bytes[9]
seconds = date_bytes[10] * 10 + date_bytes[11]
# Pack the bits
timestamp = (
(year & 0x3F) << 26 |
(month & 0x0F) << 22 |
(day & 0x1F) << 17 |
(hours & 0x1F) << 12 |
(minutes & 0x3F) << 6 |
(seconds & 0x3F)
)
return timestamp
# Example usage
# ASCII bytes example: "130125072145" = day 13, month 01, year 25, hour 07, min 21, sec 45
date_bytes = b'130125072145'
timestamp = bytes_to_timestamp_12(date_bytes)
print(f"Input bytes: {date_bytes}")
print(f"Timestamp: {timestamp}")
print(f"Timestamp (hex): 0x{timestamp:08X}")
print(f"Timestamp (binary): 0b{timestamp:032b}")
# Verify by unpacking
def unpack_timestamp(timestamp):
"""Unpack timestamp to verify correctness"""
seconds = timestamp & 0x3F
minutes = (timestamp >> 6) & 0x3F
hours = (timestamp >> 12) & 0x1F
day = (timestamp >> 17) & 0x1F
month = (timestamp >> 22) & 0x0F
year = (timestamp >> 26) & 0x3F
return {
'year': 2000 + year, # Convert back to full year
'month': month,
'day': day,
'hours': hours,
'minutes': minutes,
'seconds': seconds
}
print("\nUnpacked values:")
unpacked = unpack_timestamp(timestamp)
for key, value in unpacked.items():
print(f"{key}: {value}")
# Test with another example
print("\n--- Another example ---")
date_bytes2 = b'281225112113' # 28-12-2025 11:21:13
timestamp2 = bytes_to_timestamp_12(date_bytes2)
print(f"Input bytes: {date_bytes2}")
print(f"Timestamp: {timestamp2} (0x{timestamp2:08X})")
unpacked2 = unpack_timestamp(timestamp2)
print("Unpacked:", unpacked2)
# If you want a more readable version with explicit field names
def parse_ddmmyyhhmmss(date_bytes):
"""Parse DDMMYYHHMMSS format with clear field names"""
date_string = date_bytes.decode('ascii')
fields = {
'day': int(date_string[0:2]),
'month': int(date_string[2:4]),
'year': int(date_string[4:6]),
'hours': int(date_string[6:8]),
'minutes': int(date_string[8:10]),
'seconds': int(date_string[10:12])
}
print(f"Parsed fields: {fields}")
# Create timestamp
timestamp = (
(fields['year'] & 0x3F) << 26 |
(fields['month'] & 0x0F) << 22 |
(fields['day'] & 0x1F) << 17 |
(fields['hours'] & 0x1F) << 12 |
(fields['minutes'] & 0x3F) << 6 |
(fields['seconds'] & 0x3F)
)
return timestamp
The main changes:
Day is now parsed from positions 0-1
Month from positions 2-3
Year from positions 4-5
Hours from positions 6-7
Minutes from positions 8-9
Seconds from positions 10-11
The bit packing remains the same since that's determined by the C structure, not the input format.