From 07b77b0d18d9c7c90e89804791d5d78dc13017e6 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Wed, 25 Feb 2009 09:59:32 +0100 Subject: IPTC Time to string conversion. --- src/pyexiv2.py | 11 +++++++++++ unittest/iptc.py | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/pyexiv2.py b/src/pyexiv2.py index d3814e1..90ef36e 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -561,6 +561,17 @@ class IptcTag(MetadataTag): else: raise IptcValueError(value, xtype) + elif xtype == 'Time': + if type(value) in (datetime.time, datetime.datetime): + r = value.strftime('%H%M%S') + if value.tzinfo is not None: + r += value.strftime('%z') + else: + r += '+0000' + return r + else: + raise IptcValueError(value, xtype) + # TODO: other types raise IptcValueError(value, xtype) diff --git a/unittest/iptc.py b/unittest/iptc.py index dd633d7..70eb977 100644 --- a/unittest/iptc.py +++ b/unittest/iptc.py @@ -118,6 +118,32 @@ class TestIptcTag(unittest.TestCase): self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '21:12:98+00:00', xtype) self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, '081242+0000', xtype) + def test_convert_to_string_time(self): + xtype = 'Time' + # Valid values + self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4), xtype), + '105204+0000') + self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, 574), xtype), + '105204+0000') + self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset()), xtype), + '105204+0000') + self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('+', 5, 30)), xtype), + '105204+0530') + self.assertEqual(IptcTag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('-', 4, 0)), xtype), + '105204-0400') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4), xtype), + '105204+0000') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, 478), xtype), + '105204+0000') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset()), xtype), + '105204+0000') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('+', 5, 30)), xtype), + '105204+0530') + self.assertEqual(IptcTag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('-', 4, 0)), xtype), + '105204-0400') + # Invalid values + self.failUnlessRaises(IptcValueError, IptcTag._convert_to_python, 'invalid', xtype) + def test_convert_to_python_undefined(self): xtype = 'Undefined' # Valid values -- cgit