-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (51 loc) · 1.16 KB
/
main.go
File metadata and controls
63 lines (51 loc) · 1.16 KB
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
package main
import (
"flag"
"fmt"
"os"
"time"
)
func main() {
mark := flag.String("mark", "2019-09-01T00:00:00+05:30", "The remaining time for the countdown timer in RFC3339 format (e.g. 2019-09-01T00:00:00+05:30)")
flag.Parse()
if *mark == "" {
flag.PrintDefaults()
os.Exit(1)
}
v, err := time.Parse(time.RFC3339, *mark)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for range time.Tick(1 * time.Second) {
timeRemaining := getTimeRemaining(v)
if timeRemaining.t <= 0 {
fmt.Println("Countdown reached!")
break
}
fmt.Printf("%02d Days, %02d Hours, %02d Minutes, %02d Seconds. \n", timeRemaining.d, timeRemaining.h, timeRemaining.m, timeRemaining.s)
}
}
type countdown struct {
t int
d int
h int
m int
s int
}
func getTimeRemaining(t time.Time) countdown {
currentTime := time.Now()
difference := t.Sub(currentTime)
total := int(difference.Seconds())
days := int(total / (60 * 60 * 24))
hours := int(total / (60 * 60) % 24)
minutes := int(total/60) % 60
seconds := int(total % 60)
return countdown{
t: total,
d: days,
h: hours,
m: minutes,
s: seconds,
}
}