Time class represents the local time of the day which is independent of any particular day. This class can have the tzinfo object which represents the timezone of the given time. If the tzinfo is None then the time object is the naive object otherwise it is the aware object.
Syntax:
class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
All the arguments are optional. tzinfo can be None otherwise all the attributes must be integer in the following range –
- 0 <= hour < 24
- 0 <= minute < 60
- 0 <= second < 60
- 0 <= microsecond < 1000000
- fold in [0, 1]
Example:
Python3
from datetime import time
my_time = time( 12 , 14 , 36 )
print ( "Entered time" , my_time)
my_time = time(minute = 12 )
print ( "\nTime with one argument" , my_time)
my_time = time()
print ( "\nTime without argument" , my_time)
|
OutputEntered time 12:14:36
Time with one argument 00:12:00
Time without argument 00:00:00
Class Attributes
Let’s see the attributes provided by this class –
Attribute Name | Description |
---|
min | Minimum possible representation of time |
max | Maximum possible representation of time |
resolution | The minimum possible difference between time objects |
hour | The range of hour must be between 0 and 24 (not including 24) |
minute | The range of minute must be between 0 and 60 (not including 60) |
second | The range of second must be between 0 and 60 (not including 60) |
microsecond | The range of microsecond must be between 0 and 1000000 (not including 1000000) |
tzinfo | The object containing timezone information |
fold | Represents if the fold has occurred in the time or not |
Example 1: Getting min and max representable time
Python3
from datetime import time
mintime = time. min
print ( "Min Time supported" , mintime)
maxtime = time. max
print ( "Max Time supported" , maxtime)
|
OutputMin Time supported 00:00:00
Max Time supported 23:59:59.999999
Example 2: Accessing the hour, minutes, seconds, and microseconds attribute from the time class
Python3
from datetime import time
Time = time( 12 , 24 , 36 , 1212 )
print ( "Hour:" , Time.hour)
print ( "Minutes:" , Time.minute)
print ( "Seconds:" , Time.second)
print ( "Microseconds:" , Time.microsecond)
|
OutputHour: 12
Minutes: 24
Seconds: 36
Microseconds: 1212
Class Functions
Time class provides various functions like we can get time from string or convert time to string, format the time according to our need, etc. Let’s see a list of all the functions provided by the time class.
List of Time Class Functions
Function Name | Description |
---|
dst() | Returns tzinfo.dst() is tzinfo is not None |
fromisoformat() | Returns a time object from the string representation of the time |
isoformat() | Returns the string representation of time from the time object |
replace() | Changes the value of the time object with the given parameter |
strftime() | Returns a string representation of the time with the given format |
tzname() | Returns tzinfo.tzname() is tzinfo is not None |
utcoffset() | Returns tzinfo.utcffsets() is tzinfo is not None |
Let’s see certain examples of the above functions
Example 1: Converting time object to string and vice versa
Python3
from datetime import time
Time = time( 12 , 24 , 36 , 1212 )
Str = Time.isoformat()
print ( "String Representation:" , Str )
print ( type ( Str ))
Time = "12:24:36.001212"
Time = time.fromisoformat( Str )
print ( "\nTime from String" , Time)
print ( type (Time))
|
Output
String Representation: 12:24:36.001212
<class 'str'>
Time from String 12:24:36.001212
<class 'datetime.time'>
Example 2: Changing the value of an already created time object and formatting the time
Python3
from datetime import time
Time = time( 12 , 24 , 36 , 1212 )
print ( "Original time:" , Time)
Time = Time.replace(hour = 13 , second = 12 )
print ( "New Time:" , Time)
Ftime = Time.strftime( "%I:%M %p" )
print ( "Formatted time" , Ftime)
|
OutputOriginal time: 12:24:36.001212
New Time: 13:24:12.001212
Formatted time 01:24 PM
Note: For more information on Python Datetime, refer to Python Datetime Tutorial