diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2007-01-16 22:53:18 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2007-01-16 22:53:18 +0100 |
commit | 8a4fb37b49d78a42e5051b6e5b3b3300ba56b33f (patch) | |
tree | ec96d994a314a3fdc935fe2a284cd343439c5e6b /src/pyexiv2.py | |
parent | b1631e6c1d3c6638d1449c03e09a7c52aae6e503 (diff) | |
download | pyexiv2-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.
Diffstat (limited to 'src/pyexiv2.py')
-rw-r--r-- | src/pyexiv2.py | 50 |
1 files changed, 47 insertions, 3 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 |