Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add total_seconds() method to datetime.time object #125435

Closed
MirrorAzure opened this issue Oct 14, 2024 · 3 comments
Closed

Add total_seconds() method to datetime.time object #125435

MirrorAzure opened this issue Oct 14, 2024 · 3 comments
Labels
extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@MirrorAzure
Copy link

MirrorAzure commented Oct 14, 2024

Feature or enhancement

Proposal:

It often happens that when working with datetime.time objects, user has to write custom constructions to get the number of seconds from the time object, like that:

>>> time_object = time(hour=5)
>>> total_seconds = time_object.hour * 3600 + time_object.minute * 60 + time_object.second
>>> print(total_seconds)
18000

It would be convenient to have a method that returns total number of seconds of time object
Example:

>>> time_object = time(hour=5)
>>> total_seconds = time_object.total_seconds()
>>> print(total_seconds)
18000

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@MirrorAzure MirrorAzure added the type-feature A feature request or enhancement label Oct 14, 2024
@picnixz picnixz added stdlib Python modules in the Lib dir extension-modules C modules in the Modules dir labels Oct 14, 2024
@erlend-aasland
Copy link
Contributor

This functionality is present in the timedelta class:

>>> import datetime
>>> t = datetime.timedelta(hours=5)
>>> t.total_seconds()
18000.0

@erlend-aasland erlend-aasland added the pending The issue will be closed if no feedback is provided label Oct 14, 2024
@pganssle
Copy link
Member

The time object does not represent an amount of time, it represents an instant on the clock. Like Erlend says, use timedelta if you want to represent an amount of time.

Thank you for taking the time to write a well-formatted request with an accompanying PR! I hate to close issues and PRs like this because they clearly represent the kind of effort that I prefer to encourage, but unfortunately this one is based on a fundamental misunderstanding of the purpose of the time class, and if we were to implement it it would propagate this misunderstanding. Please do not let this discourage you from further reports and PRs in the future.

@pganssle pganssle closed this as not planned Won't fix, can't repro, duplicate, stale Oct 14, 2024
@erlend-aasland erlend-aasland removed the pending The issue will be closed if no feedback is provided label Oct 14, 2024
@MirrorAzure
Copy link
Author

Thanks for the feedback.
In general, I understand why you are not going to implement this functionality. Perhaps I myself misunderstood the purpose of the datetime.time object.
I wanted to suggest my improvement, since I had a task to calculate the difference in seconds between two timestamps, for which I used datetime.time (it might have been more correct to use datetime.datetime, but the date was irrelevant in the context of the task).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Archived in project
Development

No branches or pull requests

4 participants