-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Hurley
authored and
Aaron Hurley
committed
Jul 18, 2019
0 parents
commit 3ee0566
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# dayoftheyear | ||
Super simple app that returns the day number of the year and what percentage of the year has been completed. | ||
See it at http://dayoftheyear.cfapps.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
func dayOfTheYear(w http.ResponseWriter, r *http.Request) { | ||
pacific, err := time.LoadLocation("America/Los_Angeles") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
now := time.Now().In(pacific) | ||
firstDayOfThisYear := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, pacific) | ||
firstDayOfNextYear := time.Date(now.Year() + 1, 1, 1, 0, 0, 0, 0, pacific) | ||
totalDaysOfThisYear := daysBetween(firstDayOfThisYear, firstDayOfNextYear) | ||
|
||
dayOfThisYear := now.YearDay() | ||
percentage := float64(dayOfThisYear) / float64(totalDaysOfThisYear) | ||
daysUntilNextYear := totalDaysOfThisYear - dayOfThisYear | ||
|
||
message := fmt.Sprintf("Today is Day: %d.\n", dayOfThisYear) | ||
message += fmt.Sprintf("We are %.1f%% through the year.\n", percentage * 100) | ||
message += fmt.Sprintf("There are %d days left in the year.", daysUntilNextYear) | ||
w.Write([]byte(message)) | ||
} | ||
|
||
func daysBetween(earlier, later time.Time) int { | ||
return int(later.Sub(earlier).Hours() / 24) | ||
} | ||
|
||
func main() { | ||
http.HandleFunc("/", dayOfTheYear) | ||
port := os.Getenv("PORT") | ||
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
applications: | ||
- name: dayoftheyear | ||
memory: 32M | ||
disk_quota: 32M | ||
buildpacks: | ||
- go_buildpack | ||
env: | ||
GOVERSION: go1 | ||
GOPACKAGENAME: github.com/aaronshurley/dayoftheyear |