forked from nikitavoloboev/past-alfred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirthday.go
90 lines (73 loc) · 2.09 KB
/
birthday.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"log"
"os"
"time"
aw "github.com/deanishe/awgo"
humanize "github.com/dustin/go-humanize"
)
var (
updateAvailable = &aw.Icon{Value: "icons/update-available.png"}
dateOfBirth = os.Getenv("Date of Birth (DD/MM/YYYY)")
query string
)
// Gets age correctly.
func getAge(birthday time.Time) int {
currentYear := time.Now().Year()
currentMonth := time.Now().Month()
currentDay := time.Now().Day()
birthYear := birthday.Year()
birthMonth := birthday.Month()
birthDay := birthday.Day()
var age int
if currentMonth < birthMonth {
age = currentYear - birthYear - 1
} else if currentMonth > birthMonth {
age = currentYear - birthYear
} else {
if currentDay >= birthDay {
age = currentYear - birthYear
} else {
age = currentYear - birthYear - 1
}
}
return age
}
// TODO: cache value so not to rerun this every second, only append to seconds on reruns
func getBirthday() error {
log.Printf("query=%s", query)
// Get birthday
if dateOfBirth == "" {
wf.NewItem("Please add your date of birth in configuration sheet")
wf.SendFeedback()
} else {
wf.Rerun(1)
const dateForm = "02/01/2006"
parsedDate, err := time.Parse(dateForm, dateOfBirth)
if err != nil {
log.Fatal(err)
}
birthDate := time.Date(parsedDate.Year(), parsedDate.Month(), parsedDate.Day(), 0, 0, 0, 0, time.UTC)
timeSinceBirthdate := time.Since(birthDate)
wf.NewItem("You have lived:")
// Years
wf.NewItem(humanize.Comma(int64(timeSinceBirthdate.Hours()/8760)) + " years")
// Months
wf.NewItem(humanize.Comma(int64(timeSinceBirthdate.Hours()/730)) + " months")
// Days
wf.NewItem(humanize.Comma(int64(timeSinceBirthdate.Hours()/24)) + " days")
// Hours
wf.NewItem(humanize.Comma(int64(timeSinceBirthdate.Hours())) + " hours")
// Minutes
wf.NewItem(humanize.Comma(int64(timeSinceBirthdate.Minutes())) + " minutes")
// Seconds
wf.NewItem(humanize.Comma(int64(timeSinceBirthdate.Seconds())) + " seconds")
query = os.Args[1]
if query != "" {
wf.Filter(query)
}
wf.WarnEmpty("No matching items", "Try a different query?")
wf.SendFeedback()
}
return nil
}