PYTHON PROGRAMMING (2010)
Python Date and Time
There are various methods for handling date and time in python. Therefore has various calendar modules by which the actual time and date are tracked.
Tick
The interval of time in python is in the time instance of seconds. These seconds are in floating point format.
Example
#!/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
Output
Number of ticks since 12:00am, January 1, 1970: 7186862.73399.
Python handles time with Tuple of 9 numbers.
S. No. |
Field |
Values |
0 |
4 DIGITED YEAR |
2008 |
1 |
MONTH |
1 TO 12 |
2 |
DAYS |
1 TO 31 |
3 |
HOURS |
0 TO 23 |
4 |
MINUTES |
0 TO 59 |
5 |
SECONDS |
0 TO 61 |
6 |
DAY OF WEEK |
0 TO 6 |
7 |
DAY OF YEAR |
1 TO 366 |
8 |
DAYLIGHT SAVING |
-1, 0, 1,-1 |
Table
The above tuple are considered equal to the STRUC_TIME structure. Therefore these also contain some attributes which are as follows.
S. No. |
Attributes |
Value |
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 |
5 |
tm_sec |
0 to 61 |
6 |
tm_wday |
0 to 6 |
7 |
tm_yday |
1 to 366 |
8 |
tm_isdst |
-1,0,1,-1 |
Table
Getting current time
This passes the particular required floating point to the function. It returns all the valid nine element to the time Tuple.
Example
#!/usr/bin/python
import time;
localtime = time.localtime(time.time())
print "Local current time :", localtime
Output
Local current time : time.struct_time(tm_year=2008, tm_mon=5, tm_mday=15,
tm_hour=12, tm_min=55, tm_sec=32, tm_wday=0, tm_yday=136, tm_isdst=1.
Getting formatted time
When particular output we have is lagging or is wrong. Therefore there is a need of formatting the time that we have.
Example
#!/usr/bin/python
import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime.
Output
Local current time : Tue Jan 13 10:17:09 2009.
Getting calendar for a month
It helps us to perform functionality with already existing yearly as well as monthly calendar.
Example
#!/usr/bin/python
import calendar
cal = calendar.month(2008, 1)
print "Here is the calendar:"
print cal;
Output
Here is the calendar-
January 2008
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31