Search This Blog

Wednesday, July 9, 2008

Date and Time handling in Python

  1. # handling date/time data
  2. # Python23 tested vegaseat 3/6/2005
  3. import time
  4. print "List the functions within module time:"
  5. for funk in dir(time):
  6. print funk
  7. print time.time(), "seconds since 1/1/1970 00:00:00"
  8. print time.time()/(60*60*24), "days since 1/1/1970"
  9. # time.clock() gives wallclock seconds, accuracy better than 1 ms
  10. # time.clock() is for windows, time.time() is more portable
  11. print "Using time.clock() = ", time.clock(), "seconds since first call to clock()"
  12. print "\nTiming a 1 million loop 'for loop' ..."
  13. start = time.clock()
  14. for x in range(1000000):
  15. y = x # do something
  16. end = time.clock()
  17. print "Time elapsed = ", end - start, "seconds"
  18. # create a tuple of local time data
  19. timeHere = time.localtime()
  20. print "\nA tuple of local date/time data using time.localtime():"
  21. print "(year,month,day,hour,min,sec,weekday(Monday=0),yearday,dls-flag)"
  22. print timeHere
  23. # extract a more readable date/time from the tuple
  24. # eg. Sat Mar 05 22:51:55 2005
  25. print "\nUsing time.asctime(time.localtime()):", time.asctime(time.localtime())
  26. # the same results
  27. print "\nUsing time.ctime(time.time()):", time.ctime(time.time())
  28. print "\nOr using time.ctime():", time.ctime()
  29. print "\nUsing strftime():"
  30. print "Day and Date:", time.strftime("%a %m/%d/%y", time.localtime())
  31. print "Day, Date :", time.strftime("%A, %B %d, %Y", time.localtime())
  32. print "Time (12hr) :", time.strftime("%I:%M:%S %p", time.localtime())
  33. print "Time (24hr) :", time.strftime("%H:%M:%S", time.localtime())
  34. print "DayMonthYear:",time.strftime("%d%b%Y", time.localtime())
  35. print
  36. print "Start a line with this date-time stamp and it will sort:",\
  37. time.strftime("%Y/%m/%d %H:%M:%S", time.localtime())
  38. print
  39. def getDayOfWeek(dateString):
  40. # day of week (Monday = 0) of a given month/day/year
  41. t1 = time.strptime(dateString,"%m/%d/%Y")
  42. # year in time_struct t1 can not go below 1970 (start of epoch)!
  43. t2 = time.mktime(t1)
  44. return(time.localtime(t2)[6])
  45. Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
  46. 'Friday', 'Saturday', 'Sunday']
  47. # sorry about the limitations, stay above 01/01/1970
  48. # more exactly 01/01/1970 at 0 UT (midnight Greenwich, England)
  49. print "11/12/1970 was a", Weekday[getDayOfWeek("11/12/1970")]
  50. print
  51. print "Calculate difference between two times (12 hour format) of a day:"
  52. time1 = raw_input("Enter first time (format 11:25:00AM or 03:15:30PM): ")
  53. # pick some plausible date
  54. timeString1 = "03/06/05 " + time1
  55. # create a time tuple from this time string format eg. 03/06/05 11:22:00AM
  56. timeTuple1 = time.strptime(timeString1, "%m/%d/%y %I:%M:%S%p")
  57. #print timeTuple1 # test eg. (2005, 3, 6, 11, 22, 0, 5, 91, -1)
  58. time2 = raw_input("Enter second time (format 11:25:00AM or 03:15:30PM): ")
  59. # use same date to stay in same day
  60. timeString2 = "03/06/05 " + time2
  61. timeTuple2 = time.strptime(timeString2, "%m/%d/%y %I:%M:%S%p")
  62. # mktime() gives seconds since epoch 1/1/1970 00:00:00
  63. time_difference = time.mktime(timeTuple2) - time.mktime(timeTuple1)
  64. #print type(time_difference) # test
  65. print "Time difference = %d seconds" % int(time_difference)
  66. print "Time difference = %0.1f minutes" % (time_difference/60.0)
  67. print "Time difference = %0.2f hours" % (time_difference/(60.0*60))
  68. print
  69. print "Wait one and a half seconds!"
  70. time.sleep(1.5)
  71. print "The end!"

No comments: