I guess I should really be putting these things up in GitHub. The way I see it, the coding journal is just a place to share the code I write or study along with any notes I have about it. It's sort of a documentation LiveJournal, if you will.
Anyway, this is a "study" for my project idea: create an app that will prompt the user for two dates, then calculate the difference between them. The burden of this study is twofold: (1) convert dates in standard American form (e.g. December 15, 1993) into dates in standard American numeric form (e.g. 12/15/1993); (2) create a numerical representation of the date.
To process the date, I started with a list of the months. I then used a loop to create a dictionary that would attach a value to each month. Next I had to parse the user entry (I haven't added any debugging for incorrect entries yet). I did so by splitting the entry into "raw" data. I used my dictionary to process the month name into a number, stripped the day of the comma and changed it into an integer, and ended with a tuple with the numeric values of month, day, and year. A little bit of processing gave me back a string in the "xx/xx/xx" format.
The hard part was getting the number. You can of course start with the day (including the current day in the total days elapsed). But what about the month? January has 31 days and February has 28 or 29 depending if it's a leap year. Then the months alternate between 31 and 30 days.
I don't know any other way than to split up the operation according to these possibilities: (A) it is February (January requires no processing); (B) it is March; (C) it's later than March. In the first case, we add 31 to our total for January. In the second, we add 31 for January and then check if it's a leap-year. It's a leap year if the year modulo 4 is 0 (I think): the leap years are 0, 4, 8, etc. If it's a leap year, we add 29, otherwise 28. In the final case, we have to check whether it's a leap year and proceed as in Case 2, then loop through the months remaining and add 31 when we're on even months, 30 when we're on odd months.
Once I have everything, I print the results. I now have a number for each date (IF my calculations and loops are all correct) that can be used to calculate the distance between dates. God-willing.
#This app is an exploratory for a program that will allow the user to enter
#dates, possibly in multiple formats, and then print the difference between
#the two. This app will store a date entered in the form
#"[Month] [day], [year]" and (1) convert it to the form "xx/xx/xx",
#(2) produce a mod-365 representation of the date.
import string
months = ["January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"]
monthsdic = dict()
i = 0
for month in months:
monthsdic[month] = i+1
i += 1
while True:
datestr = input("Enter a date (e.g. December 15, 1993): ")
rdt = datestr.split()
pdt = (monthsdic[rdt[0]], int(rdt[1].strip(string.punctuation)), int(rdt[2]))
mdate = pdt[1] + ((pdt[2] - pdt[2] % 4)/4) + pdt[2]*365
if pdt[0] == 2:
mdate += 31
if pdt[0] == 3:
if pdt[2] % 4 == 0:
mdate += 31 + 29
else:
mdate += 31 + 28
if 3 < pdt[0] and pdt[0] < 13:
if pdt[2] % 4 == 0:
mdate += 31 + 29
i = 0
while i < pdt[0]-3:
if i % 2 == 0:
mdate += 31
else:
mdate += 30
i += 1
else:
mdate += 31 + 28
i = 0
while i < pdt[0]-3:
if i % 2 == 0:
mdate += 31
else:
mdate += 30
i += 1
newdatestr = str(pdt[0]) + '/' + str(pdt[1]) + '/' + str(pdt[2])
print("Standard format date:", newdatestr)
print("Days after Christ:", int(mdate))
Comments
Post a Comment