aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier@tilloy.net>2007-01-16 22:53:18 +0100
committerOlivier Tilloy <olivier@tilloy.net>2007-01-16 22:53:18 +0100
commit8a4fb37b49d78a42e5051b6e5b3b3300ba56b33f (patch)
treeec96d994a314a3fdc935fe2a284cd343439c5e6b
parentb1631e6c1d3c6638d1449c03e09a7c52aae6e503 (diff)
downloadpyexiv2-8a4fb37b49d78a42e5051b6e5b3b3300ba56b33f.tar.gz
Implemented utility function StringToTime(string) (could be improved but should work fine).
See the first item of the todo list about exiv2's format for IPTC dates and times.
-rw-r--r--src/pyexiv2.py50
-rw-r--r--todo11
2 files changed, 55 insertions, 6 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py
index 05a8e4a..846684e 100644
--- a/src/pyexiv2.py
+++ b/src/pyexiv2.py
@@ -168,15 +168,59 @@ def StringToTime(string):
Keyword arguments:
string -- the string potentially containing a time
"""
+
+ # TODO: for now, and if the function manages to correctly parse a time,
+ # the returned result is a list of 3 elements
+ # (localTime, offsetSign, offsetTime).
+ # A more elegant solution would be to use a tzinfo object for the
+ # representation of the offset
+ # (http://docs.python.org/lib/datetime-tzinfo.html).
+ # A minimal tzinfo concrete subclass must be implemented...
+
+ # First pass: process the offset (value added to the local time to arrive
+ # at UTC) separately.
+ p = string.rfind('+')
+ if p != -1:
+ # the timezone is west of the Prime Meridian (Greenwich)
+ localtime = string[:p]
+ offset = string[(p + 1):]
+ offsetSign = '+'
+ else:
+ p = string.rfind('-')
+ if p != -1:
+ # the timezone is east of the Prime Meridian
+ localtime = string[:p]
+ offset = string[(p + 1):]
+ offsetSign = '-'
+ else:
+ # no offset found, assume it is nil
+ localtime = string
+ offset = '0000'
+ offsetSign = '+'
+ if len(offset) != 4:
+ # the format of the offset is unknown, assume it is nil
+ offsetTime = datetime.time(0, 0)
+ else:
+ offsetTime = datetime.time(int(offset[:2]), int(offset[2:]))
+
+ # Second pass: process the local time.
+
# Possible formats to try
# According to the IPTC specification
# [http://www.iptc.org/std/IIM/4.1/specification/IIMV4.1.pdf], the only
# accepted format for a string field representing a time is
- # '%H%M%S±%H%M', but it seems that others formats can be found in the wild,
+ # '%H%M%S' (not taking into account the offset since it has been already
+ # processed), but it seems that others formats can be found in the wild,
# so this list could be extended to include new exotic formats.
+ formats = ['%H%M%S', '%H:%M:%S']
- # TODO: a little bit trickier, since time.strptime() does not support
- # the second part of the time format (difference with UTC timezone).
+ for format in formats:
+ try:
+ t = time.strptime(localtime, format)
+ return datetime.time(*t[3:6]), offsetSign, offsetTime
+ except ValueError:
+ # the tested format does not match, do nothing
+ pass
# none of the tested formats matched, return the original string unchanged
return string
diff --git a/todo b/todo
index 7826cd0..5ea6e4a 100644
--- a/todo
+++ b/todo
@@ -1,5 +1,10 @@
todo list
-- High-level (Python types) IPTC tags handling: implement method setIptcTagValue()
-- Add extensive unit tests
-- Document the binding
+- Investigate on how the IPTC date and time formats are handled by exiv2: it
+ seems that they use an specific internal format (that would be, namely,
+ '%Y-%m-%d' and '%H:%M:%S[±%H:%M]'). It would, in a way, make the parsing of
+ dates and times much simpler.
+- High-level (Python types) IPTC tags handling: implement method
+ setIptcTagValue().
+- Add extensive unit tests.
+- Document the binding.