forked from vbauerster/60-days-of-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrstrange.go
53 lines (44 loc) · 1.39 KB
/
drstrange.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
package main
import (
"fmt"
"math"
"time"
)
func main() {
// Sleep 2 seconds A detail, this instruction will block process
// until finnish
time.Sleep(2 * time.Second)
// christmas
christmas := time.Date(2017, 12, 25, 0, 0, 0, 0, time.UTC)
println(christmas.Day())
println(christmas.Year())
// week day
fmt.Println(christmas.Weekday())
// duration until christmas
fmt.Println("days until christmas: ", math.Trunc(time.Until(christmas).Hours()/24))
// check if date already passed
fmt.Println("already passed: ", christmas.Before(time.Now()))
// date after 3 days
now := time.Now()
days := time.Duration(24 * 3)
future := now.Add(days * time.Hour)
fmt.Println(future)
// timer will block for 5 seconds
timer := time.NewTimer(time.Second * 5)
// on each second ticker will "tick"
ticker := time.NewTicker(time.Second)
// actual hour
initial := time.Now()
go func() {
// iterate over "ticks" and print number of seconds passed
for _ = range ticker.C {
fmt.Printf("\r try %d: Dormammu, i've come to bargain............☠", int(time.Since(initial).Seconds()))
}
}()
// block until timer happens
<-timer.C
fmt.Printf("\rtry %d: Dormammu, i've come to bargain............OK", int(time.Since(initial).Seconds()))
fmt.Print("\nThe earth is safe again.")
// If you want to see more about dates, see the docs: https://golang.org/pkg/time/
// here too: https://gobyexample.com/time
}