Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure representing time without date in Python [closed]

Tags:

python

time

Is there in Python data structure to represent only hour minute and second without another info, if not then what structure is best to use in my case?

like image 795
CppMonster Avatar asked Jan 11 '23 18:01

CppMonster


1 Answers

The datetime module offers several types to represent dates and time, the datetime.time() object may fit your needs here:

from datetime import time

afternoon_tea = time(16, 30)

If you are trying to represent durations, you can use datetime.timedelta() objects; these can be used to adjust datetime.date() or datetime.datetime() objects:

from datetime import timedelta

halfday = timedelta(hours=12)

You could also just represent your time as an integer, representing seconds:

onehour = 60

This all depends on your use cases, which you didn't include in your question, unfortunately.

like image 57
Martijn Pieters Avatar answered Jan 31 '23 09:01

Martijn Pieters