Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime: conversion from string with timezone name not working

I have the following string "2017-03-30 08:25:00CET" which I want to convert to a datetime tz-aware object.

According this SO question, from python 3.2 it can be done using only datetime module. In addition, from the documentation, I see

%z |  UTC offset in the form +HHMM or -HHMM (empty string if the object is naive). |  (empty), +0000, -0400, +1030
%Z |  Time zone name (empty string if the object is naive).                        |  (empty), UTC, EST, CST

So I try the following

datetime.strptime(dep_dt, '%Y-%m-%d %H:%M:%S%Z')

I do not get any error, but then the object I get is not tz-aware

datetime.datetime(2017, 3, 30, 8, 25)

On the other hand, if I convert my string to "2017-03-30 08:25:00+0200" and then convert it to an object with

datetime.strptime(dep_dt, '%Y-%m-%d %H:%M:%S%z')

I do get a tz-aware datetime:

datetime.datetime(2017, 3, 30, 8, 25, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))

Any ideas of why it works with %z but not with %Z? What am I doing wrong?

like image 991
J0ANMM Avatar asked Feb 02 '17 10:02

J0ANMM


People also ask

Does Python datetime include timezone?

One of the modules that helps you work with date and time in Python is datetime . With the datetime module, you can get the current date and time, or the current date and time in a particular time zone.

How do I convert a string to a datetime in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I remove Tzinfo from datetime?

To remove timestamp, tzinfo has to be set None when calling replace() function. First, create a DateTime object with current time using datetime. now(). The DateTime object was then modified to contain the timezone information as well using the timezone.

What is Tzinfo datetime?

tzinfo is an abstract base class. It cannot be instantiated directly. A concrete subclass has to derive it and implement the methods provided by this abstract class. The instance of the tzinfo class can be passed to the constructors of the datetime and time objects.


1 Answers

Judging from the information given by tzinfo, the %Z does not work the way that you feed in a string like "UTC" and it will return a datetime instance with the correct timezone.

pytz provides a possible solution to your problem. However, the documentation says, that there is no guarentee that your timezone will be recognized by datetime and recommends working with UTC as long as possible.

Solution using pytz:

from datetime import datetime
from pytz import timezone
dep_dt = '2017-03-30 08:25:00CET'
dt = datetime.strptime(dep_dt, '%Y-%m-%d %H:%M:%S%Z')
timezone(dep_dt[19:]).localize(dt)

Out:

datetime.datetime(2017, 3, 30, 8, 25, tzinfo=<DstTzInfo 'CET' CEST+2:00:00 DST>)
like image 111
UpSampler Avatar answered Oct 26 '22 13:10

UpSampler