pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

py77.python private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Anonymous on Mon 5 May 09:26
report abuse | download | new post

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. #daysDelta24.py  
  4. """
  5. Compute the difference in days (and weeks and days) between 2 dates.
  6. E.g., The difference between 07/04/1776 and 09/11/2001 is 82248 days
  7. Or 11749 weeks and 5 days
  8. """
  9.  
  10. from mx import DateTime as DT
  11. import time
  12. import calendar
  13.  
  14.  
  15. def getDate():
  16.     while True:
  17.         try:
  18.             dstr = raw_input("Enter date as month/day/year, or enter nothing for today: ")
  19.             if dstr == "":
  20.                 return DT.now()
  21.             else:
  22.                 return DT.Parser.DateFromString(dstr, ('us',))
  23.         except ValueError:
  24.             print "\nDate was in wrong form."
  25.             print "Enter date in form m/d/yyyy, e.g. 07/04/1776, 1/18/2009, etc."
  26.            
  27. def DayOfWeek(year, month, day):
  28.     from calendar import weekday
  29.     dayNumber = calendar.weekday(year, month, day)
  30.     dayNames = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
  31.                 'Friday', 'Saturday', 'Sunday')
  32.     return dayNames[dayNumber]
  33.            
  34. def printDate(date):
  35.     date2 = date.strftime("%Y,%m,%d")
  36.     year, month, day = date2[0:4], date2[5:7], date2[8:10]
  37.     astr = "Date entered was %s, a %s"
  38.     print astr % (date.strftime("%m/%d/%Y"), DayOfWeek(int(year), int(month), int(day)))
  39.    
  40. def printInstruction(flag):
  41.     if flag == 'date1':
  42.         print "Enter 2 dates."
  43.         print "What's one of the dates?"
  44.     else:
  45.         print "What's the other date?"
  46.        
  47. def calculateDaysAndWeeksDiffs(date1, date2):
  48.     days1 = int(abs(date2 - date1).days)
  49.     weeks = days1//7
  50.     days2 = days1 % 7
  51.     return days1, weeks, days2
  52.  
  53. def printResult(date1, date2, days1, weeks, days2):
  54.     print "\nThe difference between %s and %s is %d days" % (date1.strftime("%m/%d/%Y"),
  55.         date2.strftime("%m/%d/%Y"), days1)
  56.     print "Or %d weeks and %d days" % (weeks, days2)
  57.     print "=================================================================="
  58.    
  59. def main():
  60.     printInstruction(flag='date1')
  61.     date1 = getDate()
  62.     printDate(date1)
  63.     printInstruction(flag='date2')
  64.     date2 = getDate()
  65.     printDate(date2)
  66.     days1, weeks, days2 = calculateDaysAndWeeksDiffs(date1, date2)
  67.     printResult(date1, date2, days1, weeks, days2)
  68.  
  69. if __name__ == '__main__':
  70.     while True:
  71.         main()
  72.         print
  73.         ans = raw_input("Again? Press Enter for yes; Enter 'n' for no: ")
  74.         if ans == 'n':
  75.             print
  76.             print "Bye"
  77.             time.sleep(1.5)
  78.             break
  79.         print        
  80.    
  81.    
  82. """
  83. Outputs:
  84. ==============#1================================
  85. Enter 2 dates.
  86. What's one of the dates?
  87. Enter date as month/day/year, or enter nothing for today: 5/5/2008
  88. Date entered was 05/05/2008, a Monday
  89. What's the other date?
  90. Enter date as month/day/year, or enter nothing for today: 5/8/2008
  91. Date entered was 05/08/2008, a Thursday
  92.  
  93. The difference between 05/05/2008 and 05/08/2008 is 3 days
  94. Or 0 weeks and 3 days
  95. ===============#2===============================
  96. Enter 2 dates.
  97. What's one of the dates?
  98. Enter date as month/day/year, or enter nothing for today: 5/8/2008
  99. Date entered was 05/08/2008, a Thursday
  100. What's the other date?
  101. Enter date as month/day/year, or enter nothing for today: 5/5/2008
  102. Date entered was 05/05/2008, a Monday
  103.  
  104. The difference between 05/08/2008 and 05/05/2008 is 3 days
  105. Or 0 weeks and 3 days
  106. ================#3===============================
  107. Enter 2 dates.
  108. What's one of the dates?
  109. Enter date as month/day/year, or enter nothing for today:
  110. Date entered was 05/05/2008, a Monday
  111. What's the other date?
  112. Enter date as month/day/year, or enter nothing for today: 5/8/2008
  113. Date entered was 05/08/2008, a Thursday
  114.  
  115. The difference between 05/05/2008 and 05/08/2008 is 2 days
  116. Or 0 weeks and 2 days
  117. =================#4================================
  118. What's one of the dates?
  119. Enter date as month/day/year, or enter nothing for today: 5/8/2008
  120. Date entered was 05/08/2008, a Thursday
  121. What's the other date?
  122. Enter date as month/day/year, or enter nothing for today:
  123. Date entered was 05/05/2008, a Monday
  124.  
  125. The difference between 05/08/2008 and 05/05/2008 is 2 days
  126. Or 0 weeks and 2 days
  127. ==================end==============================
  128. """

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me