Sunday, January 7, 2018

python

Date and Time

Python is very useful in case of Date and Time. We can easily retrieve current date and time using Python.

Retrieve Time

To 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.

  1. import time;  
  2. localtime = time.localtime(time.time())  
  3. print "Current Time is :", localtime




  1. import calendar  
  2. calendar.prcal(2017)  

  1. import calendar  
  2. print (calendar.isleap(2000))

  1. import calendar  
  2. print (calendar.monthcalendar(2014,6))


  1. import calendar  
  2. print (calendar.prmonth(2014,6) )


  1. import calendar  
  2. print "Current month is:"  
  3. cal = calendar.month(20146)  
  4. print (cal)  


  1. import time  
  2. t = (2014626173381480)  
  3. t = time.mktime(t)  
  4. print (time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))