Reputation: 1235
I have to get a datetime
from a float using Python. I am tryint to use the following code:
import time
print (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(float(63650571169.50261))))
But when executing this code, I get an error:
OSError: [Errno 22] Invalid argument
The sys.float_info.max
displays 1.7976931348623157e+308
.
How do I convert this float number into a datetime
?
Upvotes: 4
Views: 3547
Reputation: 49812
It looks like your OS is using the ISO 8601:2004 (clause 4.3.2.1 The Gregorian calendar) with the epoch at Year 0. This can be converted by applying the correct zero offset as:
Code:
import datetime as dt
# define some constants
epoch = dt.datetime(1970, 1, 1)
gregorian_8601_to_unix_epoch = 62167305600
def gregorian_8601_to_datetime(gregorian_8601_seconds):
# get number of seconds from epoch
from_epoch = gregorian_8601_seconds - gregorian_8601_to_unix_epoch
# convert to python datetime
return epoch + dt.timedelta(seconds=from_epoch)
Test Code:
print(gregorian_8601_to_datetime(63650571169.50261))
Results:
2017-01-01 10:12:49.502609
Upvotes: 1
Reputation: 5774
Try this :
import datetime
print datetime.datetime.fromtimestamp(63650571169.50261)
#3987-01-03 05:12:49.502609
Upvotes: 0