1744 |
1744 |
1745 def shortdate(date=None): |
1745 def shortdate(date=None): |
1746 """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
1746 """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
1747 return datestr(date, format='%Y-%m-%d') |
1747 return datestr(date, format='%Y-%m-%d') |
1748 |
1748 |
1749 def parsetimezone(tz): |
1749 def parsetimezone(s): |
1750 """parse a timezone string and return an offset integer""" |
1750 """find a trailing timezone, if any, in string, and return a |
1751 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): |
1751 (offset, remainder) pair""" |
1752 sign = (tz[0] == "+") and 1 or -1 |
1752 |
1753 hours = int(tz[1:3]) |
1753 if s.endswith("GMT") or s.endswith("UTC"): |
1754 minutes = int(tz[3:5]) |
1754 return 0, s[:-3].rstrip() |
1755 return -sign * (hours * 60 + minutes) * 60 |
1755 |
1756 if tz == "GMT" or tz == "UTC": |
1756 # Unix-style timezones [+-]hhmm |
1757 return 0 |
1757 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit(): |
1758 return None |
1758 sign = (s[-5] == "+") and 1 or -1 |
|
1759 hours = int(s[-4:-2]) |
|
1760 minutes = int(s[-2:]) |
|
1761 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip() |
|
1762 |
|
1763 return None, s |
1759 |
1764 |
1760 def strdate(string, format, defaults=[]): |
1765 def strdate(string, format, defaults=[]): |
1761 """parse a localized time string and return a (unixtime, offset) tuple. |
1766 """parse a localized time string and return a (unixtime, offset) tuple. |
1762 if the string cannot be parsed, ValueError is raised.""" |
1767 if the string cannot be parsed, ValueError is raised.""" |
1763 # NOTE: unixtime = localunixtime + offset |
1768 # NOTE: unixtime = localunixtime + offset |
1764 offset, date = parsetimezone(string.split()[-1]), string |
1769 offset, date = parsetimezone(string) |
1765 if offset is not None: |
|
1766 date = " ".join(string.split()[:-1]) |
|
1767 |
1770 |
1768 # add missing elements from defaults |
1771 # add missing elements from defaults |
1769 usenow = False # default to using biased defaults |
1772 usenow = False # default to using biased defaults |
1770 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
1773 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
1771 found = [True for p in part if ("%"+p) in format] |
1774 found = [True for p in part if ("%"+p) in format] |