Open In App

Python time.daylight() Function

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Time.daylight() function which returns a non zero integer value when Daylight Saving Time (DST) is defined, else it returns 0. Daylight Saving Time, in short, is called DST which is a best practice of setting a clock time one hour forward from standard time during months of summer and back again in fall. This will be done to make better use of sunlight.

Syntax:

time.daylight

Parameters:

This function doesn’t require any parameters.

Example 1:

Here, the daylight function returns 0 indicates that Daylight Saving Time (DST) is not defined.

Python3




# import necessary packages
import time
  
# use daylight function
print(time.daylight)


Output:

0

Instead of time.daylight function, we can also check whether the Daylight Saving Time (DST) is defined or not using localtime method.

Localtime method

It returns the present Date, Time along with whether a Daylight Saving Time (DST) is set or not. If set, in the returned result it will be displayed as tm_isdst=1 else 0. 

Example 2:

At last in the first line of output tm_isdst=0 specifies that DST is not set. So in confirmation time.daylight also returns 0 that DST is not defined.

Python3




# import necessary packages
import time
  
# use localtime method
print(time.localtime())
  
# use daylight function
print(time.daylight)


Output:

time.struct_time(tm_year=2021, tm_mon=12, tm_mday=6, tm_hour=9, tm_min=0, tm_sec=43, tm_wday=0, tm_yday=340, tm_isdst=0)

0

Example 3:

Here, we are going to check whether the DST is defined or not.

Python3




# import necessary packages
import time
  
# if DST defined
if(time.daylight):
    print('DST defined')
else:
    print('DST not defined')


Output:

DST not defined


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads