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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
const usage = `usage: when <time>
This program converts the given time into various formats (currently a local
timestamp, UTC timestamp, and UNIX epoch). It tries to guess what the input is.
Some valid queries:
2024-01-05
1715079241
tomorrow 5PM
-22h
-7h10m
Mar 15
Sep 3 18:00
For now a single timestamp and a single duration (which is added either to the
current time, or the given time) is supported.`
func printTime(t time.Time) {
fmt.Println("Local:", t.Format("Mon 02 January 2006 at 15:04:05 MST"))
fmt.Println("UTC: ", t.UTC().Format(time.RFC3339))
fmt.Println("UNIX: ", t.Unix())
}
type FieldSet uint8
const (
SetYear FieldSet = 1 << iota
SetDay
SetMonth
SetHour
SetMinute
SetSecond
SetLocation
)
const (
SetDate = SetYear | SetDay | SetMonth
SetClock = SetHour | SetMinute | SetSecond
)
// mergeTimes returns a new time.Time with all fields in this overridden with the
// specified fields from that.
func mergeTimes(this time.Time, that time.Time, set FieldSet) time.Time {
year, month, day := this.Date()
hour, min, sec := this.Clock()
loc := this.Location()
if set&SetYear == SetYear {
year = that.Year()
}
if set&SetMonth == SetMonth {
month = that.Month()
}
if set&SetDay == SetDay {
day = that.Day()
}
if set&SetHour == SetHour {
hour = that.Hour()
}
if set&SetMinute == SetMinute {
min = that.Minute()
}
if set&SetSecond == SetSecond {
sec = that.Second()
}
if set&SetLocation == SetLocation {
loc = that.Location()
}
return time.Date(year, month, day, hour, min, sec, 0, loc)
}
func parseTime(input string) (time.Time, error) {
// try unix times
if i, err := strconv.ParseInt(input, 10, 64); err == nil {
if i < 9999999999 {
return time.Unix(i, 0), nil
}
if i < 9999999999999 {
return time.UnixMilli(i), nil
}
}
// try simple date/time formats
if t, err := time.Parse(time.DateOnly, input); err == nil {
return t, nil
}
if t, err := time.Parse(time.Kitchen, input); err == nil {
now := time.Now()
return mergeTimes(now, t, SetClock), nil
}
if t, err := time.Parse(time.TimeOnly, input); err == nil {
now := time.Now()
return mergeTimes(now, t, SetClock), nil
}
if t, err := time.Parse("15:04", input); err == nil {
now := time.Now()
return mergeTimes(now, t, SetClock), nil
}
if t, err := time.Parse("3PM", input); err == nil {
now := time.Now()
return mergeTimes(now, t, SetClock), nil
}
if t, err := time.Parse(time.DateTime, input); err == nil {
return t, nil
}
if t, err := time.Parse(time.Stamp, input); err == nil {
now := time.Now()
return mergeTimes(t, now, SetYear|SetLocation), nil
}
if t, err := time.Parse("Jan _2 15:04", input); err == nil {
now := time.Now()
return mergeTimes(t, now, SetYear|SetLocation), nil
}
if t, err := time.Parse("Jan _2", input); err == nil {
now := time.Now()
return mergeTimes(t, now, SetYear|SetLocation), nil
}
return time.Time{}, fmt.Errorf("could not parse time: %q", input)
}
func parseDuration(input string) (time.Duration, error) {
// some simple rewriting
switch input {
case "yesterday":
input = "-24h"
case "tomorrow":
input = "24h"
case "today", "now":
return time.Duration(0), nil
}
// TODO: days, months, weeks, ...
return time.ParseDuration(input)
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
var d time.Duration
var t time.Time
var err error
var haveTime, haveDuration bool
// Try to parse entire input as one full thing, before getting more
// clever.
if t, err = parseTime(strings.Join(os.Args[1:], " ")); err == nil {
printTime(t)
return
}
for _, arg := range os.Args[1:] {
if !haveTime {
if t, err = parseTime(arg); err == nil {
haveTime = true
continue
}
}
if !haveDuration {
if d, err = parseDuration(arg); err == nil {
haveDuration = true
continue
}
}
}
if err != nil {
fmt.Fprintln(os.Stderr, "Not sure what you want, try another time.")
os.Exit(1)
}
if haveTime && haveDuration {
printTime(t.Add(d))
} else if haveTime {
printTime(t)
} else if haveDuration {
printTime(time.Now().Add(d))
} else {
fmt.Fprintln(os.Stderr, "Not sure what you want, try another time.")
os.Exit(1)
}
}
|