-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnonsmoking.go
48 lines (37 loc) · 1.04 KB
/
nonsmoking.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
package main
/*
example ./smoke-stop 2016-09-19 30 0.285714286
*/
import (
"time"
"fmt"
"os"
"strconv"
)
func getSmokingString(dateStopped time.Time, cigarettesPerDay int64, pricePerCigarette float64) string {
today := time.Now()
diff := today.Sub(dateStopped)
hours := diff.Hours()
cigarettesNotSmoked := float64(cigarettesPerDay) / float64(24) * float64(hours)
moneySaved := cigarettesNotSmoked * pricePerCigarette
daysWithoutSmoking := hours / 24
return fmt.Sprintf("You stopped smoking %d days ago. Saved %d Euro and you didn't smoked %d cigarettes",
int(daysWithoutSmoking),
int(moneySaved),
int(cigarettesNotSmoked))
}
func main() {
dateStopped, e := time.Parse(time.RFC3339, os.Args[1] + "T00:00:00+00:00")
if (nil != e) {
fmt.Println(e)
}
cigarettesPerDay, e := strconv.ParseInt(os.Args[2], 10, 64)
if (nil != e) {
fmt.Println(e)
}
pricePerCigarette, e := strconv.ParseFloat(os.Args[3], 64)
if (nil != e) {
fmt.Println(e)
}
fmt.Println(getSmokingString(dateStopped, cigarettesPerDay, pricePerCigarette))
}