Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

%Y-%m-%dT%H:%M:%SZ convert to date time object


from datetime import datetime, timedelta, tzinfo

class FixedOffset(tzinfo):
    """offset_str: Fixed offset in str: e.g. '-0400'"""
    def __init__(self, offset_str):
        sign, hours, minutes = offset_str[0], offset_str[1:3], offset_str[3:]
        offset = (int(hours) * 60 + int(minutes)) * (-1 if sign == "-" else 1)
        self.__offset = timedelta(minutes=offset)
        # NOTE: the last part is to remind about deprecated POSIX GMT+h timezones
        # that have the opposite sign in the name;
        # the corresponding numeric value is not used e.g., no minutes
        '<%+03d%02d>%+d' % (int(hours), int(minutes), int(hours)*-1)
    def utcoffset(self, dt=None):
        return self.__offset
    def tzname(self, dt=None):
        return self.__name
    def dst(self, dt=None):
        return timedelta(0)
    def __repr__(self):
        return 'FixedOffset(%d)' % (self.utcoffset().total_seconds() / 60)

date_with_tz = "2017-01-12T14:12:06.000-0500"
date_str, tz = date_with_tz[:-5], date_with_tz[-5:]
dt_utc = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%f")
dt = dt_utc.replace(tzinfo=FixedOffset(tz))
print(dt)

Comment

PREVIOUS NEXT
Code Example
Python :: Python colorbar for circular heatmap 
Python :: weighted averae multiple columns 
Python :: convert to pdf fresh little library that outputs our notebook in a nice LaTex format without installing/doing anything else. 
Python :: MultiValueDictKeyError at /user/register 
Python :: cairo.context transform vertical text python 
Python :: ensure string length 2 python 
Python :: ex: for stopping the while loop after 5 minute in python 
Python :: sklearn random forest feature importance 
Python :: pylesson 
Python :: multipart encoder 
Python :: arrow.get(null) 
Python :: fading hex color python 
Python :: text image thresholding 
Python :: JEW token authentication in Django UTC 
Python :: how to bacome michael reeves in python 
Python :: Grid-Strategy 
Python :: How to derive using sympy 
Python :: x = y < z and z y or y z and z < y python 
Python :: Python Split list into chunks using itertools Method 
Python :: add variable in text python 
Python :: ssl expired python 
Python :: python modules screen 
Python :: custom auth django channels 
Python :: coin flip numpy 
Python :: online python debugger 
Python :: display all rows pandas 
Python :: grandest staircase foobar 
Python :: python how to acquire the html code for a website 
Python :: handle dict invalid key python 
Python :: background subtraction params opencv python 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =