Auto AdSense

Saturday, 24 January 2015

Python Date/Time

 


  • What is Tick?
    Python's time and calendar modules help track dates and times.
    Time intervals are floating-point numbers in units of seconds.
    Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).
    There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).
  • 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

    This would produce a result something as follows:

    Number of ticks since 12:00am, January 1, 1970: 7186862.73399
  • Getting current time -:
    #!/usr/bin/python
    localtime = time.localtime(time.time())
    print "Local current time :", localtime
  • Output
    Local current time : (2008, 5, 15, 12, 55, 32, 0, 136, 1)
  • Getting formatted time -:
    #!/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 -:
    #!/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

No comments:

Post a Comment