|
1 | 1 | from datetime import UTC, datetime |
2 | 2 |
|
3 | 3 | import dateutil.parser |
4 | | -from discord.ext.commands import BadArgument, Converter |
| 4 | +from discord.ext.commands import BadArgument, Context, Converter |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class ISODateTime(Converter): |
8 | 8 | """Converts an ISO-8601 datetime string into a datetime.datetime.""" |
9 | 9 |
|
10 | | - async def convert(self, datetime_string: str) -> datetime: |
| 10 | + async def convert(self, _context: Context, datetime_string: str) -> datetime: |
11 | 11 | """ |
12 | | - Converts a ISO-8601 `datetime_string` into a `datetime.datetime` object. |
| 12 | + Converts an ISO-8601 ``datetime_string`` into a ``datetime.datetime`` object. |
13 | 13 |
|
14 | | - The converter is flexible in the formats it accepts, as it uses the `isoparse` method of |
15 | | - `dateutil.parser`. In general, it accepts datetime strings that start with a date, |
| 14 | + The converter is flexible in the formats it accepts, as it uses the :meth:`isoparse <dateutil.parser.isoparse>` |
| 15 | + method of :mod:`dateutil.parser`. In general, it accepts datetime strings that start with a date, |
16 | 16 | optionally followed by a time. Specifying a timezone offset in the datetime string is |
17 | | - supported, but the `datetime` object will be converted to UTC. If no timezone is specified, the datetime will |
| 17 | + supported, but the ``datetime`` object will be converted to UTC. If no timezone is specified, the datetime will |
18 | 18 | be assumed to be in UTC already. In all cases, the returned object will have the UTC timezone. |
19 | 19 |
|
20 | 20 | See: https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.isoparse |
21 | 21 |
|
22 | 22 | Formats that are guaranteed to be valid by our tests are: |
23 | 23 |
|
24 | | - - `YYYY-mm-ddTHH:MM:SSZ` | `YYYY-mm-dd HH:MM:SSZ` |
25 | | - - `YYYY-mm-ddTHH:MM:SS±HH:MM` | `YYYY-mm-dd HH:MM:SS±HH:MM` |
26 | | - - `YYYY-mm-ddTHH:MM:SS±HHMM` | `YYYY-mm-dd HH:MM:SS±HHMM` |
27 | | - - `YYYY-mm-ddTHH:MM:SS±HH` | `YYYY-mm-dd HH:MM:SS±HH` |
28 | | - - `YYYY-mm-ddTHH:MM:SS` | `YYYY-mm-dd HH:MM:SS` |
29 | | - - `YYYY-mm-ddTHH:MM` | `YYYY-mm-dd HH:MM` |
30 | | - - `YYYY-mm-dd` |
31 | | - - `YYYY-mm` |
32 | | - - `YYYY` |
33 | | -
|
34 | | - Note: ISO-8601 specifies a `T` as the separator between the date and the time part of the |
35 | | - datetime string. The converter accepts both a `T` and a single space character. |
| 24 | + - ``YYYY-mm-ddTHH:MM:SSZ`` | ``YYYY-mm-dd HH:MM:SSZ`` |
| 25 | + - ``YYYY-mm-ddTHH:MM:SS±HH:MM`` | ``YYYY-mm-dd HH:MM:SS±HH:MM`` |
| 26 | + - ``YYYY-mm-ddTHH:MM:SS±HHMM`` | ``YYYY-mm-dd HH:MM:SS±HHMM`` |
| 27 | + - ``YYYY-mm-ddTHH:MM:SS±HH`` | ``YYYY-mm-dd HH:MM:SS±HH`` |
| 28 | + - ``YYYY-mm-ddTHH:MM:SS`` | ``YYYY-mm-dd HH:MM:SS`` |
| 29 | + - ``YYYY-mm-ddTHH:MM`` | ``YYYY-mm-dd HH:MM`` |
| 30 | + - ``YYYY-mm-dd`` |
| 31 | + - ``YYYY-mm`` |
| 32 | + - ``YYYY`` |
| 33 | +
|
| 34 | + .. note:: |
| 35 | +
|
| 36 | + ISO-8601 specifies a ``T`` as the separator between the date and the time part of the |
| 37 | + datetime string. The converter accepts both a ``T`` and a single space character. |
| 38 | +
|
| 39 | + :rtype: datetime.datetime |
36 | 40 | """ |
37 | 41 | try: |
38 | 42 | dt = dateutil.parser.isoparse(datetime_string) |
|
0 commit comments