Archived Forum Post

Index of archived forum posts

Question:

Getting Python datetime from Chilkat objects

May 23 '17 at 14:40

I have code that returns a CkSFtpFile object from the SFTP library in Python, but I need to convert the file modified datetime to a standard Python datetime.

Assuming I have CkSFtpFile object which returns a CkDateTime object, how do I convert this to a Python datetime?


Answer

The solution is to find an intermediate format that can be used to construct a Python datetime. One possibility is to use a UTC Unix time.

See http://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date-in-python

You should be able to do it like this:

(Assuming that "ckdt" is a CkDateTime object.)
datetime.datetime.utcfromtimestamp(ckdt.getAsUnixTimeStr(False)).strftime('%Y-%m-%dT%H:%M:%SZ')


Answer

Or like this:

datetime.datetime.utcfromtimestamp(float(ckdt.getAsUnixTimeStr(False))).strftime('%Y-%m-%dT%H:%M:%SZ')