Mercurial > hg > mercurial-crew-with-dirclash
comparison mercurial/util.py @ 3810:d6529582942a
improve date parsing for numerous new date formats
Add lots of new date formats
Formats without year, month, or day default to current
Strip leading and trailing whitespace
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Wed, 06 Dec 2006 13:13:31 -0600 |
parents | e43b48f0f718 |
children | 4d93b37b5963 |
comparison
equal
deleted
inserted
replaced
3809:e43b48f0f718 | 3810:d6529582942a |
---|---|
70 except UnicodeDecodeError, inst: | 70 except UnicodeDecodeError, inst: |
71 sub = s[max(0, inst.start-10), inst.start+10] | 71 sub = s[max(0, inst.start-10), inst.start+10] |
72 raise Abort("decoding near '%s': %s!\n" % (sub, inst)) | 72 raise Abort("decoding near '%s': %s!\n" % (sub, inst)) |
73 | 73 |
74 # used by parsedate | 74 # used by parsedate |
75 defaultdateformats = ('%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M', | 75 defaultdateformats = ( |
76 '%a %b %d %H:%M:%S %Y') | 76 '%Y-%m-%d %H:%M:%S', |
77 '%Y-%m-%d %I:%M:%S%p', | |
78 '%Y-%m-%d %H:%M', | |
79 '%Y-%m-%d %I:%M%p', | |
80 '%Y-%m-%d', | |
81 '%m-%d', | |
82 '%m/%d', | |
83 '%m/%d/%y', | |
84 '%m/%d/%Y', | |
85 '%a %b %d %H:%M:%S %Y', | |
86 '%a %b %d %I:%M:%S%p %Y', | |
87 '%b %d %H:%M:%S %Y', | |
88 '%b %d %I:%M:%S%p', | |
89 '%b %d %H:%M', | |
90 '%b %d %I:%M%p', | |
91 '%b %d %Y', | |
92 '%b %d', | |
93 '%H:%M:%S', | |
94 '%I:%M:%SP', | |
95 '%H:%M', | |
96 '%I:%M%p', | |
97 ) | |
77 | 98 |
78 class SignalInterrupt(Exception): | 99 class SignalInterrupt(Exception): |
79 """Exception raised on SIGTERM and SIGHUP.""" | 100 """Exception raised on SIGTERM and SIGHUP.""" |
80 | 101 |
81 # like SafeConfigParser but with case-sensitive keys | 102 # like SafeConfigParser but with case-sensitive keys |
1050 date, tz = string[:-6], string[-5:] | 1071 date, tz = string[:-6], string[-5:] |
1051 tz = int(tz) | 1072 tz = int(tz) |
1052 offset = - 3600 * (tz / 100) - 60 * (tz % 100) | 1073 offset = - 3600 * (tz / 100) - 60 * (tz % 100) |
1053 else: | 1074 else: |
1054 date, offset = string, None | 1075 date, offset = string, None |
1076 | |
1077 # add missing elements | |
1078 if '%y' not in format.lower(): | |
1079 date += "@" + datestr(makedate(), "%Y", False) | |
1080 format += "@%Y" | |
1081 if '%m' not in format and '%b' not in format: | |
1082 date += "@" + datestr(makedate(), "%m", False) | |
1083 format += "@%m" | |
1084 if '%d' not in format: | |
1085 date += "@" + datestr(makedate(), "%d", False) | |
1086 format += "@%d" | |
1087 | |
1055 timetuple = time.strptime(date, format) | 1088 timetuple = time.strptime(date, format) |
1056 localunixtime = int(calendar.timegm(timetuple)) | 1089 localunixtime = int(calendar.timegm(timetuple)) |
1057 if offset is None: | 1090 if offset is None: |
1058 # local timezone | 1091 # local timezone |
1059 unixtime = int(time.mktime(timetuple)) | 1092 unixtime = int(time.mktime(timetuple)) |
1068 formats.""" | 1101 formats.""" |
1069 if not string: | 1102 if not string: |
1070 return 0, 0 | 1103 return 0, 0 |
1071 if not formats: | 1104 if not formats: |
1072 formats = defaultdateformats | 1105 formats = defaultdateformats |
1106 string = string.strip() | |
1073 try: | 1107 try: |
1074 when, offset = map(int, string.split(' ')) | 1108 when, offset = map(int, string.split(' ')) |
1075 except ValueError: | 1109 except ValueError: |
1076 for format in formats: | 1110 for format in formats: |
1077 try: | 1111 try: |