Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ISO time (ISO 8601) in Python

I have a file. In Python, I would like to take its creation time, and convert it to an ISO time (ISO 8601) string while preserving the fact that it was created in the Eastern Time Zone (ET).

How do I take the file's ctime and convert it to an ISO time string that indicates the Eastern Time Zone (and takes into account daylight savings time, if necessary)?

like image 865
Joseph Turian Avatar asked Jan 27 '10 22:01

Joseph Turian


People also ask

How do I convert a date to ISO 8601 in Python?

To get an ISO 8601 date in string format in Python 3, you can simply use the isoformat function. It returns the date in the ISO 8601 format. For example, if you give it the date 31/12/2017, it'll give you the string '2017-12-31T00:00:00'.

What is ISO format in Python?

The isoformat() method returns the date value of a Python datetime. date object in ISO 8601 format. The standard ISO 8601 is about date formats for Gregorian calendar. It prescribes that a calendar date to be represented using a 4-digit year followed by a two-digit month and a two-digit date. i.e., YYYY-MM-DD.

How do I set timezone in ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).


2 Answers

Local to ISO 8601:

import datetime datetime.datetime.now().isoformat() >>> 2020-03-20T14:28:23.382748 

UTC to ISO 8601:

import datetime datetime.datetime.utcnow().isoformat() >>> 2020-03-20T01:30:08.180856 

Local to ISO 8601 without microsecond:

import datetime datetime.datetime.now().replace(microsecond=0).isoformat() >>> 2020-03-20T14:30:43 

UTC to ISO 8601 with TimeZone information (Python 3):

import datetime datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() >>> 2020-03-20T01:31:12.467113+00:00 

UTC to ISO 8601 with Local TimeZone information without microsecond (Python 3):

import datetime datetime.datetime.now().astimezone().replace(microsecond=0).isoformat() >>> 2020-03-20T14:31:43+13:00 

Local to ISO 8601 with TimeZone information (Python 3):

import datetime datetime.datetime.now().astimezone().isoformat() >>> 2020-03-20T14:32:16.458361+13:00 

Notice there is a bug when using astimezone() on utc time. This gives an incorrect result:

datetime.datetime.utcnow().astimezone().isoformat() #Incorrect result 

For Python 2, see and use pytz.

like image 73
estani Avatar answered Sep 16 '22 16:09

estani


Here is what I use to convert to the XSD datetime format:

from datetime import datetime datetime.now().replace(microsecond=0).isoformat() # You get your ISO string 

I came across this question when looking for the XSD date time format (xs:dateTime). I needed to remove the microseconds from isoformat.

like image 40
radtek Avatar answered Sep 17 '22 16:09

radtek