Search code examples
pythontimestamputc

Unix timestamp conversion to time with microsecond accuracy of seconds


I have 1000 of UYC timestamps in csv file, I want to convert it into date and time but I am only interested in second like

Timestamps= 1666181576.26295, 1666181609.54292

19/10/2022 15:45:25.34568

from that I only have interest in 25.34568 seconds, also the numbers after points. How can I get this type of conversion in python? Mostly the search on the internet is interested in conversation from UTC to time and date but I also want precision in seconds.


Solution

  • from datetime import datetime
    from decimal import Decimal
    ts = 1666181576.26295
    timestamp = datetime.fromtimestamp(ts)
    result = timestamp.second + Decimal(timestamp.microsecond)/1000000
    print(result)
    

    Will result in 56.26295