0

let's say I have multiple lists of objects (representing log-like record) containing the property returning a timestamp when they were created (it's simple datetime.now() record). My idea was that after I join all these objects to a single list, I sort them chronologically just by their "timestamp". They're not created in parallel (but by multiple class instances recursively), so I expected everytime I call datetime.now(), the record would be unique and sortable. The problem is that it seems the objects are created faster than datetime resolution can be enough, and many of the objects ends with the same "timestamp".

I made a workaround by adding another property "index" to the objects and separate counter, but it increased overall code complexity, I must watch out many places now to not forget counting, had to convert some static functions to instantiable etc... Is there a clean way to create timestamp that is unique for any call, sortable and possibly able to reformat on print output using string.format? Thank you!

  • 1
    If you increase the granularity, the microseconds or etc should be unique. Or just add a random suffix to each number, like `now + timedelta(seconds=random(0, 0.001))` – tripleee Dec 26 '22 at 14:45
  • I would suggest something like this https://stackoverflow.com/questions/2394485/get-posix-unix-time-in-seconds-and-nanoseconds-in-python but I wouldn't be surprised if the GIL gets involved and things could be time-stamped slightly out of sync from what you expect (though the chance of duplicates should be effectively removed) – roganjosh Dec 26 '22 at 15:04
  • Note the ```datetime``` is using the system clock, not a monotonic clock. So system time changes may also throw you off. – Homer512 Dec 26 '22 at 15:11
  • @tripleee Did you mean *uniform* rather than *random*? – DarkKnight Dec 26 '22 at 15:17
  • @Fred No, I did not. The point is to simply make the timestamps unique if you need higher granularity than the system is able to provide. – tripleee Dec 26 '22 at 15:22
  • @tripleee I'm obviously missing something. Where does your *random* function come from? – DarkKnight Dec 26 '22 at 15:26
  • Ah, sorry, I missed your point; yes [`random.uniform`](https://docs.python.org/3/library/random.html#random.uniform) – tripleee Dec 26 '22 at 15:28
  • @triplee: Adding random suffix might make the timestamp unique, but I think it wouldn't be possible to sort items chronologically. – petrak.dan Jan 09 '23 at 14:31
  • @roganjosh: I read and tried the way you linked to, but it has the same problem, clock "refreshes" after like hundreds of nanosecs and the script can do many loops with the same timestamp in the meanwhile. – petrak.dan Jan 09 '23 at 14:34
  • @Homer512: That's a good point to keep in mind in general. But it's "solved" already if I keep the counter. – petrak.dan Jan 09 '23 at 14:45
  • Thank you guys for answers. Another "solution" that crossed my mind is to wait until clock refreshes, but it's kinda bizarre imho, I'll stick with the counter for now, I rather managed to generalize and use just single method for generating the items, that takes care about it. – petrak.dan Jan 09 '23 at 14:56
  • If instead of randomness you add the line number divided by some big number (like 1e18, depending on your timestamp accuracy and available decimal precision), that should produce reliable monolithically increasing time stamps. – tripleee Jan 09 '23 at 16:06

0 Answers0