-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
54 lines (44 loc) · 1.86 KB
/
main.go
File metadata and controls
54 lines (44 loc) · 1.86 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
// The `so/time` package provides time-related types
// and functions, similar to Go's `time` package.
package main
import "solod.dev/so/time"
func main() {
// Pre-allocate a buffer for string formatting.
buf := make([]byte, 64)
// We'll start by getting the current time.
now := time.Now()
println("now =", now.String(buf))
// You can build a Time struct by providing the year, month, day,
// etc. Times are always in UTC. You can provide a UTC offset in
// seconds, and the time will be adjusted accordingly.
then := time.Date(2026, 2, 1, 12, 34, 56, 555555555, time.UTC)
println("then =", then.String(buf))
// Extract the various components of the time value.
println("now year =", now.Year())
println("now month =", now.Month())
println("now day =", now.Day())
println("now hour =", now.Hour())
println("now minute =", now.Minute())
println("now second =", now.Second())
println("now nano =", now.Nanosecond())
// Weekday returns 1 for Monday, 2 for Tuesday, and so on.
println("now weekday =", now.Weekday())
// These methods compare two times to check if the first one
// happens before, after, or at the same time as the second.
println("then before now =", then.Before(now))
println("then after now =", then.After(now))
println("then equal now =", then.Equal(now))
// The Sub methods returns a `Duration` representing
// the interval between two times.
diff := now.Sub(then)
println("diff (now - then) =", diff.String(buf))
// Calculate the duration length in different units.
println("diff hours =", diff.Hours())
println("diff minutes =", diff.Minutes())
println("diff seconds =", diff.Seconds())
println("diff nanos =", diff.Nanoseconds())
// Use Add to advance a time by a given duration
// (or go back by using a negative duration).
println("then + diff =", then.Add(diff).String(buf))
println("then - diff =", then.Add(-diff).String(buf))
}