Open In App

Time tuple in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Python datetime module with examples

In Python, the datetime module is used to operate with date and time objects. This module has different functions and classes for working with date and time parsing, formatting and arithmetic operations. The datetime module has various constants such as MINYEAR, MAXYEAR etc, various classes like datetime.date, datetime.time, datetime.datetime etc and various instance methods such as replace(), weekday(), isocalendar() etc of date object and time(), dst(), timestamp(), etc. of time object.

Timetupe()

In order to use the timetuple() method we need to import the datetime module. The timetuple() method is an instance method for the datetime module this method returns time.struct_time object. The attributes of the time.struct_time object can be accessed by index or name of the attribute. struct_time object has attributes for representing both date and time fields, these attributes are stored in tuples:

Index Attribute Field Domain
0 4 Digit Year tm_year Example: 2020
1 Month tm_mon 1 to 12
2 Day tm_mday 1 to 31
3 Hour tm_hour 0 to 23
4 Minute tm_min 0 to 59
5 Second tm_sec 0 to 61
6 Day of Week tm_wday 0 to 6
7 Day of Year tm_yday 1 to 366
8 Daylight Saving Time tm_isdst -1, 0, 1

Note: The tm_isdst attribute is a flag which is set to 0 when daylight saving time active is off, 1 if daylight saving time is active and -1 if daylight saving time is determined by the compiler, the tm_yday attribute is based on Julian Day Number and in tm_sec, values 60 or 61 are leap seconds.

Example 1: Below example shows the time tuple of the present date.




# program to illustrate timetuple()
# method in Python
  
  
import datetime
  
  
# creating object
obj = datetime.datetime.today()
  
# obtaining the attributes of the
# datetime instance as a tuple
objTimeTuple = obj.timetuple()
  
# displaying the tuples of the object
print(objTimeTuple)


Output:

time.struct_time(tm_year=2020, tm_mon=1, tm_mday=29, tm_hour=14, tm_min=13, tm_sec=32, tm_wday=2, tm_yday=29, tm_isdst=-1)

In the above program, the datetime object obj is assigned with the present date and then timetuple() method is used to obtain the attributes of obj in tuples and is displayed. We can also use a loop to display the attributes of obj.

Example 2: Let us look at another example where we use a custom date.




# program to illustrate timetuple() 
# method in Python 
  
  
import datetime
  
  
# creating object and initializing
# it with custom date
birthDate = datetime.datetime(1999, 4, 6)
  
  
# obtaining the attributes and displaying them
print("Year: ", birthDate.timetuple()[0])
print("Month: ", birthDate.timetuple()[1])
print("Day: ", birthDate.timetuple()[2])
print("Hour: ", birthDate.timetuple()[3])
print("Minute: ", birthDate.timetuple()[4])
print("Second: ", birthDate.timetuple()[5])
print("Day of Week: ", birthDate.timetuple()[6])
print("Day of Year: ", birthDate.timetuple()[7])
print("Daylight Saving Time: ", birthDate.timetuple()[8])


Output:

Year:  1999
Month:  4
Day:  6
Hour:  0
Minute:  0
Second:  0
Day of Week:  1
Day of Year:  96
Daylight Saving Time:  -1

Here the datetime object birthDate is assigned with a custom date and then each attribute of that object is displayed by the index of the tuple.The parameters retrieved from the time.struct_time structure can be accessed as elements of tuple and the year, month, and day fields assigned as the datetime object(here 1999, 4 and 6) and fields referring to the hour, minutes, seconds are set to zero.



Last Updated : 30 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads