Open main menu

CDOT Wiki β

OPS435 Python Assignment 2 A

Revision as of 09:57, 27 March 2018 by Rchan (talk | contribs) (Created page with "Category:OPS435-PythonCategory:rchan = Overview = = Sample code for the tomorrow() function = <pre> # echo the date in YYYYMMDD after the current day # def tomorrow(...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Overview

Sample code for the tomorrow() function

# echo the date in YYYYMMDD after the current day
# 
def tomorrow(today):
    if len(today) != 8:
       return '00000000'
    else:
       year = int(today[0:4])
       month = int(today[4:6])
       day = int(today[6:])

       lyear = year % 4
       if lyear == 0:
          feb_max = 29 # this is a leap year
       else:
          feb_max = 28 # this is not a leap year

       lyear = year % 100
       if lyear == 0:
          feb_max = 28 # this is not a leap year

       lyear = year % 400
       if lyear == 0:
          feb_max = 29 # this is a leap year

       tmp_day = day + 1 # tomorrow's day

       mon_max = { 1:31, 2:feb_max, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
       if tmp_day > mon_max[month]:
          to_day = tmp_day % mon_max[month] # if tmp_day > this month's max, reset to 1
          tmp_month = month + 1
       else:
          to_day = tmp_day
          tmp_month = month + 0

       if tmp_month > 12:
           to_month = 1
           year = year + 1
       else:
           to_month = tmp_month + 0

       tomorrow_date = str(year)+str(to_month).zfill(2)+str(to_day).zfill(2)
     
       return tomorrow_date