Mercurial > public > mercurial-scm > hg
comparison mercurial/util.py @ 6229:c3182eeb70ea
dates: improve timezone handling
datestr:
- add format specifiers %1 and %2 for timezone hours and minutes
- remove timezone and timezone format options
- correctly find timezone hours and minutes for fractional and negative timezones
- update users
strdate:
- correctly find timezone hours and minutes for fractional and negative timezones
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Tue, 11 Mar 2008 17:42:41 -0500 |
parents | 210ee6204a29 |
children | c7253d1a774e |
comparison
equal
deleted
inserted
replaced
6228:c0c4c7b1e8d3 | 6229:c3182eeb70ea |
---|---|
1522 tz = time.altzone | 1522 tz = time.altzone |
1523 else: | 1523 else: |
1524 tz = time.timezone | 1524 tz = time.timezone |
1525 return time.mktime(lt), tz | 1525 return time.mktime(lt), tz |
1526 | 1526 |
1527 def datestr(date=None, format='%a %b %d %H:%M:%S %Y', timezone=True, timezone_format=" %+03d%02d"): | 1527 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): |
1528 """represent a (unixtime, offset) tuple as a localized time. | 1528 """represent a (unixtime, offset) tuple as a localized time. |
1529 unixtime is seconds since the epoch, and offset is the time zone's | 1529 unixtime is seconds since the epoch, and offset is the time zone's |
1530 number of seconds away from UTC. if timezone is false, do not | 1530 number of seconds away from UTC. if timezone is false, do not |
1531 append time zone to string.""" | 1531 append time zone to string.""" |
1532 t, tz = date or makedate() | 1532 t, tz = date or makedate() |
1533 if "%1" in format or "%2" in format: | |
1534 sign = (tz > 0) and "-" or "+" | |
1535 minutes = abs(tz) / 60 | |
1536 format = format.replace("%1", "%c%02d" % (sign, minutes / 60)) | |
1537 format = format.replace("%2", "%02d" % (minutes % 60)) | |
1533 s = time.strftime(format, time.gmtime(float(t) - tz)) | 1538 s = time.strftime(format, time.gmtime(float(t) - tz)) |
1534 if timezone: | |
1535 s += timezone_format % (int(-tz / 3600.0), ((-tz % 3600) / 60)) | |
1536 return s | 1539 return s |
1537 | 1540 |
1538 def shortdate(date=None): | 1541 def shortdate(date=None): |
1539 """turn (timestamp, tzoff) tuple into iso 8631 date.""" | 1542 """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
1540 return datestr(date, format='%Y-%m-%d', timezone=False) | 1543 return datestr(date, format='%Y-%m-%d') |
1541 | 1544 |
1542 def strdate(string, format, defaults=[]): | 1545 def strdate(string, format, defaults=[]): |
1543 """parse a localized time string and return a (unixtime, offset) tuple. | 1546 """parse a localized time string and return a (unixtime, offset) tuple. |
1544 if the string cannot be parsed, ValueError is raised.""" | 1547 if the string cannot be parsed, ValueError is raised.""" |
1545 def timezone(string): | 1548 def timezone(string): |
1546 tz = string.split()[-1] | 1549 tz = string.split()[-1] |
1547 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): | 1550 if tz[0] in "+-" and len(tz) == 5 and tz[1:].isdigit(): |
1548 tz = int(tz) | 1551 sign = (tz[0] == "+") and 1 or -1 |
1549 offset = - 3600 * (tz / 100) - 60 * (tz % 100) | 1552 hours = int(tz[1:3]) |
1550 return offset | 1553 minutes = int(tz[3:5]) |
1554 return -sign * (hours * 60 + minutes) * 60 | |
1551 if tz == "GMT" or tz == "UTC": | 1555 if tz == "GMT" or tz == "UTC": |
1552 return 0 | 1556 return 0 |
1553 return None | 1557 return None |
1554 | 1558 |
1555 # NOTE: unixtime = localunixtime + offset | 1559 # NOTE: unixtime = localunixtime + offset |
1599 if part[0] in "HMS": | 1603 if part[0] in "HMS": |
1600 defaults[part] = "00" | 1604 defaults[part] = "00" |
1601 elif part[0] in "dm": | 1605 elif part[0] in "dm": |
1602 defaults[part] = "1" | 1606 defaults[part] = "1" |
1603 else: | 1607 else: |
1604 defaults[part] = datestr(now, "%" + part[0], False) | 1608 defaults[part] = datestr(now, "%" + part[0]) |
1605 | 1609 |
1606 for format in formats: | 1610 for format in formats: |
1607 try: | 1611 try: |
1608 when, offset = strdate(date, format, defaults) | 1612 when, offset = strdate(date, format, defaults) |
1609 except (ValueError, OverflowError): | 1613 except (ValueError, OverflowError): |