Python - SPLessons

Python Date And Time

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Python Date And Time

Python Date And Time

shape Description

A Python program can deal with date and time in a few ways. Changing over between date organizations is a typical task for PCs. Python's chance and date-book modules track times and date. To recover current time a predefined work localtime() is utilized. The localtime() gets a parameter time. The time is a module, time() is a capacity that profits the present framework time in number of ticks since 12:00 am , January 1,1970. It is called as epoch.

Getting current time

shape Description

To interpret a period moment from a seconds since the epoch coasting point esteem into a period tuple, pass the gliding direct esteem toward a capacity that profits a period tuple with every one of the nine things legitimate. The following is an example code. [c]import time; localtime = time.localtime(time.time()) print "Local current time :", localtime[/c] Now compile the code result will be as follows. [c]Local current time : time.struct_time(tm_year=2016, tm_mon=12, tm_mday=20, tm_ho ur=13, tm_min=38, tm_sec=38, tm_wday=1, tm_yday=355, tm_isdst=0) [/c]

Formatted time

shape Description

User can easily format the time by using the method asctime(). The following is an example. [c]import time; localtime = time.asctime( time.localtime(time.time()) ) print "Local current time :", localtime[/c] Now compile the code result will be as follows. [c]Local current time : Tue Dec 20 13:41:59 2016[/c]

Time module functions

shape Description

The following are the list of python time module functions.
Functions Description
time.tzset() Resets the time transformation rules utilized by the library schedules.
time.sleep(secs) To keep the threads for some time.
mktime() To give back the second in floating point.
time() Returns gliding point esteem in seconds since age

Get the calender from a month

shape Description

The following is an example. [c]import calendar cal = calendar.month(2016, 12) print "Here is the calendar:" print cal[/c] Now compile the code result will be as follows. [c]Here is the calendar: December 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 31[/c] The following is an example to get the total calender of a year. [c]import calendar calendar.prcal(2016) [/c] Now compile the code result will be as follows. [c] 2016 January February March Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 4 5 6 7 8 9 10 8 9 10 11 12 13 14 7 8 9 10 11 12 13 11 12 13 14 15 16 17 15 16 17 18 19 20 21 14 15 16 17 18 19 20 18 19 20 21 22 23 24 22 23 24 25 26 27 28 21 22 23 24 25 26 27 25 26 27 28 29 30 31 29 28 29 30 31 April May June Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 1 2 3 4 5 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 30 31 July August September Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 1 2 3 4 5 6 7 1 2 3 4 4 5 6 7 8 9 10 8 9 10 11 12 13 14 5 6 7 8 9 10 11 11 12 13 14 15 16 17 15 16 17 18 19 20 21 12 13 14 15 16 17 18 18 19 20 21 22 23 24 22 23 24 25 26 27 28 19 20 21 22 23 24 25 25 26 27 28 29 30 31 29 30 31 26 27 28 29 30 October November December Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 1 2 3 4 5 6 1 2 3 4 3 4 5 6 7 8 9 7 8 9 10 11 12 13 5 6 7 8 9 10 11 10 11 12 13 14 15 16 14 15 16 17 18 19 20 12 13 14 15 16 17 18 17 18 19 20 21 22 23 21 22 23 24 25 26 27 19 20 21 22 23 24 25 24 25 26 27 28 29 30 28 29 30 26 27 28 29 30 31 31 [/c]

Summary

shape Key Points

  • The isleap(year) is the method of calender module to give back the boolean value.
  • The epoch is nothing nut a time period.
  • The tuple is a set of values which placed in a order.