0

Converting Unix Epoch time to readable time information. Trials are as per below. It throws an error "OSError: [Errno 22] Invalid argument". Looks like the method does not like the given argument (1693063813031885), but it works great at https://unixtime.org/ where in "Your Time Zone".

import time
    
dt_ts = time.strftime("%m-%d-%Y %H:%M:%S", time.gmtime(1693063813031885))
print(dt_ts)

Output should be 2023-08-26-11:30:13.031_885(UTC-05:00).

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
OO7
  • 350
  • 2
  • 12
  • See [How do I create a datetime in Python from milliseconds?](https://stackoverflow.com/q/748491/10197418). Besides, to get a result with UTC offset (UTC-05:00), you'll have to specify a time zone, since Unix time refers to UTC. Be careful: if you don't set a time zone, Python's naive datetime will resemble *local time*. – FObersteiner Aug 27 '23 at 11:06

1 Answers1

1

time.gmtime takes the number of seconds since the epoch as argument, what you have passed are microseconds.

You have to insert a decimal point before (or drop) the last 6 digits.

>>> time.gmtime(1693063813.031885)
time.struct_time(tm_year=2023, tm_mon=8, tm_mday=26, tm_hour=15, tm_min=30, tm_sec=13, tm_wday=5, tm_yday=238, tm_isdst=0)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65