Show More
@@ -18,6 +18,18 b' from .. import (' | |||
|
18 | 18 | pycompat, |
|
19 | 19 | ) |
|
20 | 20 | |
|
21 | if pycompat.TYPE_CHECKING: | |
|
22 | from typing import ( | |
|
23 | Callable, | |
|
24 | Dict, | |
|
25 | Iterable, | |
|
26 | Optional, | |
|
27 | Tuple, | |
|
28 | Union, | |
|
29 | ) | |
|
30 | ||
|
31 | hgdate = Tuple[float, int] # (unixtime, offset) | |
|
32 | ||
|
21 | 33 | # used by parsedate |
|
22 | 34 | defaultdateformats = ( |
|
23 | 35 | b'%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601 |
@@ -62,6 +74,7 b' extendeddateformats = defaultdateformats' | |||
|
62 | 74 | |
|
63 | 75 | |
|
64 | 76 | def makedate(timestamp=None): |
|
77 | # type: (Optional[float]) -> hgdate | |
|
65 | 78 | """Return a unix timestamp (or the current time) as a (unixtime, |
|
66 | 79 | offset) tuple based off the local timezone.""" |
|
67 | 80 | if timestamp is None: |
@@ -79,6 +92,7 b' def makedate(timestamp=None):' | |||
|
79 | 92 | |
|
80 | 93 | |
|
81 | 94 | def datestr(date=None, format=b'%a %b %d %H:%M:%S %Y %1%2'): |
|
95 | # type: (Optional[hgdate], bytes) -> bytes | |
|
82 | 96 | """represent a (unixtime, offset) tuple as a localized time. |
|
83 | 97 | unixtime is seconds since the epoch, and offset is the time zone's |
|
84 | 98 | number of seconds away from UTC. |
@@ -116,11 +130,13 b" def datestr(date=None, format=b'%a %b %d" | |||
|
116 | 130 | |
|
117 | 131 | |
|
118 | 132 | def shortdate(date=None): |
|
133 | # type: (Optional[hgdate]) -> bytes | |
|
119 | 134 | """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
|
120 | 135 | return datestr(date, format=b'%Y-%m-%d') |
|
121 | 136 | |
|
122 | 137 | |
|
123 | 138 | def parsetimezone(s): |
|
139 | # type: (bytes) -> Tuple[Optional[int], bytes] | |
|
124 | 140 | """find a trailing timezone, if any, in string, and return a |
|
125 | 141 | (offset, remainder) pair""" |
|
126 | 142 | s = pycompat.bytestr(s) |
@@ -156,6 +172,7 b' def parsetimezone(s):' | |||
|
156 | 172 | |
|
157 | 173 | |
|
158 | 174 | def strdate(string, format, defaults=None): |
|
175 | # type: (bytes, bytes, Optional[Dict[bytes, Tuple[bytes, bytes]]]) -> hgdate | |
|
159 | 176 | """parse a localized time string and return a (unixtime, offset) tuple. |
|
160 | 177 | if the string cannot be parsed, ValueError is raised.""" |
|
161 | 178 | if defaults is None: |
@@ -198,6 +215,7 b' def strdate(string, format, defaults=Non' | |||
|
198 | 215 | |
|
199 | 216 | |
|
200 | 217 | def parsedate(date, formats=None, bias=None): |
|
218 | # type: (Union[bytes, hgdate], Optional[Iterable[bytes]], Optional[Dict[bytes, bytes]]) -> hgdate | |
|
201 | 219 | """parse a localized date/time and return a (unixtime, offset) tuple. |
|
202 | 220 | |
|
203 | 221 | The date may be a "unixtime offset" string or in one of the specified |
@@ -223,8 +241,11 b' def parsedate(date, formats=None, bias=N' | |||
|
223 | 241 | bias = {} |
|
224 | 242 | if not date: |
|
225 | 243 | return 0, 0 |
|
226 |
if isinstance(date, tuple) |
|
|
227 |
|
|
|
244 | if isinstance(date, tuple): | |
|
245 | if len(date) == 2: | |
|
246 | return date | |
|
247 | else: | |
|
248 | raise error.ProgrammingError(b"invalid date format") | |
|
228 | 249 | if not formats: |
|
229 | 250 | formats = defaultdateformats |
|
230 | 251 | date = date.strip() |
@@ -284,6 +305,7 b' def parsedate(date, formats=None, bias=N' | |||
|
284 | 305 | |
|
285 | 306 | |
|
286 | 307 | def matchdate(date): |
|
308 | # type: (bytes) -> Callable[[float], bool] | |
|
287 | 309 | """Return a function that matches a given date match specifier |
|
288 | 310 | |
|
289 | 311 | Formats include: |
@@ -313,10 +335,12 b' def matchdate(date):' | |||
|
313 | 335 | """ |
|
314 | 336 | |
|
315 | 337 | def lower(date): |
|
338 | # type: (bytes) -> float | |
|
316 | 339 | d = {b'mb': b"1", b'd': b"1"} |
|
317 | 340 | return parsedate(date, extendeddateformats, d)[0] |
|
318 | 341 | |
|
319 | 342 | def upper(date): |
|
343 | # type: (bytes) -> float | |
|
320 | 344 | d = {b'mb': b"12", b'HI': b"23", b'M': b"59", b'S': b"59"} |
|
321 | 345 | for days in (b"31", b"30", b"29"): |
|
322 | 346 | try: |
General Comments 0
You need to be logged in to leave comments.
Login now