Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. /usr/bin/env python3
  2. '''
  3. OPS435 Assignment 1 - Winter 2019
  4. Program: dnguyen72.py
  5. Author: David Nguyen
  6. The python code in this file (dnguyen72.py) is original work written by
  7. David Nguyen. No code in this file is copied from any other source
  8. except those provided by the course instructor, including any person,
  9. textbook, or on-line resource. I have not shared this python script
  10. with anyone or anything except for submission for grading.
  11. I understand that the Academic Honesty Policy will be enforced and
  12. violators will be reported and appropriate action will be taken.
  13. '''
  14. import sys
  15.  
  16.  
  17.  
  18. def usage():
  19. '''
  20. The usage() function will output a string describing the proper usage of the script if the incorrect number of arguments are given.
  21. '''
  22.  
  23. if ((len(sys.argv) != 4) or len(sys.argv) != 3):
  24. print(sys.argv[0] + '[--step] YYYYMMDD +/-n')
  25. exit()
  26.  
  27. def dbda(date,days):
  28. '''
  29. The dbda() function takes two arguments; a date in "YYYYMMDD" format and a positive or negative interger.
  30. The function will add or subtract the amount of days corresponding to the interger from the given date and determine the new date.
  31.  
  32. Examples:
  33. dbda(20190101, 20)
  34. 20190121
  35. '''
  36.  
  37. if len(days) == 8:
  38. valid_date(days)
  39.  
  40. day_counter = 0
  41. temp_day = days
  42.  
  43. if date > days:
  44.  
  45. while date > temp_day:
  46. temp_day = tomorrow(temp_day)
  47. day_counter = day_counter + 1
  48.  
  49. elif temp_day > date:
  50. while date != temp_day:
  51. temp_day = yesterday(temp_day)
  52. day_counter = day_counter + 1
  53.  
  54. print(day_counter)
  55.  
  56. else:
  57. year = int(date[0:4])
  58. month = int(date[4:6])
  59. day = int(date[6:])
  60.  
  61. counter = int(days)
  62. new_date = date
  63.  
  64. if counter > 0:
  65. while counter != 0:
  66. new_date = tomorrow(new_date)
  67. counter = counter - 1
  68. if sys.argv[1] == '--step':
  69. print(new_date)
  70. elif counter < 0:
  71. while counter != 0:
  72. new_date = yesterday(new_date)
  73. counter = counter + 1
  74. if sys.argv[1] == '--step':
  75. print(new_date)
  76. print(new_date)
  77.  
  78. def tomorrow(date):
  79. '''
  80. The tomorrow() function will take a given date and return the date of the next day.
  81.  
  82. Example:
  83. tomorrow(20190101)
  84. 20190102
  85. '''
  86. year = int(date[0:4])
  87. month = int(date[4:6])
  88. day = int(date[6:])
  89. temp_day = day + 1
  90. temp_month = month
  91. mon_max = days_in_mon(year)
  92.  
  93. # If days go over 31
  94. if temp_day > mon_max[month]:
  95. temp_day = 1
  96. temp_month = month + 1
  97.  
  98. # If month goes over max for year
  99. if temp_month > 12:
  100. temp_day = 1
  101. temp_month = 1
  102. year = year + 1
  103.  
  104. next_day = str(year) + str(temp_month).zfill(2) + str(temp_day).zfill(2)
  105. return next_day
  106.  
  107. def yesterday(date):
  108. '''
  109. The yesterday() function will take a given date and return the date of the previous day.
  110.  
  111. Example:
  112. yesterday(20190102)
  113. 20190101
  114. '''
  115.  
  116. year = int(date[0:4])
  117. month = int(date[4:6])
  118. day = int(date[6:])
  119. temp_day = day - 1
  120. temp_month = month
  121. mon_max = days_in_mon(year)
  122.  
  123. # If day goes to 0
  124. if temp_day == 0:
  125. temp_day = mon_max[month]
  126. temp_month = month - 1
  127.  
  128. # If month goes less than 1
  129. if temp_month == 0:
  130. temp_day = 31
  131. temp_month = 12
  132. year = year - 1
  133.  
  134. previous_day = str(year) + str(temp_month).zfill(2) + str(temp_day).zfill(2)
  135. return previous_day
  136.  
  137. def valid_date(date):
  138. '''
  139. The valid_date() function will take a given date and determine if it is valid. If function will return True if valid, or give an error message if invalid.
  140.  
  141. Examples:
  142. valid_date(2020202020)
  143. Error: wrong date entered
  144. '''
  145.  
  146. year = int(date[0:4])
  147. month = int(date[4:6])
  148. day = int(date[6:])
  149.  
  150. # Check if date format is valid
  151. if len(str(date)) != 8:
  152. print('Error: wrong date entered')
  153. exit()
  154.  
  155. # Check if month inputed is valid
  156. month_day = days_in_mon(year)
  157. if month not in month_day.keys():
  158. print('Error: wrong month entered')
  159. exit()
  160.  
  161. # Check if day inputed is valid
  162. month_days_dictionary = days_in_mon(year)
  163. month_days = month_days_dictionary[month]
  164.  
  165. if day not in range(1, month_days+1):
  166. print('Error: wrong day entered')
  167. exit()
  168.  
  169. def days_in_mon(year):
  170. '''
  171. The days_in_mon() function will take a given year and determine the maximum amount of days for each month in the given year.
  172. It will return a dictionary object containing the maximum days corresponding to each month of the given year.
  173.  
  174. Examples:
  175. days_in_mon(2020)
  176. {1:31, 2:29, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
  177. '''
  178.  
  179. if leap_year(year) == 'TRUE':
  180. feb = 29
  181. else:
  182. feb = 28
  183. month_day = {1:31, 2:feb, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
  184. return month_day
  185.  
  186.  
  187. def leap_year(year):
  188. '''
  189. The leap_year() function will take a given year and determine whether it is a leap year or not.
  190.  
  191. Examples:
  192. leap_year(2019)
  193. FALSE
  194. '''
  195.  
  196. # Determine if year given is a "leap year"
  197. if year % 4 == 0:
  198. if year % 100 == 0:
  199. if year % 400 == 0:
  200. feb = 29
  201. else:
  202. feb = 28
  203. else:
  204. feb = 29
  205. else:
  206. feb = 28
  207.  
  208. # Return TRUE or FALSE if leap year
  209. if feb == 28:
  210. return False
  211. else:
  212. return True
  213.  
  214. if __name__ == '__main__':
  215. usage()
  216.  
  217. date = sys.argv[1]
  218. days = sys.argv[2]
  219.  
  220. if sys.argv[1] == '--step':
  221. date = sys.argv[2]
  222. day = sys.argv[3]
  223. else:
  224. date = sys.argv[1]
  225. day = sys.argv[2]
  226.  
  227. dbda(date, days)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement