4

I have some doubts in the dateformat Tue Feb 25 2014 00:00:00 GMT+0530 (IST).

  • does Tue Feb 25 2014 00:00:00 means GMT or IST
  • Is it possible to convert this into python datetime.
  • and also is it possible convert it into format DD-MM-YY,HH:MM:SS in GMT.

Here is what i tried to convert into python datetime::

but i got error,when i tried with %z:

>>> time_format="%a %b %d %Y %H:%M:%S GMT%z (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT+0530 (IST)"
>>> mydate=datetime.strptime(v,time_format)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: 'z' is a bad directive in format '%a %b %d %Y %H:%M:%S GMT%z (%Z)'

But this works:

>>> time_format="%a %b %d %Y %H:%M:%S GMT (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT (IST)"
>>> datetime.strptime(v,time_format)    
datetime.datetime(2014, 2, 25, 0, 0)

but still didn't understand anything about TIMEZONE.

suhailvs
  • 20,182
  • 14
  • 100
  • 98
  • 1
    possible duplicate of [How do I translate a ISO 8601 datetime string into a Python datetime object?](http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object) – Martijn Pieters Mar 18 '14 at 16:05
  • @MartijnPieters so what does `Tue Feb 25 2014 00:00:00` means? `GMT` or `IST` – suhailvs Mar 18 '14 at 16:07
  • IST. The offset is given *relative to* GMT. The Indian Standard Time timezone is 5 hours, 30 minutes east of Greenwich Main Time. – Martijn Pieters Mar 18 '14 at 16:08
  • @MartijnPieters but i got error,when i tried with `%z` which is mentioned in [this](http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior) documentation – suhailvs Mar 18 '14 at 16:16
  • I linked you to a duplicate; there are no timezone definitions in the standard Python library. Note that `%z` is also handled at the C level, which knows even less about Python and timezone support (although I heard Python 3.2 and up handle that case better). – Martijn Pieters Mar 18 '14 at 16:19
  • @MartijnPieters so i want to use `python-dateutil`? does it worth it beacuse it needs to download a python library for just to parse. – suhailvs Mar 18 '14 at 16:50
  • The library gives you timezones *and* painless date/time string parsing. It works directly on your input. Yes, you want to use `python-dateutil`. – Martijn Pieters Mar 18 '14 at 16:52
  • Are you trying to map IST to a *time zone aware `DateTime`*? Make sure you understand "naive" vs "aware" as described [in the docs](http://docs.python.org/2/library/datetime.html). Also, are you using [pytz](http://pytz.sourceforge.net/)? If so, you probably want the `Asia/Kolkata` time zone. If not, you can (in this special case) create a time zone with a fixed +0530 offset. But don't try to apply that in the general case, since many time zones go through changes for daylight saving time. – Matt Johnson-Pint Mar 18 '14 at 23:55

2 Answers2

5

In the system terminal

easy_install python-dateutil

In the python shell

from dateutil import parser as date_parser
print date_parser.parse("Tue Feb 25 2014 00:00:00 GMT+0530 (IST)")

Dateutil's parse can typically just parse anything you throw at it into a Python datetime.

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
2

I think the best way would be to try something like this, especially if you want to avoid using external libraries for example in Python 3.6.9 :

datetime.strptime(v,"%a %b %d %Y %H:%M:%S %Z%z (IST)")
Elias
  • 743
  • 7
  • 16