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
#11
(Aug-29-2019, 09:59 AM)Larz60+ Wrote: 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.

Exactly, i'm quite sure this can be done easily with C, but my whole program is in Python and i'd like to try in this way. I have a timestamp in that form (readable string with year, month etc and with decimal seconds with 9 digits!). I have been told that, to find the next timestamp, i have to "replace the last 4 bytes with the ones i gave you". Now, i don't know if the bytes are referred to the timestamp or the related unix time. The only thing i know is that the two timestamps are very close to each other, order of milliseconds/seconds.
It seems a naive tecnique to me, i didn't find anything similar around the web.

Now i'm going to try the Nicolas code, let's see what happen!

(Aug-28-2019, 09:12 PM)avorane Wrote: 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

Hi Nicolas, thank you again. Now it seems to work, but i gave you the bytes in the inverse order. I put the in the opposite order and it works!

I'm curious to see what @Larz60+ would solve the problem. As i said, I only know that i have to replace the last 4 bytes of the timestamp with the ones they gave me. I'm quite sure there's a way to solve it without using unix time.
Reply
#12
Quote:Exactly, i'm quite sure this can be done easily with C, but my whole program is in Python
I wasn't implying that you should use 'C', just that it looked like you were trying to emulate 'C'.

Quote:I have been told that, to find the next timestamp, i have to "replace the last 4 bytes with the ones i gave you".
I've never heard of such a thing. Where did you get such information?
timestamp is taken from the clock, trying to determine what the clock will return for a timestamp makes no sense, therefore, whoever told you this is taking you for a ride.
Reply
#13
(Aug-28-2019, 09:12 PM)avorane Wrote: 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

Hi Nicolas,

i'm back, i'm still stucked on this problem. But now i have an additional information: the new timestamps should differ from the others by a time close to 1 second. It's not always exactly a second, but it's a good way to test if the code works. Do you think this info may be useful to find out the solution? Also, with your code, sometimes the result is a timestamp with 1973 year, instead of 2019.
Thank you again for your support
Reply
#14
This will create a timestamp with current time (less fraction):
>>> import time
>>> def get_ts_filename(base, suffix):
...     return(f"{base}{int(time.time())}.{suffix}")
... 
>>> get_ts_filename('Jimbo', 'txt')
'Jimbo1568631640.txt'
>>>
Reply
#15
(Aug-30-2019, 10:05 AM)Larz60+ Wrote:
Quote:Exactly, i'm quite sure this can be done easily with C, but my whole program is in Python
I wasn't implying that you should use 'C', just that it looked like you were trying to emulate 'C'.

Quote:I have been told that, to find the next timestamp, i have to "replace the last 4 bytes with the ones i gave you".
I've never heard of such a thing. Where did you get such information?
timestamp is taken from the clock, trying to determine what the clock will return for a timestamp makes no sense, therefore, whoever told you this is taking you for a ride.

This method is useful when you have to collect and send payloads with multiple datapoints taken at different times in a short period. In this case a single payload has ony one complete timestamp, and each datapoint has his own 4 bytes to replace to the payload timestamp. The timestamp is like a reference epoch. It's possible to do this operation in C/Java, but i can't see how can i do this in Python.
Reply
#16
Hello,

When I try the code with new bytes that you hage given, i get a difference near 1 second (~1.1718)(WARNING: new bytes in the code are different of the question).

Can you give me the dates where the year is 1973, please?

Best Regards,

Nicolas TATARENKO
Reply
#17
The timestamp is current time, so will change every second (every time called).
see: https://pymotw.com/3/time/
Reply
#18
I found the solution using a more "Pythonic" method using Pandas:

aa= bytearray(new_bytes) 
temp = struct.unpack('<f', aa)
t = int(float(str(temp)[1:13])*1000000000)
rem_t = t/1000
rem_t = str(rem_t-int(rem_t))[1:]
rem_t = int(float(rem_t)*1000)

old_date = pd.Timestamp(old_date)
new_date = old_date - pd.Timedelta(t, 'ns') 

new_date = pd.datetime.strftime(new_date, "%Y-%m-%dT%H:%M:%S.%f")+str(rem_t)+"Z"
Thank you for all your support.
Reply
#19
Yes indeed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in timestamp Led_Zeppelin 3 3,224 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,009 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  Timestamp is undefined ErnestTBass 7 7,954 Feb-16-2019, 08:27 PM
Last Post: snippsat
  replace bytes with other byte or bytes BigOldArt 1 10,629 Feb-02-2019, 11:00 PM
Last Post: snippsat
  Search & Replace - Newlines Added After Replace dj99 3 3,397 Jul-22-2018, 01:42 PM
Last Post: buran
  timestamp not updating bowen73 3 7,194 Aug-20-2017, 11:13 PM
Last Post: bowen73
  Is there a built-in function to replace multiple bytes? Raptor88 4 34,168 Feb-25-2017, 03:48 AM
Last Post: Raptor88
  matplotlib timestamp zero_shubh0 2 6,819 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