Module 13
Module 13
• A Python program can handle date and 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.
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).
Example
ticks = time.time()
print("Number of ticks since 12:00am, January 1, 1970:", ticks)
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
0 tm_year 2008
1 tm_mon 1 to 12
2 tm_mday 1 to 31
3 tm_hour 0 to 23
4 tm_min 0 to 59
6 tm_wday 0 to 6 (0 is Monday)
import time
localtime = time.localtime(time.time())
print("Local current time :", localtime)
This would produce the following result, which could be formatted in any other presentable form −
import time
import time
localtime = time.ctime()
print("Local current time :", localtime)
The calendar module gives a wide range of methods to play with yearly and monthly calendars. Here,
we print a calendar for a given month ( Jan 2008 ) −
import calendar
1) time altzone() : The method altzone() is the attribute of the time module. This returns the offset of
the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST
timezone is east of UTC (as in Western Europe, including the UK). Only use this if daylight is nonzero.
Example
import time
print("time.altzone:", time.altzone)
time.altzone: -23400
Example:-
The following example shows the usage of asctime() method.
import time
t = time.localtime()
print("time.asctime(t):", time.asctime(t))
When we run above program, it produces following result:
def procedure():
time.sleep(2.5)
Example
import time
Example
import time
print("time.gmtime():" , time.gmtime())
But making one small change (add the asterisks in front of time.gmtime()):
print("time.gmtime():" , *time.gmtime())
Produces:
time.gmtime(): 2021 11 14 9 54 27 6 318 0
Example
import time
print("time.localtime():", *time.localtime())
If the input value cannot be represented as a valid time, either OverflowError or ValueError will be
raised.
Example
The following example shows the usage of mktime() method.
import time
time.mktime(t): 1636883863.0
asctime(localtime(secs)): Sun Nov 14 15:27:43 2021
The actual suspension time may be less than that requested because any caught signal will terminate
the sleep() following execution of that signal's catching routine.
Example
The following example shows the usage of sleep() method.
import time
If t is not provided, the current time as returned by localtime() is used. format must be a string. An
exception ValueError is raised if any field in t is outside of the allowed range.
Example
import time
The format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d
%H:%M:%S %Y" which matches the formatting returned by ctime().
If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is
raised.
Example
import time
Example
import time
print("time.time():", time.time())
print(time.localtime(time.time()))
print(time.asctime(time.localtime(time.time())))
time.time(): 1636885442.4242332
time.struct_time(tm_year=2021, tm_mon=11, tm_mday=14, tm_hour=15, tm_min=54, tm_sec=2,
tm_wday=6, tm_yday=318, tm_isdst=0)
Sun Nov 14 15:54:02 2021