diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-02-25 09:36:39 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-02-25 09:36:39 +0100 |
commit | 87e717129934903f87048db77cab9fd0b440757c (patch) | |
tree | b68d1620fb1deaa76918a3b0b44aacaa00545c17 | |
parent | 943641e68b1b0d50262c8008fe6a3e61151f48a2 (diff) | |
download | pyexiv2-87e717129934903f87048db77cab9fd0b440757c.tar.gz |
IPTC Date to string conversion.
-rw-r--r-- | src/pyexiv2.py | 7 | ||||
-rw-r--r-- | unittest/iptc.py | 15 |
2 files changed, 22 insertions, 0 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index 2cda133..d3814e1 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -554,6 +554,13 @@ class IptcTag(MetadataTag): else: raise IptcValueError(value, xtype) + elif xtype == 'Date': + if type(value) in (datetime.date, datetime.datetime): + # ISO 8601 date format + return value.strftime('%Y%m%d') + else: + raise IptcValueError(value, xtype) + # TODO: other types raise IptcValueError(value, xtype) diff --git a/unittest/iptc.py b/unittest/iptc.py index 2d747d1..dd633d7 100644 --- a/unittest/iptc.py +++ b/unittest/iptc.py @@ -86,6 +86,21 @@ class TestIptcTag(unittest.TestCase): self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-10-32', xtype) self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-02-24T22:12:54', xtype) + def test_convert_to_string_date(self): + xtype = 'Date' + # Valid values + self.assertEqual(IptcTag._convert_to_string(datetime.date(2009, 2, 4), xtype), + '20090204') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(1999, 10, 13), xtype), + '19991013') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2009, 2, 4), xtype), + '20090204') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2009, 2, 4, 10, 52, 37), xtype), + '20090204') + # Invalid values + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, 'invalid', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_string, None, xtype) + def test_convert_to_python_time(self): xtype = 'Time' # Valid values |