91-9990449935 0120-4256464 |
Date and TimePython is very useful in case of Date and Time. We can easily retrieve current date and time using Python. Retrieve TimeTo retrieve current time a predefined function localtime() is used. localtime() receives a parameter time.time() . Here, time is a module, time() is a function that returns the current system time in number of ticks since 12:00 am , January 1,1970. It is known as epoch. Tick is simply a floating point number in seconds since epoch. eg: import time; localtime = time.localtime(time.time()) print "Current Time is :", localtime Output: >>> Current Time is :time.struct_time(tm_year=2014, tm_mon=6, tm_mday=18, tm_hour=12, tm_min=35, tm_sec=44, tm_wday=2, tm_yday=169, tm_isdst=0) >>> Explanation: The time returned is a time structure which includes 9 attributes. These are summoned in the table given below.
Formatted TimePython also support formatted time. Proceed as follows:
eg: import time; localtime = time.asctime( time.localtime(time.time()) ) print "Formatted time :", localtime Output: >>> Formatted time : Sun Jun 22 18:54:20 2014 >>> time module:There are many built in functions defined in time module which are used to work with time.
time() eg: import time printtime.time() Output: >>> 1403700740.39 >>> asctime(time) import time t = time.localtime() printtime.asctime(t) Output: >>> Wed Jun 25 18:30:25 2014 >>> sleep(time) Eg: import time localtime = time.asctime( time.localtime(time.time()) ) printlocaltime time.sleep( 10 ) localtime = time.asctime( time.localtime(time.time()) ) printlocaltime Output: >>> Wed Jun 25 18:15:30 2014 Wed Jun 25 18:15:40 2014 >>> strptime(String str,format f) Eg: import time timerequired = time.strptime("26 Jun 14", "%d %b %y") printtimerequired Output: >>> time.struct_time(tm_year=2014, tm_mon=6, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=177, tm_isdst=-1) >>> Explanation: The strptime() takes a String and format as argument. The format refers to String passed as an argument. "%a %b %d %H:%M:%S %Y" are the default directives. There are many other directives which can be used. In the given example we have used three directives: %d%b%y which specifies day of the month, month in abbreviated form and year without century respectively. Some of them are given as:
etc... gtime() Eg: import time printtime.gmtime() Output: >>> time.struct_time(tm_year=2014, tm_mon=6, tm_mday=28, tm_hour=9, tm_min=38, tm_sec=0, tm_wday=5, tm_yday=179, tm_isdst=0) >>> mktime() Eg: import time t = (2014, 2, 17, 17, 3, 38, 1, 48, 0) second = time.mktime( t ) print second Output: >>> 1392636818.0 >>> strftime() Eg: import time t = (2014, 6, 26, 17, 3, 38, 1, 48, 0) t = time.mktime(t) printtime.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)) Output: >>> Jun 26 2014 11:33:38 >>> CalendarPython provides calendar module to display Calendar. Eg: import calendar print "Current month is:" cal = calendar.month(2014, 6) printcal Output: >>> Current month is: June 2014 Mo TuWe ThFr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 1718 19 2021 22 2324 25 26 27 28 29 30 >>> Calendar module:Python provides calendar module which provides many functions and methods to work on calendar. A list of methods and function used is given below:
prcal(year) Eg: import calendar calendar.prcal(2014) Output: >>> ================================ RESTART ================================ >>> firstweekday() Eg: import calendar printcalendar.firstweekday() Output: >>> 0 >>> isleap(year) Eg: import calendar printcalendar.isleap(2000) Output: >>> True >>> monthcalendar(year,month) Eg: import calendar printcalendar.monthcalendar(2014,6) Output: >>> [[0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]] >>> prmonth(year,month) Eg: import calendar printcalendar.prmonth(2014,6) Output: >>> June 2014 Mo Tu We ThFrSa 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 None >>>
Next TopicPython Exception Handling
|