def test_iptc_date(self): # datetime d = datetime.datetime(1899, 12, 31) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23, 59) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23, 59, 59) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23, 59, 59, 999999) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset()) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(1899, 12, 31, 23, 59, 59, tzinfo=FixedOffset(hours=5)) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.datetime(2011, 8, 8, 19, 3, 37) self.assertEqual(DateTimeFormatter.iptc_date(d), '2011-08-08') # date d = datetime.date(1899, 12, 31) self.assertEqual(DateTimeFormatter.iptc_date(d), '1899-12-31') d = datetime.date(2011, 8, 8) self.assertEqual(DateTimeFormatter.iptc_date(d), '2011-08-08') # invalid type self.assertRaises(TypeError, DateTimeFormatter.iptc_date, None) self.assertRaises(TypeError, DateTimeFormatter.iptc_date, 3.14)
def _convert_to_string(self, value): """Convert one value to its corresponding string representation, suitable to pass to libexiv2. Args: value -- the value to be converted Return: the value converted to its corresponding string representation Raise IptcValueError: if the conversion fails """ if self.type == 'Short': if isinstance(value, int): return str(value) else: raise IptcValueError(value, self.type) elif self.type == 'String': if isinstance(value, str): try: return value.encode('utf-8') except UnicodeEncodeError: raise IptcValueError(value, self.type) elif isinstance(value, bytes): return value else: raise IptcValueError(value, self.type) elif self.type == 'Date': if isinstance(value, (datetime.date, datetime.datetime)): return DateTimeFormatter.iptc_date(value) else: raise IptcValueError(value, self.type) elif self.type == 'Time': if isinstance(value, (datetime.time, datetime.datetime)): return DateTimeFormatter.iptc_time(value) else: raise IptcValueError(value, self.type) elif self.type == 'Undefined': if isinstance(value, str): return value else: raise IptcValueError(value, self.type) raise IptcValueError(value, self.type)
def _convert_to_string(self, value): """ Convert one value to its corresponding string representation, suitable to pass to libexiv2. :param value: the value to be converted :return: the value converted to its corresponding string representation :rtype: string :raise IptcValueError: if the conversion fails """ if self.type == 'Short': if isinstance(value, int): return str(value) else: raise IptcValueError(value, self.type) elif self.type == 'String': if isinstance(value, unicode): try: return value.encode('utf-8') except UnicodeEncodeError: raise IptcValueError(value, self.type) elif isinstance(value, str): return value else: raise IptcValueError(value, self.type) elif self.type == 'Date': if isinstance(value, (datetime.date, datetime.datetime)): return DateTimeFormatter.iptc_date(value) else: raise IptcValueError(value, self.type) elif self.type == 'Time': if isinstance(value, (datetime.time, datetime.datetime)): return DateTimeFormatter.iptc_time(value) else: raise IptcValueError(value, self.type) elif self.type == 'Undefined': if isinstance(value, str): return value else: raise IptcValueError(value, self.type) raise IptcValueError(value, self.type)
def test_iptc_date_invalid(bad_ts): with pytest.raises(TypeError): DateTimeFormatter.iptc_date(bad_ts)
def test_iptc_date_valid(formatted, ts): assert DateTimeFormatter.iptc_date(ts) == formatted