Python Forum
Replace last 4 bytes of a timestamp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace last 4 bytes of a timestamp
#1
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!!!
Reply
#2
>>> zero_ts = "2019-08-06T07:37:51.358999967Z"
>>> new_timestamp = f"{zero_ts[:-4]}{chr(0xc8)}{chr(0xdc)}{chr(0xdc)}{chr(0x65)}"
Reply
#3
(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"
Reply
#4
What should convert result of 0xc8 be?
Python look it at as Hexadecimal and do convert automatic if try.
>>> 0xc8
200
Reply
#5
(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.
Reply
#6
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
Reply
#7
(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.
Reply
#8
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
Reply
#9
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
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in timestamp Led_Zeppelin 3 3,231 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,011 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Timestamp is undefined ErnestTBass 7 7,958 Feb-16-2019, 08:27 PM
Last Post: snippsat
  replace bytes with other byte or bytes BigOldArt 1 10,641 Feb-02-2019, 11:00 PM
Last Post: snippsat
  Search & Replace - Newlines Added After Replace dj99 3 3,403 Jul-22-2018, 01:42 PM
Last Post: buran
  timestamp not updating bowen73 3 7,199 Aug-20-2017, 11:13 PM
Last Post: bowen73
  Is there a built-in function to replace multiple bytes? Raptor88 4 34,190 Feb-25-2017, 03:48 AM
Last Post: Raptor88
  matplotlib timestamp zero_shubh0 2 6,825 Dec-02-2016, 02:12 PM
Last Post: zero_shubh0

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020