The time class of python datetime module represents a time quantity. The time class can have a tzinfo representing the timezone for which the time is specified. If tzinfo is None then the time object is a “naive” object. If tzinfo is not None then the time object is an “aware” object. The date class does not possess attributes related to time. Similarly the time class does not have date information. A datetime class has both attributes pertaining to date and time. Remember, a date object and a time object can be made into a time object by calling the combine method of the datetime class.
Calling the constructor of the time class can create the time objects. Time Constructor: time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
- hour
- minute
- second
- microsecond
- tzinfo
- *
- fold
All the above parameters are optional parameters. The tzinfo is an object of a class derived from tzinfo which represents the timezone. Generally the timezone concrete class of datetime module is used to represent a timezone. Specialized timezone classes can be designed as per the needs by deriving from the tzinfo class. The fold parameter specifies whether there was any fold in time. A fold in time means a reverse back of the clock time. In countries following Daylight Saving time during the end of summer clocks are reversed back by 1 hour. This reverse back is a fold in time.
- means a splat operator. Using a splat operator a tuple can be unpacked and a time object can be constructed out of the values from the tuple. The time objects can be created, by calling the constructor of time class without providing any parameters. A time object without any parameters will have its hour, minute, second and microsecond initialized to zero.
import datetime
t1 = datetime.time()
print(t1)
t2 = t1.replace(23)
print(t2)
t3 = t2.replace(minute=30)
print(t3)
Output:
00:00:00
23:00:00
23:30:00
import datetime
timeInstance = datetime.time(1,15,16,17)
print(timeInstance)
pstTimeDelta = datetime.timedelta(hours=-8)
pstTZObject = datetime.timezone(pstTimeDelta, name="PST")
timeInstance1 = timeInstance.replace(tzinfo=pstTZObject)
print(timeInstance1)
Output:
01:15:16.000017
01:15:16.000017-08:00
import datetime
timeTuple = (11,59,59,9999)
timeInstance = datetime.time(*timeTuple)
print(timeInstance)
Output:
11:59:59.999999