A Python program can handle Python Date & Time in several ways. Converting between date formats is a common chore for computers. Python’s time and calendar modules help track dates and times.
What is Tick?
Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).
There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).
#!/usr/bin/python import time; # This is required to include time module. ticks = time.time() print "Number of ticks since 12:00am, January 1, 1970:", ticks
Number of ticks since 12:00am, January 1, 1970: 7186862.73399
Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form. Dates in the far future also cannot be represented this way – the cutoff point is sometime in 2038 for UNIX and Windows.
What is Time Tuple?
Many of Python’s time functions handle time as a tuple of 9 numbers, as shown below −
|1||Month||1 to 12|
|2||Day||1 to 31|
|3||Hour||0 to 23|
|4||Minute||0 to 59|
|5||Second||0 to 61 (60 or 61 are leap-seconds)|
|6||Day of Week||0 to 6 (0 is Monday)|
|7||Day of year||1 to 366 (Julian day)|
|8||Daylight savings||-1, 0, 1, -1 means library determines DST|
The above tuple is equivalent to struct_time structure. This structure has following attributes −
|1||tm_mon||1 to 12|
|2||tm_mday||1 to 31|
|3||tm_hour||0 to 23|
|4||tm_min||0 to 59|
|5||tm_sec||0 to 61 (60 or 61 are leap-seconds)|
|6||tm_wday||0 to 6 (0 is Monday)|
|7||tm_yday||1 to 366 (Julian day)|
|8||tm_isdst||-1, 0, 1, -1 means library determines DST|
Getting formatted time
The time can be formatted by using the asctime() function of the time module. It returns the formatted time for the time tuple being passed.
import time #returns the formatted time print(time.asctime(time.localtime(time.time())))
Tue Dec 18 15:31:39 2018
Python sleep time
The sleep() method of time module is used to stop the execution of the script for a given amount of time. The output will be delayed for the number of seconds provided as the float.
Consider the following example.
import time for i in range(0,5): print(i) #Each element will be printed after 1 second time.sleep(1)
0 1 2 3 4
The Python Date & Time Module
The datetime module enables us to create the custom date objects, perform various operations on dates like the comparison, etc.
To work with dates as date objects, we have to import the datetime module into the python source code.
Consider the following example to get the datetime object representation for the current time.
import datetime #returns the current datetime object print(datetime.datetime.now())
Creating date objects
We can create the date objects bypassing the desired date in the datetime constructor for which the date objects are to be created.
Consider the following example.
import datetime #returns the datetime object for the specified date print(datetime.datetime(2020,04,04))
Printing the calendar of whole year
The prcal() method of calendar module is used to print the calendar of the entire year. The year of which the calendar is to be printed must be passed into this method.
import calendar #printing the calendar of the year 2019 s = calendar.prcal(2020)