-
Notifications
You must be signed in to change notification settings - Fork 2
/
time.go
executable file
·124 lines (103 loc) · 4.94 KB
/
time.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//usr/bin/env go run "$0" "$@"; exit
/* hello-time.go from https://github.com/wilsonmar/golang-sample
and in Go Playpen: https://play.golang.org/p/v3BElaaAbM
A tutorial for first-time developers of GoLang.
See https://coderwall.com/p/_kdjzq/go-run-run-go-as-a-script-language
https://greatercommons.com/learn/6600110066630656 on time/date
*/
// All Go programs must be part of a package.
// main is the default package.
package main
// import libraries:
// fmt provides basic standard out formatting functions.
import ( // PROTIP: avoid errors with semicolons vs. commas by using a list:
"fmt"
"strconv" // notice semicolons are used. see https://golang.org/pkg/time/
"time" // see https://golang.org/pkg/time/
// "strings"
)
const (
// For use with http://golang.org/pkg/time/#Parse
timeLayout = "2020-01-02 15:04 MST"
)
// The one of a few reasons for a global variable:
var runtime_start = time.Now() // See: http://golang.org/pkg/time/
// A function must match the package name to
// provide an entry point which gets called when run:
func main() {
// Four character indent. gofmt indents using tabs that take 8 characters.
// Go requires a time zone when specifying a date, so build one:
milestone_tz, _ := time.LoadLocation("MST") // You can also use time.UTC constant
milestone_date := time.Date(2009, 11, 10, 23, 15, 4, 5, milestone_tz)
// Format by example does not recognize use of %v
fmt.Println("Go became an open source project on",milestone_date.Format("Mon, Jan 2, 2006 at 3:04pm"))
// No space needs to be added in the text above.
// fmt.Printf("\n") // blank line
// Code in the Go Playground http://play.golang.org/p/m5AWasfhQy
// always returns timestamps at 1257894000 (Tue, 10 Nov 2009 23:00:00),
// the date Go became a public open source project.
now := time.Now()
epoch_start := now.Unix() // Alternate way to define variable
// BLAH: Duration objections do not have days due to timezones
// and daylight savings time (which we think is a cop out but whatever)
// Time Addition/Subtraction returns a Duration object
diff := now.Sub(milestone_date)
diff_days := int(diff.Hours() / 24)
fmt.Printf("That's %d days ago\n", diff_days)
// The Println method from fmt package writes to standard out:
fmt.Printf("from now at %v (default format)",now)
fmt.Printf("\n") // Blank line needs to be specified.
// Use built-in time constants for date formatting
// See Time constans for more: http://golang.org/pkg/time/#pkg-constants
fmt.Println("RFC850: ",now.Format(time.RFC850))
fmt.Println("RFC1123: ",now.Format(time.RFC1123))
// Double quotes within double quotes:
fmt.Printf("That's %v seconds from midnight, January 1, 1970 (\"epoch time\")",epoch_start)
// PROTIP: Put blank lines in a separate line for easier refactoring:
fmt.Printf("\n")
// Construct date of next Christmas:
// TODO: Convert milestone_tz (from above) to string
christmas_text := strconv.Itoa(now.Year()) + "-12-25 00:00 MST" // 0:0:0 won't dd.
//christmas_text := "2017-12-26 12:00 MST" // + milestone_tz
//then, err := time.Parse("2006-01-02 15:04:05", christmas_text)
then, err := time.Parse(timeLayout, christmas_text)
if err != nil {
fmt.Println(">>> ",err)
// return // due to err
}
// Calculate difference in dates from now, made positive:
diff_now := time.Since(then) * -1
// Go does not allow re-definition of the same variable.
// BLAH: Duration objections do not have Days() due to timezones and daylight savings time.
// Time Addition/Subtraction returns a Duration object
//diff_xmas_hours := int(diff_now.Hours())
diff_xmas_days := int(diff_now.Hours() / 24) // 24 hours in a day.
// TODO: Calculate number of hours remaining from days.
fmt.Println("And",diff_xmas_days,"days until next Midnight Christmas on", then.Format("Monday, Jan 2, 2006"))
// See https://golang.org/pkg/fmt/
/*
twodaysDiff := time.Hour * 24 * 2
twodays := now.Add(twodaysDiff)
fmt.Println("Two Days: ", twodays.Format(time.ANSIC))
// Time Conditionals: Use Before() After() Equal() to compare dates:
if milestone_date.Before(now) {
fmt.Println("That is before Now ")
}
*/
runtime_elapsed := time.Since(runtime_start)
fmt.Printf("Execution time: %s microseconds", runtime_elapsed)
// See https://coderwall.com/p/cp5fya/measuring-execution-time-in-go
fmt.Printf("\n") // added so new prompt begins from left margin.
}
// Sample output: $ go run hello-time.go
/*
Go became an open source project on Tue, Nov 10, 2009 at 11:15pm
That's 2713 days ago
from now at 2017-04-16 06:56:01.397340297 -0600 MDT (default format)
RFC850: Sunday, 16-Apr-17 06:56:01 MDT
RFC1123: Sun, 16 Apr 2017 06:56:01 MDT
That's 1492347361 seconds from midnight, January 1, 1970 ("epoch time")
Add 252 days until next Midnight Christmas on Monday, Dec 25, 2017
Execution time: 312.909µs microseconds
*/
// EOF