-
Notifications
You must be signed in to change notification settings - Fork 0
/
isodate.go
163 lines (143 loc) · 4.29 KB
/
isodate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
/*******************************************************************************
Output the current date and time in a formatted way.
Author: [email protected]
*******************************************************************************/
import (
"time"
"flag"
"fmt"
)
/* pulled from documentation
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
*/
var (
verbose *bool
test_now time.Time
)
func main() {
dump := flag.Bool("dump", false, "dump all dates in all formats with names")
format := flag.String("format", "iso", "defaults to ISO, format to output")
verbose = flag.Bool("verbose", false, "verbose")
flag.Parse()
if *dump {
Dump()
} else {
Action(*format)
}
}
/******************************************************************************/
// #mark - Actions
/**
Primary action, what is run by default. Displays one date string in the
requested format
@param format to display
*/
func Action (format string) {
selectedFormat := NowByFormat (format)
vf("%s = ", format)
fmt.Println(Now().Format(selectedFormat))
}
/**
Additional action, displays all the date formats names and outputs
*/
func Dump() {
vln("Default format is 'iso' which is an alias for 3339:")
DumpLine("iso")
vln("\nDump of all other formats")
DumpLine("1123")
DumpLine("1123z")
DumpLine("3339")
DumpLine("3339nano")
DumpLine("822")
DumpLine("822z")
DumpLine("850")
DumpLine("ansic")
DumpLine("kitchen")
DumpLine("micro")
DumpLine("milli")
DumpLine("nano")
DumpLine("ruby")
DumpLine("stamp")
DumpLine("unix")
}
/******************************************************************************/
// #mark - Helpers
/**
writes a "dump" line to the console, writes the format name and the result
@param format date format name to output
*/
func DumpLine(format string) {
var now = NowByFormat(format)
fmt.Printf("%9s = %s\n", format, Now().Format(now))
}
/*
gives the current time in the requested format
@param format date format to output
@return the current date formatted in the requested format
*/
func NowByFormat(format string) string{
var now = "unknown"
switch (format) {
case "ansic" : now = time.ANSIC
case "unix" : now = time.UnixDate
case "ruby" : now = time.RubyDate
case "822" : now = time.RFC822
case "822z" : now = time.RFC822Z
case "850" : now = time.RFC850
case "1123" : now = time.RFC1123
case "1123z" : now = time.RFC1123Z
case "iso", "3339" : now = time.RFC3339
case "3339nano" : now = time.RFC3339Nano
case "kitchen" : now = time.Kitchen
case "stamp" : now = time.Stamp
case "milli" : now = time.StampMilli
case "micro" : now = time.StampMicro
case "nano" : now = time.StampNano
default: now = time.RFC3339
}
return now
}
func Now() time.Time {
if test_now.Year() != 1 {
return test_now
} else {
return time.Now()
}
}
/**
Prints a message line to the console if app is in verbose mode
@param msg message to print out
*/
func vln(msg string) {
if *verbose {
fmt.Println(msg)
}
}
/**
Prints a message using Printf to the console if app is in verbose mode
@param format message format - see Printf()
@param args arguments for the massage format, variable list - see Printf()
*/
func vf(format string, args ...interface{}) {
if *verbose {
fmt.Printf(format, args...)
}
}