Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Dates and Times in Python (2014) Dates and Times Manipulating Time Already

Abhishek Kulkarni
Abhishek Kulkarni
4,405 Points

Why isn't there simply an 'Hour' attribute?

Why did we have to calculate hours on our own? There are attributes for days, seconds and microseconds but why not hours which has a lot more use cases.

2 Answers

Steven Parker
Steven Parker
231,236 Points

The Python reference for timedelta says:

Only days, seconds and microseconds are stored internally. Arguments are converted to those units:

  • A millisecond is converted to 1000 microseconds.
  • A minute is converted to 60 seconds.
  • An hour is converted to 3600 seconds.
  • A week is converted to 7 days.

Only the language designers would know why they didn't break up the stored values further, but we might guess that it was for efficiency reasons, and because the other values can be easily calculated.

:bulb: Idea: You could create your own sub-class that provides additional values as computed properties.

After watching this video I knew that this course was recorded in 2014. Does this still work? Or it is outdated??

Steven Parker
Steven Parker
231,236 Points

Why not try a little experiment and find out? Or just check the current official documentation.