diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2010-12-22 18:16:01 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2010-12-22 18:16:01 +0100 |
commit | 59ee4d6f36aaaea03dc9ffcacaf338903392db06 (patch) | |
tree | 1fa65e44127ce84c529a82e10bba6fb969d00229 | |
parent | 6569f59296eb4ac29123c0c7adfead5929978acb (diff) | |
download | pyexiv2-59ee4d6f36aaaea03dc9ffcacaf338903392db06.tar.gz |
Fix the IPTC Time type to string conversion,
the string expected by exiv2's TimeValue::read(string) does not respect the IPTC specification
('%H:%M:%S±%H:%M' instead of '%H%M%S±%H%M).
-rw-r--r-- | src/pyexiv2/iptc.py | 13 | ||||
-rw-r--r-- | test/iptc.py | 20 |
2 files changed, 19 insertions, 14 deletions
diff --git a/src/pyexiv2/iptc.py b/src/pyexiv2/iptc.py index 1e5429e..128f77e 100644 --- a/src/pyexiv2/iptc.py +++ b/src/pyexiv2/iptc.py @@ -346,11 +346,16 @@ class IptcTag(ListenerInterface): elif self.type == 'Time': if isinstance(value, (datetime.time, datetime.datetime)): - r = value.strftime('%H%M%S') - if value.tzinfo is not None: - r += value.strftime('%z') + # According to the IPTC specification, the format for a string + # field representing a time is '%H%M%S±%H%M'. However, the + # string expected by exiv2's TimeValue::read(string) should be + # formatted using pattern '%H:%M:%S±%H:%M'. + r = value.strftime('%H:%M:%S') + if value.tzinfo is not None and \ + not (value.tzinfo.hours == 0 and value.tzinfo.minutes == 0): + r += value.strftime('%Z') else: - r += '+0000' + r += '+00:00' return r else: raise IptcValueError(value, self.type) diff --git a/test/iptc.py b/test/iptc.py index ead0f47..9cf198e 100644 --- a/test/iptc.py +++ b/test/iptc.py @@ -137,25 +137,25 @@ class TestIptcTag(unittest.TestCase): tag = IptcTag('Iptc.Envelope.TimeSent') self.assertEqual(tag.type, 'Time') self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4)), - '105204+0000') + '10:52:04+00:00') self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, 574)), - '105204+0000') + '10:52:04+00:00') self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset())), - '105204+0000') + '10:52:04+00:00') self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('+', 5, 30))), - '105204+0530') + '10:52:04+05:30') self.assertEqual(tag._convert_to_string(datetime.time(10, 52, 4, tzinfo=FixedOffset('-', 4, 0))), - '105204-0400') + '10:52:04-04:00') self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4)), - '105204+0000') + '10:52:04+00:00') self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, 478)), - '105204+0000') + '10:52:04+00:00') self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset())), - '105204+0000') + '10:52:04+00:00') self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('+', 5, 30))), - '105204+0530') + '10:52:04+05:30') self.assertEqual(tag._convert_to_string(datetime.datetime(2007, 2, 7, 10, 52, 4, tzinfo=FixedOffset('-', 4, 0))), - '105204-0400') + '10:52:04-04:00') # Invalid values self.failUnlessRaises(IptcValueError, tag._convert_to_string, 'invalid') |