year-progress/year-progress.py

61 lines
2.1 KiB
Python

import datetime
import math
from flask import Flask, render_template, url_for
app = Flask(__name__)
def num2day(num):
match num:
case 1:
return "Monday"
case 2:
return "Tuesday"
case 3:
return "Wednesday"
case 4:
return "Thursday"
case 5:
return "Friday"
def workWeek(yearPercent):
totalNumHours = 40*yearPercent
numDays = totalNumHours/8
hoursIntoDay = (numDays - math.floor(numDays))*8
minutesIntoDay = round((hoursIntoDay - math.floor(hoursIntoDay))*60)
workDatetime = datetime.datetime(year=1970,month=1,day=1,hour=math.floor(hoursIntoDay)+9,minute=minutesIntoDay)
workTime = workDatetime.strftime("%-I:%M%p")
return num2day(math.floor(numDays+1)), workTime
def getTemplate():
now = datetime.datetime.now()
nowTime = now.strftime("%-I:%M%p")
nowDate = now.strftime("%A") + " " + now.strftime("%-d") + " " + now.strftime("%B")
delta = now - datetime.datetime(day=1,month=1,year=now.year)
totalDays = datetime.datetime(day=1,month=1,year=now.year+1) - datetime.datetime(day=1,month=1,year=now.year)
yearPercent = delta/totalDays
footballMinute = math.floor(yearPercent*90)
workDay, workTime = workWeek(yearPercent)
marathonMile = 26.2 - math.floor(yearPercent*26.2)
return now.year, nowTime, nowDate, round(yearPercent*100,2), footballMinute, workDay, workTime, marathonMile
#abbey road
#the hobbit
@app.route("/")
def hello_world():
year, nowTime, nowDate, yearPercent, footballMinute, workDay, workTime, marathonMile = getTemplate()
cssPath = url_for('static', filename='style.css')
return render_template("index.html",\
cssPath=cssPath,\
year=year,\
nowTime=nowTime,\
nowDate=nowDate,\
yearPercent=yearPercent,\
footballMinute=footballMinute,\
workDay=workDay,\
workTime=workTime,\
marathonMile=marathonMile)