Get tz offset from the sting

I have a date that is in local time:

date: "2013-12-02 22:00:00"

      

and another tz value:

timezone_offset: "GMT-0800"

      

If I:, dateutil.parser.parse(date).isoformat()

I get:

"2013-12-02T22:00:00+0000"

      


I want to implement date

in ISO format with tz information and get the result:

"2013-12-02T22:00:00-0800"

      

Something close to parse(date,tzinfos=??).isoformat()

:? How can I get tzinfo from string timezone_offset

?

+1


source to share


2 answers


>>> from dateutil.parser import parse
>>> dt = parse("2013-12-02 22:00:00" + "GMT+0800")
>>> dt.isoformat()
'2013-12-02T22:00:00-08:00'

      

Note: the sign is canceled.

You can also do this using just stdlib:

>>> from datetime import datetime
>>> dt = datetime.strptime("2013-12-02 22:00:00", "%Y-%m-%d %H:%M:%S")
>>> dt = dt.replace(tzinfo=FixedOffset(-8*60, "GMT+0800"))
>>> dt.isoformat()
'2013-12-02T22:00:00-08:00'

      



where taken from the docs : FixedOffset

datetime

from datetime import tzinfo, timedelta

class FixedOffset(tzinfo):
    """Fixed offset in minutes east from UTC."""

    def __init__(self, offset, name):
        self.__offset = timedelta(minutes = offset)
        self.__name = name

    def utcoffset(self, dt):
        return self.__offset

    def tzname(self, dt):
        return self.__name

    def dst(self, dt):
        return timedelta(0)

      

Here, using pytz

module
:

>>> from datetime import datetime
>>> import pytz
>>> dt = datetime.strptime("2013-12-02 22:00:00", "%Y-%m-%d %H:%M:%S")
>>> dt = pytz.timezone('Etc/GMT+8').localize(dt)
>>> dt.isoformat()
'2013-12-02T22:00:00-08:00'

      

0


source


Here are two approaches you could use:

>>> import datetime
>>> dtnow = datetime.datetime.now();dtutcnow = datetime.datetime.utcnow()
>>> dtnow
datetime.datetime(2013, 11, 12, 9, 10, 48, 404000)
>>> dtutcnow
datetime.datetime(2013, 11, 12, 15, 10, 48, 404000)
>>> delta = dtnow - dtutcnow
>>> delta
datetime.timedelta(-1, 64800)
>>> hh,mm = divmod((delta.days * 24*60*60 + delta.seconds + 30) // 60, 60)
>>> hh,mm
(-6, 0)
>>> "%s%+02d:%02d" % (dtnow.isoformat(), hh, mm)
'2013-11-12T09:10:48.404000-6:00'

      

Or that:

>>> import datetime, pytz # 3rd Party
>>> datetime.datetime.now(pytz.timezone('US/Central')).strftime('%Y-%m-%dT%H:%M:%S.%f%z')
'2013-11-12T09:15:20.688000-0600'
>>> 

      



The main advantage of the second method is that your timestring is "timezone aware". From the docs :

There are two types of date and time objects: "naive" and "lucid". This distinction refers to whether the object has any concept of time zone, daylight saving time, or other algorithmic or political time. Whether a naive datetime object represents Coordinated Universal Time (UTC), local time, or the time in some other time zone depends only on the program, just as it did before the program whether a particular number is in signs, miles, or mass. naive datetime objects are easy to understand and work with, at the cost of ignoring some aspect of reality.

Hope this helps!

0


source







All Articles