Skip to main content

It's a Date

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

Popular posts from this blog

Nailing Down The Meaning Of "This" In JavaScript

Sense-certainty itself has thus to be asked: What is the This? If we take it in the two-fold form of its existence, as the Now and as the Here, the dialectic it has in it will take a form as intelligible as the This itself. To the question, What is the Now? we reply, for example, the Now is night-time. To test the truth of this certainty of sense, a simple experiment is all we need: write that truth down. A truth cannot lose anything by being written down, and just as little by our preserving and keeping it. If we look again at the truth we have written down, look at it now, at this noon-time, we shall have to say it has turned stale and become out of date.   Hegel,  Phenomenology of Spirit  S. 95 Overview In philosophy, English words like "this" are referred to as indexicals, with the implication that (A) they are used to point at something and (B) what they point at is determined by context (just as, when I point at someone, who I am pointing at is determined ...

Getting Geodata From Google's API

The apps I'm going to be analyzing are part of Dr. Charles Severance's MOOC on Python and Databases and work together according to the following structure (which applies both in this specific case and more generally to any application that creates and interprets a database using online data). The data source, in this case, is Google's Google Maps Geocoding API.  The "package" has two components: geoload.py  and geodump.py .  geoload.py  reads a list of locations from a file -- addresses for which we would like geographical information -- requests information about them from Google, and stores the information on a database ( geodata.db ).  geodump.py  reads and parses data from the database in JSON, then loads that into a javascript file.  The javascript is then used to create a web page on which the data is visualized as a series of points on the world-map.  Dr. Severance's course focuses on Python, so I'm only going to work my way through ...