Wednesday, July 3, 2024
HomeLanguagesPythonPython time.daylight() Function

Python time.daylight() Function

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

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments