diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-02-24 09:49:17 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-02-24 09:49:17 +0100 |
commit | d28e750d9810c7ddd89335efc50e591158c26da0 (patch) | |
tree | 17c468b7d80b1fc79824134d992d570631140f7f | |
parent | 6cb7e04dbde3309e575223ec5dc09cc85e61ff11 (diff) | |
download | pyexiv2-d28e750d9810c7ddd89335efc50e591158c26da0.tar.gz |
IPTC Date conversion.
-rw-r--r-- | src/pyexiv2.py | 12 | ||||
-rw-r--r-- | unittest/iptc.py | 14 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index b4d783f..bf78535 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -494,6 +494,18 @@ class IptcTag(MetadataTag): except TypeError: raise IptcValueError(value, xtype) + elif xtype == 'Date': + # According to the IPTC specification, the format for a string field + # representing a date is '%Y%m%d'. However, the string returned by + # exiv2 using method DateValue::toString() is formatted using + # pattern '%Y-%m-%d'. + format = '%Y-%m-%d' + try: + t = time.strptime(value, format) + return datetime.date(*t[:3]) + except ValueError: + raise IptcValueError(value, xtype) + # TODO: other types raise NotImplementedError('IPTC conversion for type [%s]' % xtype) diff --git a/unittest/iptc.py b/unittest/iptc.py index 035b435..c339da0 100644 --- a/unittest/iptc.py +++ b/unittest/iptc.py @@ -26,6 +26,7 @@ import unittest from pyexiv2 import IptcTag, IptcValueError +import datetime class TestIptcTag(unittest.TestCase): @@ -51,4 +52,17 @@ class TestIptcTag(unittest.TestCase): # Invalid values self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, None, xtype) + def test_convert_to_python_date(self): + xtype = 'Date' + # Valid values + self.assertEqual(IptcTag._convert_to_python('1999-10-13', xtype), + datetime.date(1999, 10, 13)) + # Invalid values + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'invalid', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '11/10/1983', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '-1000', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-02', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-10-32', xtype) + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '2009-02-24T22:12:54', xtype) + # TODO: other types |