-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48_time.go
More file actions
55 lines (45 loc) · 1.1 KB
/
48_time.go
File metadata and controls
55 lines (45 loc) · 1.1 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
package gobyexample
import (
"fmt"
"time"
)
// TimeDemo - Demonstrates usage of time in Go.
func TimeDemo() {
p := fmt.Println
// get current time
now := time.Now()
p(now)
// we can build a `time` struct by providing year, month, day, etc.
// times are always associated with a `Location`, i.e. timezone
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)
// extract various components of time value
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
// Monday to Sunday `Weekday` is also available
p(then.Weekday())
// compare two times
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
// the `Sub` methods return a `Duration` representing the interval
// between two times
diff := now.Sub(then)
p(diff)
// compute length of duration in various units
p(diff.Hours())
p(diff.Minutes())
p(diff.Minutes())
p(diff.Nanoseconds())
// use `Add` to advance a time by a given duration, or with a `-`
// to move backwards by a duration
p(then.Add(diff))
p(then.Add(-diff))
}