-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtime.go
More file actions
59 lines (39 loc) · 1.39 KB
/
Copy pathtime.go
File metadata and controls
59 lines (39 loc) · 1.39 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
package main
import (
"fmt"
"time"
)
func main() {
// https://golang.org/pkg/time
// Date parsing is peculiar in Go. Instead of using symbols to represent a date format, you need to remember a particular date and time:
//
// Jan 2 15:04:05 2006 MST
//
// (MST is UTC -0700)
// A 'layout' is then that date/time expressed in the format you want to capture
layout := "2006-01-02" //This is YYYY-MM-YY because 01 in the epcoh is January
value := "2019-04-30"
if d, err := time.Parse(layout, value); err == nil {
fmt.Printf("%v\n", d)
} else {
//Unparseable
fmt.Println(err.Error())
}
// Layouts without time components will result in a date-time with a 0000 UTC time
layout = "1 January 2006 3pm MST"
value = "2 February 2019 9am GMT"
if d, err := time.Parse(layout, value); err == nil {
fmt.Printf("%v\n", d)
} else {
//Unparseable
fmt.Println(err.Error())
}
// Intervals between two date-times are represented as a time.Duration which is an int64. The raw value is in nano seconds
// To convert between the nano second value and a higher order unit (ms, second, minute, hour) use the constants in the time package
start := time.Now()
time.Sleep(2 * time.Second)
end := time.Since(start)
fmt.Printf("Elaspsed in nano seconds %d\n", end)
fmt.Printf("Elapsed using fmt's default format %v\n", end)
fmt.Printf("Elapsed in milliseconds %d\n", end/time.Millisecond)
}