Datetime Parser Implementations

Parser implementations for types provided in the datetime package.

Enumerations

enum disnake_compass.impl.parser.datetime.Resolution(value)[source]

Bases: float, Enum

The resolution with which datetime.datetimes etc. are stored.

Member type: float

MICROS = 1e-06

Microsecond resolution.

This is the default for the datetime module, but often more than required.

MILLIS = 0.001

Millisecond resolution.

Rounds the datetime down to the nearest microsecond.

SECONDS = 1.0

Second resolution.

Rounds the datetime down to the nearest second.

MINUTES = 60.0

Minute resolution.

Rounds the datetime down to the nearest minute.

HOURS = 3600.0

Hour resolution.

Rounds the datetime down to the nearest hour.

DAYS = 86400.0

Day resolution.

Rounds the datetime down to the nearest day.

Classes

class disnake_compass.impl.parser.datetime.DatetimeParser(int_parser=NOTHING, *, resolution=Resolution.SECONDS, timezone=datetime.timezone.utc, strict=True)[source]

Bases: Parser[datetime]

Parser type with support for datetimes.

Parameters:

Attributes

int_parser: IntParser

The IntParser to use internally for this parser.

Since the default integer parser uses base-36 to “compress” numbers, the default datetime parser will also return compressed results.

resolution: int | float

The resolution with which to store datetimes in seconds.

Warning

The resolution must be greater than 1e-6, and if the resolution is smaller than 1, it must be a power of 10. If the resolution is greater than 1, it is coerced into an integer.

Note

Python datetime objects have microsecond accuracy. For most applications, this is much more precise than necessary. Since custom id space is limited, seconds was chosen as the default.

strict: bool

Whether the parser is in strict mode.

If the parser is in strict mode, loads() requires the provided datetime object to be of the correct timezone.

timezone: timezone

The timezone to use for parsing. Datetimes returned by loads() will always be of this timezone.

This is not stored in the custom id.

Methods

await dumps(argument, /)[source]

Dump a datetime into a string.

This uses the underlying int_parser.

If strict is set to True, this will fail if the provided argument does not have a timezone set. Otherwise, a timezone-naive datetime will automatically get its timezone set to timezone.

Parameters:

argument (datetime) – The value that is to be dumped.

Raises:

ValueError – Either the parser is set to strict and the provided datetime was timezone-naive, or the provided datetime’s timezone does not match that of the parser.

await loads(argument, /)[source]

Load a datetime from a string.

This uses the underlying int_parser.

The returned datetime is always of the specified timezone.

Parameters:

argument (str) – The string that is to be converted into a datetime.

class disnake_compass.impl.parser.datetime.TimedeltaParser(int_parser=NOTHING, *, resolution=Resolution.SECONDS)[source]

Bases: Parser[timedelta]

Parser type with support for datetime.timedeltas.

Parameters:

Attributes

int_parser: IntParser

The IntParser to use internally for this parser.

Since the default integer parser uses base-36 to “compress” numbers, the default datetime parser will also return compressed results.

resolution: int | float

The resolution with which to store timedeltas in seconds.

Warning

The resolution must be greater than 1e-6, and if the resolution is smaller than 1, it must be a power of 10. If the resolution is greater than 1, it is coerced into an integer.

Note

Python datetime objects have microsecond accuracy. For most applications, this is much more precise than necessary. Since custom id space is limited, seconds was chosen as the default.

Methods

await dumps(argument, /)[source]

Dump a timedelta into a string.

This uses the underlying int_parser.

Parameters:

argument (timedelta) – The value that is to be dumped.

await loads(argument, /)[source]

Load a timedelta from a string.

This uses the underlying int_parser.

Parameters:

argument (str) – The string that is to be converted into a timedelta.

class disnake_compass.impl.parser.datetime.DateParser(int_parser=NOTHING)[source]

Bases: Parser[date]

Parser type with support for dates.

Parameters:

int_parser (IntParser) – The IntParser to use internally for this parser.

Attributes

int_parser: IntParser

The IntParser to use internally for this parser.

Since the default integer parser uses base-36 to “compress” numbers, the default date parser will also return compressed results.

Methods

await dumps(argument, /)[source]

Dump a datetime into a string.

This uses the underlying int_parser.

Parameters:

argument (date) – The value that is to be dumped.

await loads(argument, /)[source]

Load a date from a string.

This uses the underlying int_parser.

Parameters:

argument (str) – The string that is to be converted into a date.

class disnake_compass.impl.parser.datetime.TimeParser(timedelta_parser=NOTHING, *, strict=True, timezone=datetime.timezone.utc)[source]

Bases: Parser[time]

Parser type with support for times.

Important

Unlike DatetimeParser etc., resolution for this class is set via the underlying timedelta_parser. Note that this class does proxy it through the precision property, which supports both getting and setting.

Parameters:

Attributes

property resolution: int | float[source]

The resolution with which to store times in seconds.

Warning

The resolution must be greater than 1e-6, and if the resolution is smaller than 1, it must be a power of 10. If the resolution is greater than 1, it is coerced into an integer.

Note

Python time objects have microsecond accuracy. For most applications, this is much more precise than necessary. Since custom id space is limited, seconds was chosen as the default.

strict: bool

Whether the parser is in strict mode.

If the parser is in strict mode, loads() requires the provided datetime object to be of the correct timezone.

timedelta_parser: TimedeltaParser

The TimedeltaParser to use internally for this parser.

Since the default timedelta parser uses base-36 to “compress” numbers, the default datetime parser will also return compressed results.

timezone: timezone

The timezone to use for parsing. Times returned by loads() will always be of this timezone.

This is not stored in the custom id.

Methods

await dumps(argument, /)[source]

Dump a time into a string.

This uses the underlying timedelta_parser.

If strict is set to True, this will fail if the provided argument does not have a timezone set. Otherwise, a timezone-naive time will automatically get its timezone set to timezone.

Parameters:

argument (time) – The value that is to be dumped.

Raises:

ValueError – Either the parser is set to strict and the provided time was timezone-naive, or the provided time’s timezone does not match that of the parser.

await loads(argument, /)[source]

Load a time from a string.

This uses the underlying timedelta_parser.

The returned time is always of the specified timezone.

Parameters:

argument (str) – The string that is to be converted into a time.

class disnake_compass.impl.parser.datetime.TimezoneParser(timedelta_parser=NOTHING)[source]

Bases: Parser[timezone]

Parser type with support for timezones.

Important

Unlike DatetimeParser etc., resolution for this class is set via the underlying timedelta_parser. Note that this class does proxy it through the precision property, which supports both getting and setting.

Parameters:

timedelta_parser (TimedeltaParser) – The TimedeltaParser to use internally for this parser.

Attributes

property resolution: int | float[source]

The resolution with which to store times in seconds.

Warning

The resolution must be greater than 1e-6, and if the resolution is smaller than 1, it must be a power of 10. If the resolution is greater than 1, it is coerced into an integer.

Note

Python time objects have microsecond accuracy. For most applications, this is much more precise than necessary. Since custom id space is limited, seconds was chosen as the default.

timedelta_parser: TimedeltaParser

The TimedeltaParser to use internally for this parser.

Since the default timedelta parser uses base-36 to “compress” numbers, the default datetime parser will also return compressed results.

Methods

await dumps(argument, /)[source]

Dump a timezone into a string.

This uses the underlying timedelta_parser.

Parameters:

argument (timezone) – The value that is to be dumped.

await loads(argument, /)[source]

Load a timezone from a string.

This uses the underlying timedelta_parser.

Parameters:

argument (str) – The string that is to be converted into a timezone.