There are various ways Python supplies date and time feature to add to the program. Python's Time and calendar module help in tracking date and time. Also, the 'DateTime' provide classes for controlling date and time by both simple and complex ways.
The differences between 'naive' and 'aware' objects are:
- The 'aware' object holds the details about time-zone and daylight saving information whereas naïve objects do not have that feature.
- Naive objects are easy to understand and to work with.
As we all can make an idea that time intervals have to be represented in floating point number. The floating-point numbers in units of seconds are signified by Tick in python. Particular instants of time are represented in seconds since 12:00 am, 1st of January, of the year 1990. A popular module name 'time' is available which provides functions that let's programmer work with time and made conversion between time-representation possible.
The pre-defined function time.time() is used to return the current time of the system.
The daytime module exports the constants listed below:
Two programs are shown showing the use of two different modules:
import time; ticktock = time.time(); print("Number of ticks:", ticktock)
Number of ticks: 1465927104.951356
Another program showing datetime module:
from datetime import datetime presentime = datetime.now() print(" NOW THE TIME IS:", presentime) print("Todays Date is:", presentime.strftime('%Y-%m-%d') ) print("Year is:", presentime.year) print("MOnth is:", presentime.month) print("Day is:", presentime.day)
NOW THE TIME IS: 2016-06-14 18:01:25.831269 Todays Date is: 2016-06-14 Year is: 2016 MOnth is: 6 Day is: 14
Timetuple In Python
Python usually handles time's function as a tuple with nine numbers starting from zero (0) to eight (8).
|0||Four digit number (year)||2016|
|1||Months||1 - 12|
|2||Day||1 - 31|
|3||Hour||0 - 23|
|4||Minute||0 - 59|
|5||Second||0 - 60|
|6||Days of the week||0 - 6 (where Zero designates Monday)|
|7||Days of a year||1 - 366|
|8||Daylight saving||-1, 0, 1, -1|
It is used to represent duration, i.e. the difference between two dates and or times. For invoking this, the syntax is:
datetime.timedelta([days, [,seconds [,microseconds [,milliseconds [, minutes [,hours [,weeks]]]]]])
In the above scenario, all arguments are optional and their default value is zero (0).
Program To Get Current Time
import time; curtime = time.localtime(time.time()) print("Current Time is:", curtime)
Current Time is: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=4, tm_sec=49, tm_wday=1, tm_yday=166, tm_isdst=0)
Calendar Of A Month
This facility is provided by "calendar" module which provides a wide range of methods to deal with monthly calendars. A simple program showing how to use:
import calendar calndr = calendar.month(2016, 6) print("The calendar for the month June of Year 2016 is:") print (calndr)
The calendar for the month June of Year 2016 is: June 2016 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