diff options
-rw-r--r-- | src/pyexiv2/xmp.py | 13 | ||||
-rw-r--r-- | test/xmp.py | 21 |
2 files changed, 30 insertions, 4 deletions
diff --git a/src/pyexiv2/xmp.py b/src/pyexiv2/xmp.py index 469af98..7240527 100644 --- a/src/pyexiv2/xmp.py +++ b/src/pyexiv2/xmp.py @@ -388,18 +388,23 @@ class XmpTag(object): elif type == 'Date': if isinstance(value, datetime.datetime): + if value.tzinfo is None or value.utcoffset() == datetime.timedelta(0): + tz = 'Z' + else: + tz = value.strftime('%z') # of the form ±%H%M + tz = tz[:3] + ':' + tz[3:] if value.hour == 0 and value.minute == 0 and \ value.second == 0 and value.microsecond == 0 and \ - (value.tzinfo is None or value.tzinfo == FixedOffset()): + (value.tzinfo is None or value.utcoffset() == datetime.timedelta(0)): return value.strftime('%Y-%m-%d') elif value.second == 0 and value.microsecond == 0: - return value.strftime('%Y-%m-%dT%H:%M%Z') + return value.strftime('%Y-%m-%dT%H:%M') + tz elif value.microsecond == 0: - return value.strftime('%Y-%m-%dT%H:%M:%S%Z') + return value.strftime('%Y-%m-%dT%H:%M:%S') + tz else: r = value.strftime('%Y-%m-%dT%H:%M:%S.') r += str(int(value.microsecond) / 1E6)[2:] - r += value.strftime('%Z') + r += tz return r elif isinstance(value, datetime.date): return value.isoformat() diff --git a/test/xmp.py b/test/xmp.py index f3fa4c3..532f1e2 100644 --- a/test/xmp.py +++ b/test/xmp.py @@ -34,6 +34,12 @@ from pyexiv2.metadata import ImageMetadata import datetime from testutils import EMPTY_JPG_DATA +# Optional dependency on python-tz, more tests can be run if it is installed +try: + import pytz +except ImportError: + pytz = None + class TestXmpTag(unittest.TestCase): @@ -130,18 +136,24 @@ class TestXmpTag(unittest.TestCase): '2009-02-04') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13), 'Date'), '1999-10-13') + self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3), 'Date'), + '1999-10-13T05:03Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03+05:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03-11:30') + self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27), 'Date'), + '1999-10-13T05:03:27Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03:27Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset('+', 5, 30)), 'Date'), '1999-10-13T05:03:27+05:30') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, tzinfo=FixedOffset('-', 11, 30)), 'Date'), '1999-10-13T05:03:27-11:30') + self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, 124300), 'Date'), + '1999-10-13T05:03:27.1243Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset()), 'Date'), '1999-10-13T05:03:27.1243Z') self.assertEqual(tag._convert_to_string(datetime.datetime(1999, 10, 13, 5, 3, 27, 124300, tzinfo=FixedOffset('+', 5, 30)), 'Date'), @@ -152,6 +164,15 @@ class TestXmpTag(unittest.TestCase): self.failUnlessRaises(XmpValueError, tag._convert_to_string, 'invalid', 'Date') self.failUnlessRaises(XmpValueError, tag._convert_to_string, None, 'Date') + @unittest.skipIf(pytz is None, 'install python-tz to run this test') + def test_convert_to_string_date_with_real_timezones(self): + tag = XmpTag('Xmp.xmp.CreateDate') + self.assertEqual(tag.type, 'Date') + t = pytz.timezone('UTC').localize(datetime.datetime(2011, 2, 2, 10, 52, 4)) + self.assertEqual(tag._convert_to_string(t, 'Date'), '2011-02-02T10:52:04Z') + t = pytz.timezone('CET').localize(datetime.datetime(2011, 2, 2, 10, 52, 4)) + self.assertEqual(tag._convert_to_string(t, 'Date'), '2011-02-02T10:52:04+01:00') + def test_convert_to_python_integer(self): # Valid values tag = XmpTag('Xmp.xmpMM.SaveID') |