0

I'm sure this must be answered somewhere, but I can't find it.

How do I print the current local date/time in ISO 8601 format, including the local timezone info?

eg: 2007-04-05T12:30-02:00

In particular (and this is the difference to the other question) - how do I get the local timezone?

Note, I'm stuck at Python 2.5, which may reduce some availability of options.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
xorsyst
  • 7,897
  • 5
  • 39
  • 58
  • Note - I finally found the answer, see this question: http://stackoverflow.com/questions/24575121/in-python-how-to-print-full-iso-8601-timestamp-including-current-timezone – xorsyst Jul 24 '14 at 11:56

1 Answers1

0

Usually this is done via isoformat. It should be available in Python 2.5.

from datetime import datetime
dt = datetime.now()
dt.isoformat("T")

which yields:

'2014-07-03T17:36:23.622683'

If you have correct UTC offsets in your tzinfo this should be rendered aswell.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • What tzinfo? Please explain. – xorsyst Jul 03 '14 at 15:39
  • A Python `datetime` can have a `tzinfo` object (defaults to `None`). See: https://docs.python.org/2/library/datetime.html#datetime.tzinfo It defines the timezone information. – Constantinius Jul 03 '14 at 15:43
  • And how do I get the tzinfo for the current timezone? – xorsyst Jul 03 '14 at 15:56
  • Define "current timezone". You mean the timezone that your machine is currently defined to be in? This depends on your OS. Maybe there are libs to deal with that, but I would not know. – Constantinius Jul 03 '14 at 16:27