Search code examples
python-3.5strptime

Ignore unconverted data from date-time stamp when using strptime


A third party API returns me a CSV data containing date time stamp like this:

dtval = '2016-10-14 05:09:30+00:00'

I have to convert it in the format : mm/dd/yyyy.

Here I'm not sure about last +XX:XX of directive:

datetime.datetime.strptime(dtval, "%Y-%m-%d %H:%M:%S+XX:XX").strftime('%m/%d/%Y') 

I tried followings but did not work:

>>>datetime.datetime.strptime('2016-10-14 05:09:30+00:00', '%Y-%m-%d %H:%M:%S')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 340, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: +00:00


>>>datetime.datetime.strptime('2016-10-14 05:09:30+00:00', "%Y-%m-%d %H:%M:%S%z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/usr/lib/python3.5/_strptime.py", line 337, in _strptime
    (data_string, format))
ValueError: time data '2016-10-14 05:09:30+00:00' does not match format '%Y-%m-%d %H:%M:%S+%z'

Is there any option in the Python3.4+'s datetime module to ignore remainging unconverted data?

I gone through this but did not find any such option


Solution

  • After a little bit research I found this fix in django's source code :

    class FixedOffset(tzinfo):
        """
        Fixed offset in minutes east from UTC. Taken from Python's docs.
        Kept as close as possible to the reference version. __init__ was changed
        to make its arguments optional, according to Python's requirement that
        tzinfo subclasses can be instantiated without arguments.
        """
    
        def __init__(self, offset=None, name=None):
            if offset is not None:
                self.__offset = timedelta(minutes=offset)
            if name is not None:
                self.__name = name
    
        def utcoffset(self, dt):
            return self.__offset
    
        def tzname(self, dt):
            return self.__name
    
        def dst(self, dt):
            return timedelta(0)
        def get_timezone(offset):
            """
            Returns a tzinfo instance with a fixed offset from UTC.
            """
            if isinstance(offset, timedelta):
                offset = offset.seconds // 60
            sign = '-' if offset < 0 else '+'
            hhmm = '%02d%02d' % divmod(abs(offset), 60)
            name = sign + hhmm
            return FixedOffset(offset, name)
    
    
    def custom_strptime(self, value):
        """Parses a string and return a datetime.datetime.
        This function supports time zone offsets. When the input contains one,
        the output uses a timezone with a fixed offset from UTC.
        Raises ValueError if the input is well formatted but not a valid datetime.
        Returns None if the input isn't well formatted.
        """
        datetime_re = re.compile(
            r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})'
            r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})'
            r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?'
            r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$'
        )
        match = datetime_re.match(value)
        if match:
            kw = match.groupdict()
            if kw['microsecond']:
                kw['microsecond'] = kw['microsecond'].ljust(6, '0')
            tzinfo = kw.pop('tzinfo')
            if tzinfo == 'Z':
                tzinfo = utc
            elif tzinfo is not None:
                offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
                offset = 60 * int(tzinfo[1:3]) + offset_mins
                if tzinfo[0] == '-':
                    offset = -offset
                tzinfo = get_timezone(offset)
                kw = {k: int(v) for k, v in kw.items() if v is not None}
    
            kw['tzinfo'] = tzinfo
            return datetime.datetime(**kw)