Monday, June 15, 2015

Project 3: Age in Seconds (and bonus 1 billion seconds and extension)

I've wanted to try out using Python's datetime library for a while. However, as xkcd has joked about, date / time libraries are notoriously complex, so I've been putting it off for a long time.

But here's a simple program I decided to write to test it anyway, since it's Project 3 in the 100 projects list.

from datetime import *
birthday = raw_input("Input your birthday in the form (0001-2015)-(01-12)-(01-31), ex. 2015-06-15:    ")
true_birthday = datetime.strptime(birthday, "%Y-%m-%d")
delta = datetime.now() - true_birthday
print("You're currently " + str(delta.total_seconds()) + " seconds old.")
base = int(raw_input("Enter your favorite number:      "))
i = 1
while base**i < 5000000000:
    tenpowerseconds = true_birthday + timedelta(seconds=base**i)
    strtps = tenpowerseconds.strftime("%a, %Y-%m-%d, %I:%m:%S %p")
    if tenpowerseconds < datetime.now():
        print("You turned {0} (or {1} to the power of {2}) seconds old on: {3}".format(base**i,base,i,strtps))
    else:
        print("You will turn {0} (or {1} to the power of {2}) seconds old on: {3}".format(base**i,base,i,strtps))
    i += 1

It's pretty straightforward, you put in a date as follows:
Input your birthday in the form (0001-2015)-(01-12)-(01-31), ex. 2015-06-15:    1997-06-02
You're currently 569181542.709 seconds old.
Enter your favorite number:      21       
You turned 21 (or 21 to the power of 1) seconds old on: Mon, 1997-06-02, 12:06:21 AM      
You turned 441 (or 21 to the power of 2) seconds old on: Mon, 1997-06-02, 12:06:21 AM    
You turned 9261 (or 21 to the power of 3) seconds old on: Mon, 1997-06-02, 02:06:21 AM  
You turned 194481 (or 21 to the power of 4) seconds old on: Wed, 1997-06-04, 06:06:21 AM
You turned 4084101 (or 21 to the power of 5) seconds old on: Sat, 1997-07-19, 06:07:21 AM
You turned 85766121 (or 21 to the power of 6) seconds old on: Sat, 2000-02-19, 03:02:21 PM
You will turn 1801088541 (or 21 to the power of 7) seconds old on: Sun, 2054-06-28, 10:06:21 PM

Pretty fun thing to do just for fun. It's on Pastebin as per usual for easier viewing.

No comments:

Post a Comment