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