示例#1
0
def adjust_datetimes(data, parent=None, key=None):
    """
    find all datetime-like strings within data (deserialized json)
    and format them uniformly, in place.

    """
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(data, basestring):
        if re_loose_datetime.match(data):
            if phone_timezones_should_be_processed():
                parent[key] = json_format_datetime(
                    iso8601.parse_date(data).astimezone(pytz.utc)
                    .replace(tzinfo=None)
                )
            else:
                parent[key] = json_format_datetime(
                    iso8601.parse_date(data).replace(tzinfo=None))

    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value, parent=data, key=key)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value, parent=data, key=i)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data
示例#2
0
文件: util.py 项目: ekush/commcare-hq
def adjust_datetimes(data, parent=None, key=None):
    """
    find all datetime-like strings within data (deserialized json)
    and format them uniformly, in place.

    """
    # this strips the timezone like we've always done
    # todo: in the future this will convert to UTC
    if isinstance(data, basestring) and re_loose_datetime.match(data):
        try:
            matching_datetime = iso8601.parse_date(data)
        except iso8601.ParseError:
            pass
        else:
            if phone_timezones_should_be_processed():
                parent[key] = json_format_datetime(
                    matching_datetime.astimezone(pytz.utc).replace(tzinfo=None)
                )
            else:
                parent[key] = json_format_datetime(
                    matching_datetime.replace(tzinfo=None))

    elif isinstance(data, dict):
        for key, value in data.items():
            adjust_datetimes(value, parent=data, key=key)
    elif isinstance(data, list):
        for i, value in enumerate(data):
            adjust_datetimes(value, parent=data, key=i)

    # return data, just for convenience in testing
    # this is the original input, modified, not a new data structure
    return data
 def test_loose_match(self):
     cases = [
         ('2015-04-03', False),
         ('2013-03-09T06:30:09.007', True),
         ('2013-03-09T06:30:09.007+03', True),
         ('351602061044374', False),
         ('2015-01-01T12:00:00.120054Z', True),
         ('2015-10-01T14:05:45.087434Z', True),
     ]
     for candidate, expected in cases:
         self.assertEqual(bool(re_loose_datetime.match(candidate)), expected, candidate)
示例#4
0
 def test_loose_match(self):
     cases = [
         ('2015-04-03', False),
         ('2013-03-09T06:30:09.007', True),
         ('2013-03-09T06:30:09.007+03', True),
         ('351602061044374', False),
         ('2015-01-01T12:00:00.120054Z', True),
         ('2015-10-01T14:05:45.087434Z', True),
     ]
     for candidate, expected in cases:
         self.assertEqual(bool(re_loose_datetime.match(candidate)),
                          expected, candidate)