Time tuple in Python

30 Jan, 2020
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.
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:
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 objectobj = datetime.datetime.today()  # obtaining the attributes of the# datetime instance as a tupleobjTimeTuple = obj.timetuple()  # displaying the tuples of the objectprint(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 datebirthDate = datetime.datetime(1999, 4, 6)    # obtaining the attributes and displaying themprint("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.
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python