Skip to content

Use datetime.timedelta to Represent Time Periods #4575

@Bibo-Joshi

Description

@Bibo-Joshi

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

Several Telegram Objects and methods currently contain fields/parameters with names \w+_period, wherer the integer value represents a time span in seconds.
Working with plain seconds is not very pythonic, just like working with posix timestamps isn't.

Describe the solution you'd like

In the long run, we should convert the attributes to timedelta objects and make the parameters accept timedelta objects.

Methods Parameters

This is realtively easy, simply accept timedelta as well as integers and pass totalseconds() to TG. Converting dtm.timedelta to int can probably be done in the internals of Bot or requests so that it doesn't have to happen in each bot method.

Classes

  • Both attributes and arguments should only accept timedeltas in the long run.
  • Since this is a breaking change, we should issue a warning for now about this changing in the future and encouraging users to harden their code against the change
  • For making the timedelta support available early, I have two ideas, both optional
    • We can provide a temporary property that returns the timedelta object (e.g. xy_period_timedelta). This would need to issue a warning stating that it will be removed once timedelta is natively supported. Not overly clean …
    • We anyways need to make the *_period attributes properties to issue the deprecation warning. We could use this and look for an environment variable, say PTB_TIMEDELTA. If set, we return the timedelta object, otherwise the int. → let's do this

Describe alternatives you've considered

No response

Additional context

Adding support for method parameters can be done independently of handling classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions