Skip to content

Instantly share code, notes, and snippets.

@chris-x86-64
Last active January 4, 2016 02:39
Show Gist options
  • Save chris-x86-64/8556424 to your computer and use it in GitHub Desktop.
Save chris-x86-64/8556424 to your computer and use it in GitHub Desktop.
Age calculator available for use at http://age.x86-64.jp/ Usage: curl http://age.x86-64.jp/1991/12/29 ... Response: "Age: 22"
from flask import Flask
from datetime import date
app = Flask(__name__)
app.config.update(
DEBUG=False,
)
@app.route('/')
def usage():
return "Usage: http://age.x86-64.jp/yyyy/mm/dd <br> curl recommended."
@app.route('/<int:yyyy>/<int:mm>/<int:dd>')
def calculate(yyyy, mm, dd):
"""This part is from Stack Overflow: http://stackoverflow.com/a/2217537/1402144"""
birthday = date(yyyy, mm, dd)
today = date.today()
years = today.year - birthday.year
if all((x >= y) for x,y in zip(today.timetuple(), birthday.timetuple())):
age = years
else:
age = years - 1
"""End StackOverflow"""
return "Age: %d" % age
if __name__ == '__main__':
app.run() # Change this to "pass" in order to run this via uwsgi.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment