Python Forum

Full Version: Replace last 4 bytes of a timestamp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello!

I have a task which i can't solve, i need some help. I have a epoch timestamp in this form:
zero_ts = "2019-08-06T07:37:51.358999967Z"
. I also have 4 bytes:
new_bytes = [0xc8, 0xdc, 0xdc, 0x65]
. To create a new timestamp i have to replace the last 4 bytes of the timestamp "zero_ts" with the ones in "new_bytes". Then i have to obtain a new timestamp with the same pattern of the original. How can i do this?
I don't think it's necessary to convert the zero timestamp in a unix time value, i think it's enough to convert the string, replace the bytes and re-convert to string Think

PS. In another forum, they suggested me to follow the same logic of this snippet, written in another language, but i don't know how to handle with bytes at this level:
new_timestamp = int64(((old_timestamp*1000)&^0xFFFFFFFF + int64(time)) / 1000)  # Not python!!!
>>> zero_ts = "2019-08-06T07:37:51.358999967Z"
>>> new_timestamp = f"{zero_ts[:-4]}{chr(0xc8)}{chr(0xdc)}{chr(0xdc)}{chr(0x65)}"
(Aug-27-2019, 07:39 PM)Larz60+ Wrote: [ -> ]
>>> zero_ts = "2019-08-06T07:37:51.358999967Z"
>>> new_timestamp = f"{zero_ts[:-4]}{chr(0xc8)}{chr(0xdc)}{chr(0xdc)}{chr(0x65)}"

Thank you! unfortunately the result with your snippet is:
new_timestamp = "2019-08-06T07:37:51.358999ÈÜÜe"
What should convert result of 0xc8 be?
Python look it at as Hexadecimal and do convert automatic if try.
>>> 0xc8
200
(Aug-27-2019, 08:49 PM)snippsat Wrote: [ -> ]What should convert result of 0xc8 be?
Python look it at as Hexadecimal and do convert automatic if try.
>>> 0xc8
200

The 4 bytes are extracted from an encoded string, and the function presented them as hex. I agree with you that there should not be any difference.
Hello,

Do you think it's the response:

from datetime import datetime
old_date = "2019-08-06T07:37:51.358999Z"
new_bytes = [0xc8, 0xdc, 0xdc, 0x65]
t = datetime.strptime(old_date, "%Y-%m-%dT%H:%M:%S.%fZ")
t = t.timestamp()
t = int(t) * 100000
print(t)
t = hex(t)
size_new_bytes = len(new_bytes) * 2
t = t[:-size_new_bytes]
for b in new_bytes:
    t += hex(b)[2:]
print(t)
t = int(t, 16)
print(t)
t = datetime.fromtimestamp(t / 100000)
t = datetime.strftime(t, "%Y-%m-%dT%H:%M:%S.%fZ")
print(t)
I get:

156506987100000
0x8e57c8dcdc65
156507683216485
2019-08-06T09:33:52.164850Z
Best Regards,

Nicolas TATARENKO
(Aug-27-2019, 09:57 PM)avorane Wrote: [ -> ]Hello,

Do you think it's the response:

from datetime import datetime
old_date = "2019-08-06T07:37:51.358999Z"
new_bytes = [0xc8, 0xdc, 0xdc, 0x65]
t = datetime.strptime(old_date, "%Y-%m-%dT%H:%M:%S.%fZ")
t = t.timestamp()
t = int(t) * 100000
print(t)
t = hex(t)
size_new_bytes = len(new_bytes) * 2
t = t[:-size_new_bytes]
for b in new_bytes:
    t += hex(b)[2:]
print(t)
t = int(t, 16)
print(t)
t = datetime.fromtimestamp(t / 100000)
t = datetime.strftime(t, "%Y-%m-%dT%H:%M:%S.%fZ")
print(t)
I get:

156506987100000
0x8e57c8dcdc65
156507683216485
2019-08-06T09:33:52.164850Z
Best Regards,

Nicolas TATARENKO

Thank you Nicolas! The code works, the problem is that you have deleted the last 4 digits in the timestamp. In fact the new timestamp should differ from the old one by milliseconds. I know that 9 digits for decimal seconds are not common, how can i do the same thing without cutting digits from the old timestamp?

@Nicolas, i think that the 4 bytes have been created considering thoose 9 digits for second decimals. That's why the difference between the two timestamps is so big: cutting 3 digits the code increased second, minutes, hours instead of decimals.
hex is only a representation.

any number or character can be displayed as decimal, hex, octal or any number base desired.
the ASCII character 'A' can be represented as:

common representations
base 10 (Decimal): 65
base 2 (Binary): 0100 0001
base 8 (Octal): 101
base 16 (hex): 41

or in obscure representations:
base 3: 2102
base 5: 230
base 20: 35

It's all the same number
Hello,

Can you try this?

from datetime import datetime
import math

old_date = "2019-08-06T07:37:51.358999986Z"
print("old date: {}".format(old_date))
splited_date = old_date.split('.')
old_date_without_milliseconds = splited_date[0]
old_date_milliseconds = splited_date[1][:-1]
print("old_date_without_milliseconds: {}".format(old_date_without_milliseconds))
print("old_date_milliseconds: {}".format(old_date_milliseconds))
new_bytes = [0xc8, 0xdc, 0xac, 0xa1]
t = datetime.strptime(old_date_without_milliseconds, "%Y-%m-%dT%H:%M:%S")
t = t.timestamp()
print("Time stamp from old_date_without_milliseconds: {}".format(t))
t = int(t) * 1000000000 + int(old_date_milliseconds)
print("Time stamp total * 1000000000: {}".format(t))
t = hex(t)
print("Time stamp total in hex: {}".format(t))
size_new_bytes = len(new_bytes) * 2
t = t[:-size_new_bytes]
for b in new_bytes:
    t += hex(b)[2:]
print("Time stamp in hex after substitution: {}".format(t))
t = int(t, 16)
print("Time stamp after substitution: {}".format(t))
partie_entiere = math.floor((t / 1000000000)) * 1000000000
decimals = t - partie_entiere
t = datetime.fromtimestamp(partie_entiere / 1000000000)
t = datetime.strftime(t, "%Y-%m-%dT%H:%M:%S.") + str(decimals) + "Z"
print("new date: {}".format(t))
Best Regards,

Nicolas TATARENKO
OK, I ran it.
It seems you are trying to do something very simple and making it very complicated.
Could you please tell me what the goal is,

starting value, amount of adjustment wanted, to (milliseconds?)
if you provide this, I can show hoe to simplify

It looks like you are trying to emulate 'C' using python.
Pages: 1 2