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

Can you try this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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


Messages In This Thread
Replace last 4 bytes of a timestamp - by mPlummers - Aug-27-2019, 06:13 PM
RE: Replace last 4 bytes of a timestamp - by mPlummers - Sep-16-2019, 09:26 AM

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