-
Notifications
You must be signed in to change notification settings - Fork 13
/
cmd_dates.go
215 lines (185 loc) · 3.86 KB
/
cmd_dates.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
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
207
208
209
210
211
212
213
214
215
//
// Copyright (c) 2017 Dean Jackson <[email protected]>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-11-25
//
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
aw "github.com/deanishe/awgo"
)
var (
oneDay = time.Hour * 24
oneWeek = oneDay * 7
today = midnight(time.Now())
tomorrow = midnight(today.AddDate(0, 0, 1))
yesterday = midnight(today.AddDate(0, 0, -1))
parseRegex = regexp.MustCompile(`^(\+|-)?(\d+)(d|w)?$`)
)
// doDates shows a list of dates in Alfred.
func doDates() error {
if len(accounts) == 0 {
wf.NewItem("No Accounts Configured").
Subtitle("Action this item to add a Google account").
Autocomplete("workflow:login").
Icon(aw.IconWarning)
wf.SendFeedback()
return nil
}
var parsed bool
if t, ok := parseDate(opts.DateFormat); ok {
parsed = true
short := t.Format(timeFormat)
long := t.Format(timeFormatLong)
wf.NewItem(long).
Subtitle(relativeDays(t, false)).
Arg(short).
Autocomplete(short).
Valid(true).
Icon(iconDefault)
} else {
for i := -3; i < 4; i++ {
var (
t = midnight(today.Add(oneDay * time.Duration(i)))
long = t.Format(timeFormatLong)
short = t.Format(timeFormat)
icon = iconDefault
)
if t.Equal(today) {
icon = iconCalToday
}
wf.NewItem(relativeDays(t, true)).
Subtitle(short).
Match(long + " " + t.Format("Monday")).
Arg(short).
Autocomplete(short).
Valid(true).
Icon(icon)
}
}
if !parsed && opts.DateFormat != "" {
_ = wf.Filter(opts.DateFormat)
}
wf.WarnEmpty("Invalid date", "Format is YYYY-MM-DD, YYYMMDD or [+|-]NN[d|w]")
wf.SendFeedback()
return nil
}
// Return midnight in local timezone for given Time.
func midnight(t time.Time) time.Time {
s := t.Local().Format(timeFormat)
m, err := time.ParseInLocation(timeFormat, s, time.Local)
if err != nil {
panic(err)
}
return m
}
// parse string into Time. Boolean is true if parsing was successful.
func parseDate(s string) (time.Time, bool) {
s = strings.TrimSpace(s)
if s == "" {
return time.Time{}, false
}
if t, err := time.ParseInLocation(timeFormat, s, time.Local); err == nil {
return t, true
}
if t, err := time.ParseInLocation("20060102", s, time.Local); err == nil {
return t, true
}
// Parse custom format [+|-]NN[d|w]
var (
add = true
delta time.Duration
t time.Time
unit = "d"
)
m := parseRegex.FindStringSubmatch(s)
if m == nil {
return time.Time{}, false
}
// Sign
if m[1] == "-" {
add = false
}
// Count
n, err := strconv.Atoi(m[2])
if err != nil {
return time.Time{}, false
}
if n == 0 {
return today, true
}
// Optional unit
if m[3] != "" {
unit = m[3]
}
// Calculate date
if unit == "d" {
delta = oneDay * time.Duration(n)
} else {
delta = oneWeek * time.Duration(n)
}
if add {
t = today.Add(delta)
} else {
t = today.Add(-delta)
}
return midnight(t), true
}
// Return Time as "x day(s) ago" or "in x day(s)"
func relativeDays(t time.Time, names bool) string {
var (
d time.Duration
days int
)
if t.Before(today) {
d = today.Sub(t)
} else if t.After(today) {
d = t.Sub(today)
} else {
return "Today"
}
days = int(d.Hours() / 24)
// Return day name
if names {
if days == 1 {
if t.Before(today) {
return "Yesterday"
}
return "Tomorrow"
}
return t.Format("Monday")
}
var (
format string
unit = "days"
)
// Return in N day(s) or N day(s) ago
format = "%d %s ago"
if t.After(today) {
format = "in %d %s"
}
if days == 1 {
unit = "day"
}
return fmt.Sprintf(format, days, unit)
}
// relativeDate returns Yesterday, Today, Tomorrow or long date.
func relativeDate(t time.Time) string {
t = midnight(t)
if t.Equal(today) {
return "Today"
}
if t.Equal(yesterday) {
return "Yesterday"
}
if t.Equal(tomorrow) {
return "Tomorrow"
}
return t.Format("Monday, 2 Jan 2006")
}