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

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 ...

Compiling and Executing Java Files With -cp

I decided I was going to "man up" and figure out how to compile a java program with an external dependency from the command line instead of relying on an IDE-- the DOS command line, to be more specific. I ran into a few problems: 1.  The external dependency was given to me as a java file.  I experimented compiling it as a .jar, but I wasn't sure how to import a class from a .jar, so I ended up compiling it into a class. 2.  When I tried to run the file, I got an error saying that the class had been compiled with a different version of Java than my JRE.  The Internet told me to check my path variable for Java.  It sure looked like it was pointing to the latest JRE (and the same version of Java as my compiler).  I asked the Internet again and found the following command: for %I in (java.exe) do @echo %~$PATH:I I'm not exactly sure what the syntax of that magic command is (intuitively it's returning the path that executes when I run the "java" com...

Quick Find / Quick Union (Connected Nodes)

Setup This week I learned about the "Quick Find" or "Quick Union" algorithm. Imagine an NxN grid of nodes, some of which are connected by lines. A connection can be interpreted as accessibility: if two nodes are connected, you can get from one to the other. Every node is accessible to itself: to get where you already are, stay there. Also, If you can get from A to B, you can go back from B to A. And if you can get from A to B and from B to C, then you can get from A to C. As a consequence, the connection between nodes divides the grid into regions of mutually accessible nodes. You can travel from any node in a given region to any other node in that region -- but not to any nodes outside that region (exercise to reader -- proof by contradiction). The problem has two parts. First, find a way to represent this grid structure and the accessibility relation; second, use your schema to efficiently calculate whether two given nodes are accessible to each other. ...