1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "bufio"
|
---|
5 | "fmt"
|
---|
6 | "io"
|
---|
7 | "os"
|
---|
8 | "path/filepath"
|
---|
9 | "sort"
|
---|
10 | "strings"
|
---|
11 | "time"
|
---|
12 |
|
---|
13 | "git.sr.ht/~sircmpwn/go-bare"
|
---|
14 | "gopkg.in/irc.v3"
|
---|
15 | )
|
---|
16 |
|
---|
17 | const fsMessageStoreMaxTries = 100
|
---|
18 |
|
---|
19 | func escapeFilename(unsafe string) (safe string) {
|
---|
20 | if unsafe == "." {
|
---|
21 | return "-"
|
---|
22 | } else if unsafe == ".." {
|
---|
23 | return "--"
|
---|
24 | } else {
|
---|
25 | return strings.NewReplacer("/", "-", "\\", "-").Replace(unsafe)
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | type date struct {
|
---|
30 | Year, Month, Day int
|
---|
31 | }
|
---|
32 |
|
---|
33 | func newDate(t time.Time) date {
|
---|
34 | year, month, day := t.Date()
|
---|
35 | return date{year, int(month), day}
|
---|
36 | }
|
---|
37 |
|
---|
38 | func (d date) Time() time.Time {
|
---|
39 | return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, time.Local)
|
---|
40 | }
|
---|
41 |
|
---|
42 | type fsMsgID struct {
|
---|
43 | Date date
|
---|
44 | Offset bare.Int
|
---|
45 | }
|
---|
46 |
|
---|
47 | func (fsMsgID) msgIDType() msgIDType {
|
---|
48 | return msgIDFS
|
---|
49 | }
|
---|
50 |
|
---|
51 | func parseFSMsgID(s string) (netID int64, entity string, t time.Time, offset int64, err error) {
|
---|
52 | var id fsMsgID
|
---|
53 | netID, entity, err = parseMsgID(s, &id)
|
---|
54 | if err != nil {
|
---|
55 | return 0, "", time.Time{}, 0, err
|
---|
56 | }
|
---|
57 | return netID, entity, id.Date.Time(), int64(id.Offset), nil
|
---|
58 | }
|
---|
59 |
|
---|
60 | func formatFSMsgID(netID int64, entity string, t time.Time, offset int64) string {
|
---|
61 | id := fsMsgID{
|
---|
62 | Date: newDate(t),
|
---|
63 | Offset: bare.Int(offset),
|
---|
64 | }
|
---|
65 | return formatMsgID(netID, entity, &id)
|
---|
66 | }
|
---|
67 |
|
---|
68 | // fsMessageStore is a per-user on-disk store for IRC messages.
|
---|
69 | type fsMessageStore struct {
|
---|
70 | root string
|
---|
71 |
|
---|
72 | files map[string]*os.File // indexed by entity
|
---|
73 | }
|
---|
74 |
|
---|
75 | var _ messageStore = (*fsMessageStore)(nil)
|
---|
76 | var _ chatHistoryMessageStore = (*fsMessageStore)(nil)
|
---|
77 |
|
---|
78 | func newFSMessageStore(root, username string) *fsMessageStore {
|
---|
79 | return &fsMessageStore{
|
---|
80 | root: filepath.Join(root, escapeFilename(username)),
|
---|
81 | files: make(map[string]*os.File),
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | func (ms *fsMessageStore) logPath(network *network, entity string, t time.Time) string {
|
---|
86 | year, month, day := t.Date()
|
---|
87 | filename := fmt.Sprintf("%04d-%02d-%02d.log", year, month, day)
|
---|
88 | return filepath.Join(ms.root, escapeFilename(network.GetName()), escapeFilename(entity), filename)
|
---|
89 | }
|
---|
90 |
|
---|
91 | // nextMsgID queries the message ID for the next message to be written to f.
|
---|
92 | func nextFSMsgID(network *network, entity string, t time.Time, f *os.File) (string, error) {
|
---|
93 | offset, err := f.Seek(0, io.SeekEnd)
|
---|
94 | if err != nil {
|
---|
95 | return "", fmt.Errorf("failed to query next FS message ID: %v", err)
|
---|
96 | }
|
---|
97 | return formatFSMsgID(network.ID, entity, t, offset), nil
|
---|
98 | }
|
---|
99 |
|
---|
100 | func (ms *fsMessageStore) LastMsgID(network *network, entity string, t time.Time) (string, error) {
|
---|
101 | p := ms.logPath(network, entity, t)
|
---|
102 | fi, err := os.Stat(p)
|
---|
103 | if os.IsNotExist(err) {
|
---|
104 | return formatFSMsgID(network.ID, entity, t, -1), nil
|
---|
105 | } else if err != nil {
|
---|
106 | return "", fmt.Errorf("failed to query last FS message ID: %v", err)
|
---|
107 | }
|
---|
108 | return formatFSMsgID(network.ID, entity, t, fi.Size()-1), nil
|
---|
109 | }
|
---|
110 |
|
---|
111 | func (ms *fsMessageStore) Append(network *network, entity string, msg *irc.Message) (string, error) {
|
---|
112 | s := formatMessage(msg)
|
---|
113 | if s == "" {
|
---|
114 | return "", nil
|
---|
115 | }
|
---|
116 |
|
---|
117 | var t time.Time
|
---|
118 | if tag, ok := msg.Tags["time"]; ok {
|
---|
119 | var err error
|
---|
120 | t, err = time.Parse(serverTimeLayout, string(tag))
|
---|
121 | if err != nil {
|
---|
122 | return "", fmt.Errorf("failed to parse message time tag: %v", err)
|
---|
123 | }
|
---|
124 | t = t.In(time.Local)
|
---|
125 | } else {
|
---|
126 | t = time.Now()
|
---|
127 | }
|
---|
128 |
|
---|
129 | // TODO: enforce maximum open file handles (LRU cache of file handles)
|
---|
130 | f := ms.files[entity]
|
---|
131 |
|
---|
132 | // TODO: handle non-monotonic clock behaviour
|
---|
133 | path := ms.logPath(network, entity, t)
|
---|
134 | if f == nil || f.Name() != path {
|
---|
135 | if f != nil {
|
---|
136 | f.Close()
|
---|
137 | }
|
---|
138 |
|
---|
139 | dir := filepath.Dir(path)
|
---|
140 | if err := os.MkdirAll(dir, 0750); err != nil {
|
---|
141 | return "", fmt.Errorf("failed to create message logs directory %q: %v", dir, err)
|
---|
142 | }
|
---|
143 |
|
---|
144 | var err error
|
---|
145 | f, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640)
|
---|
146 | if err != nil {
|
---|
147 | return "", fmt.Errorf("failed to open message log file %q: %v", path, err)
|
---|
148 | }
|
---|
149 |
|
---|
150 | ms.files[entity] = f
|
---|
151 | }
|
---|
152 |
|
---|
153 | msgID, err := nextFSMsgID(network, entity, t, f)
|
---|
154 | if err != nil {
|
---|
155 | return "", fmt.Errorf("failed to generate message ID: %v", err)
|
---|
156 | }
|
---|
157 |
|
---|
158 | _, err = fmt.Fprintf(f, "[%02d:%02d:%02d] %s\n", t.Hour(), t.Minute(), t.Second(), s)
|
---|
159 | if err != nil {
|
---|
160 | return "", fmt.Errorf("failed to log message to %q: %v", f.Name(), err)
|
---|
161 | }
|
---|
162 |
|
---|
163 | return msgID, nil
|
---|
164 | }
|
---|
165 |
|
---|
166 | func (ms *fsMessageStore) Close() error {
|
---|
167 | var closeErr error
|
---|
168 | for _, f := range ms.files {
|
---|
169 | if err := f.Close(); err != nil {
|
---|
170 | closeErr = fmt.Errorf("failed to close message store: %v", err)
|
---|
171 | }
|
---|
172 | }
|
---|
173 | return closeErr
|
---|
174 | }
|
---|
175 |
|
---|
176 | // formatMessage formats a message log line. It assumes a well-formed IRC
|
---|
177 | // message.
|
---|
178 | func formatMessage(msg *irc.Message) string {
|
---|
179 | switch strings.ToUpper(msg.Command) {
|
---|
180 | case "NICK":
|
---|
181 | return fmt.Sprintf("*** %s is now known as %s", msg.Prefix.Name, msg.Params[0])
|
---|
182 | case "JOIN":
|
---|
183 | return fmt.Sprintf("*** Joins: %s (%s@%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host)
|
---|
184 | case "PART":
|
---|
185 | var reason string
|
---|
186 | if len(msg.Params) > 1 {
|
---|
187 | reason = msg.Params[1]
|
---|
188 | }
|
---|
189 | return fmt.Sprintf("*** Parts: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
---|
190 | case "KICK":
|
---|
191 | nick := msg.Params[1]
|
---|
192 | var reason string
|
---|
193 | if len(msg.Params) > 2 {
|
---|
194 | reason = msg.Params[2]
|
---|
195 | }
|
---|
196 | return fmt.Sprintf("*** %s was kicked by %s (%s)", nick, msg.Prefix.Name, reason)
|
---|
197 | case "QUIT":
|
---|
198 | var reason string
|
---|
199 | if len(msg.Params) > 0 {
|
---|
200 | reason = msg.Params[0]
|
---|
201 | }
|
---|
202 | return fmt.Sprintf("*** Quits: %s (%s@%s) (%s)", msg.Prefix.Name, msg.Prefix.User, msg.Prefix.Host, reason)
|
---|
203 | case "TOPIC":
|
---|
204 | var topic string
|
---|
205 | if len(msg.Params) > 1 {
|
---|
206 | topic = msg.Params[1]
|
---|
207 | }
|
---|
208 | return fmt.Sprintf("*** %s changes topic to '%s'", msg.Prefix.Name, topic)
|
---|
209 | case "MODE":
|
---|
210 | return fmt.Sprintf("*** %s sets mode: %s", msg.Prefix.Name, strings.Join(msg.Params[1:], " "))
|
---|
211 | case "NOTICE":
|
---|
212 | return fmt.Sprintf("-%s- %s", msg.Prefix.Name, msg.Params[1])
|
---|
213 | case "PRIVMSG":
|
---|
214 | if cmd, params, ok := parseCTCPMessage(msg); ok && cmd == "ACTION" {
|
---|
215 | return fmt.Sprintf("* %s %s", msg.Prefix.Name, params)
|
---|
216 | } else {
|
---|
217 | return fmt.Sprintf("<%s> %s", msg.Prefix.Name, msg.Params[1])
|
---|
218 | }
|
---|
219 | default:
|
---|
220 | return ""
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | func parseMessage(line, entity string, ref time.Time) (*irc.Message, time.Time, error) {
|
---|
225 | var hour, minute, second int
|
---|
226 | _, err := fmt.Sscanf(line, "[%02d:%02d:%02d] ", &hour, &minute, &second)
|
---|
227 | if err != nil {
|
---|
228 | return nil, time.Time{}, fmt.Errorf("malformed timestamp prefix: %v", err)
|
---|
229 | }
|
---|
230 | line = line[11:]
|
---|
231 |
|
---|
232 | var cmd, sender, text string
|
---|
233 | if strings.HasPrefix(line, "<") {
|
---|
234 | cmd = "PRIVMSG"
|
---|
235 | parts := strings.SplitN(line[1:], "> ", 2)
|
---|
236 | if len(parts) != 2 {
|
---|
237 | return nil, time.Time{}, nil
|
---|
238 | }
|
---|
239 | sender, text = parts[0], parts[1]
|
---|
240 | } else if strings.HasPrefix(line, "-") {
|
---|
241 | cmd = "NOTICE"
|
---|
242 | parts := strings.SplitN(line[1:], "- ", 2)
|
---|
243 | if len(parts) != 2 {
|
---|
244 | return nil, time.Time{}, nil
|
---|
245 | }
|
---|
246 | sender, text = parts[0], parts[1]
|
---|
247 | } else if strings.HasPrefix(line, "* ") {
|
---|
248 | cmd = "PRIVMSG"
|
---|
249 | parts := strings.SplitN(line[2:], " ", 2)
|
---|
250 | if len(parts) != 2 {
|
---|
251 | return nil, time.Time{}, nil
|
---|
252 | }
|
---|
253 | sender, text = parts[0], "\x01ACTION "+parts[1]+"\x01"
|
---|
254 | } else {
|
---|
255 | return nil, time.Time{}, nil
|
---|
256 | }
|
---|
257 |
|
---|
258 | year, month, day := ref.Date()
|
---|
259 | t := time.Date(year, month, day, hour, minute, second, 0, time.Local)
|
---|
260 |
|
---|
261 | msg := &irc.Message{
|
---|
262 | Tags: map[string]irc.TagValue{
|
---|
263 | "time": irc.TagValue(t.UTC().Format(serverTimeLayout)),
|
---|
264 | },
|
---|
265 | Prefix: &irc.Prefix{Name: sender},
|
---|
266 | Command: cmd,
|
---|
267 | Params: []string{entity, text},
|
---|
268 | }
|
---|
269 | return msg, t, nil
|
---|
270 | }
|
---|
271 |
|
---|
272 | func (ms *fsMessageStore) parseMessagesBefore(network *network, entity string, ref time.Time, end time.Time, limit int, afterOffset int64) ([]*irc.Message, error) {
|
---|
273 | path := ms.logPath(network, entity, ref)
|
---|
274 | f, err := os.Open(path)
|
---|
275 | if err != nil {
|
---|
276 | if os.IsNotExist(err) {
|
---|
277 | return nil, nil
|
---|
278 | }
|
---|
279 | return nil, fmt.Errorf("failed to parse messages before ref: %v", err)
|
---|
280 | }
|
---|
281 | defer f.Close()
|
---|
282 |
|
---|
283 | historyRing := make([]*irc.Message, limit)
|
---|
284 | cur := 0
|
---|
285 |
|
---|
286 | sc := bufio.NewScanner(f)
|
---|
287 |
|
---|
288 | if afterOffset >= 0 {
|
---|
289 | if _, err := f.Seek(afterOffset, io.SeekStart); err != nil {
|
---|
290 | return nil, nil
|
---|
291 | }
|
---|
292 | sc.Scan() // skip till next newline
|
---|
293 | }
|
---|
294 |
|
---|
295 | for sc.Scan() {
|
---|
296 | msg, t, err := parseMessage(sc.Text(), entity, ref)
|
---|
297 | if err != nil {
|
---|
298 | return nil, err
|
---|
299 | } else if msg == nil || !t.After(end) {
|
---|
300 | continue
|
---|
301 | } else if !t.Before(ref) {
|
---|
302 | break
|
---|
303 | }
|
---|
304 |
|
---|
305 | historyRing[cur%limit] = msg
|
---|
306 | cur++
|
---|
307 | }
|
---|
308 | if sc.Err() != nil {
|
---|
309 | return nil, fmt.Errorf("failed to parse messages before ref: scanner error: %v", sc.Err())
|
---|
310 | }
|
---|
311 |
|
---|
312 | n := limit
|
---|
313 | if cur < limit {
|
---|
314 | n = cur
|
---|
315 | }
|
---|
316 | start := (cur - n + limit) % limit
|
---|
317 |
|
---|
318 | if start+n <= limit { // ring doesnt wrap
|
---|
319 | return historyRing[start : start+n], nil
|
---|
320 | } else { // ring wraps
|
---|
321 | history := make([]*irc.Message, n)
|
---|
322 | r := copy(history, historyRing[start:])
|
---|
323 | copy(history[r:], historyRing[:n-r])
|
---|
324 | return history, nil
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | func (ms *fsMessageStore) parseMessagesAfter(network *network, entity string, ref time.Time, end time.Time, limit int) ([]*irc.Message, error) {
|
---|
329 | path := ms.logPath(network, entity, ref)
|
---|
330 | f, err := os.Open(path)
|
---|
331 | if err != nil {
|
---|
332 | if os.IsNotExist(err) {
|
---|
333 | return nil, nil
|
---|
334 | }
|
---|
335 | return nil, fmt.Errorf("failed to parse messages after ref: %v", err)
|
---|
336 | }
|
---|
337 | defer f.Close()
|
---|
338 |
|
---|
339 | var history []*irc.Message
|
---|
340 | sc := bufio.NewScanner(f)
|
---|
341 | for sc.Scan() && len(history) < limit {
|
---|
342 | msg, t, err := parseMessage(sc.Text(), entity, ref)
|
---|
343 | if err != nil {
|
---|
344 | return nil, err
|
---|
345 | } else if msg == nil || !t.After(ref) {
|
---|
346 | continue
|
---|
347 | } else if !t.Before(end) {
|
---|
348 | break
|
---|
349 | }
|
---|
350 |
|
---|
351 | history = append(history, msg)
|
---|
352 | }
|
---|
353 | if sc.Err() != nil {
|
---|
354 | return nil, fmt.Errorf("failed to parse messages after ref: scanner error: %v", sc.Err())
|
---|
355 | }
|
---|
356 |
|
---|
357 | return history, nil
|
---|
358 | }
|
---|
359 |
|
---|
360 | func (ms *fsMessageStore) LoadBeforeTime(network *network, entity string, start time.Time, end time.Time, limit int) ([]*irc.Message, error) {
|
---|
361 | history := make([]*irc.Message, limit)
|
---|
362 | remaining := limit
|
---|
363 | tries := 0
|
---|
364 | for remaining > 0 && tries < fsMessageStoreMaxTries && end.Before(start) {
|
---|
365 | buf, err := ms.parseMessagesBefore(network, entity, start, end, remaining, -1)
|
---|
366 | if err != nil {
|
---|
367 | return nil, err
|
---|
368 | }
|
---|
369 | if len(buf) == 0 {
|
---|
370 | tries++
|
---|
371 | } else {
|
---|
372 | tries = 0
|
---|
373 | }
|
---|
374 | copy(history[remaining-len(buf):], buf)
|
---|
375 | remaining -= len(buf)
|
---|
376 | year, month, day := start.Date()
|
---|
377 | start = time.Date(year, month, day, 0, 0, 0, 0, start.Location()).Add(-1)
|
---|
378 | }
|
---|
379 |
|
---|
380 | return history[remaining:], nil
|
---|
381 | }
|
---|
382 |
|
---|
383 | func (ms *fsMessageStore) LoadAfterTime(network *network, entity string, start time.Time, end time.Time, limit int) ([]*irc.Message, error) {
|
---|
384 | var history []*irc.Message
|
---|
385 | remaining := limit
|
---|
386 | tries := 0
|
---|
387 | for remaining > 0 && tries < fsMessageStoreMaxTries && start.Before(end) {
|
---|
388 | buf, err := ms.parseMessagesAfter(network, entity, start, end, remaining)
|
---|
389 | if err != nil {
|
---|
390 | return nil, err
|
---|
391 | }
|
---|
392 | if len(buf) == 0 {
|
---|
393 | tries++
|
---|
394 | } else {
|
---|
395 | tries = 0
|
---|
396 | }
|
---|
397 | history = append(history, buf...)
|
---|
398 | remaining -= len(buf)
|
---|
399 | year, month, day := start.Date()
|
---|
400 | start = time.Date(year, month, day+1, 0, 0, 0, 0, start.Location())
|
---|
401 | }
|
---|
402 | return history, nil
|
---|
403 | }
|
---|
404 |
|
---|
405 | func (ms *fsMessageStore) LoadLatestID(network *network, entity, id string, limit int) ([]*irc.Message, error) {
|
---|
406 | var afterTime time.Time
|
---|
407 | var afterOffset int64
|
---|
408 | if id != "" {
|
---|
409 | var idNet int64
|
---|
410 | var idEntity string
|
---|
411 | var err error
|
---|
412 | idNet, idEntity, afterTime, afterOffset, err = parseFSMsgID(id)
|
---|
413 | if err != nil {
|
---|
414 | return nil, err
|
---|
415 | }
|
---|
416 | if idNet != network.ID || idEntity != entity {
|
---|
417 | return nil, fmt.Errorf("cannot find message ID: message ID doesn't match network/entity")
|
---|
418 | }
|
---|
419 | }
|
---|
420 |
|
---|
421 | history := make([]*irc.Message, limit)
|
---|
422 | t := time.Now()
|
---|
423 | remaining := limit
|
---|
424 | tries := 0
|
---|
425 | for remaining > 0 && tries < fsMessageStoreMaxTries && !truncateDay(t).Before(afterTime) {
|
---|
426 | var offset int64 = -1
|
---|
427 | if afterOffset >= 0 && truncateDay(t).Equal(afterTime) {
|
---|
428 | offset = afterOffset
|
---|
429 | }
|
---|
430 |
|
---|
431 | buf, err := ms.parseMessagesBefore(network, entity, t, time.Time{}, remaining, offset)
|
---|
432 | if err != nil {
|
---|
433 | return nil, err
|
---|
434 | }
|
---|
435 | if len(buf) == 0 {
|
---|
436 | tries++
|
---|
437 | } else {
|
---|
438 | tries = 0
|
---|
439 | }
|
---|
440 | copy(history[remaining-len(buf):], buf)
|
---|
441 | remaining -= len(buf)
|
---|
442 | year, month, day := t.Date()
|
---|
443 | t = time.Date(year, month, day, 0, 0, 0, 0, t.Location()).Add(-1)
|
---|
444 | }
|
---|
445 |
|
---|
446 | return history[remaining:], nil
|
---|
447 | }
|
---|
448 |
|
---|
449 | func (ms *fsMessageStore) ListTargets(network *network, start, end time.Time, limit int) ([]chatHistoryTarget, error) {
|
---|
450 | rootPath := filepath.Join(ms.root, escapeFilename(network.GetName()))
|
---|
451 | root, err := os.Open(rootPath)
|
---|
452 | if err != nil {
|
---|
453 | return nil, err
|
---|
454 | }
|
---|
455 |
|
---|
456 | // The returned targets are escaped, and there is no way to un-escape
|
---|
457 | // TODO: switch to ReadDir (Go 1.16+)
|
---|
458 | targetNames, err := root.Readdirnames(0)
|
---|
459 | root.Close()
|
---|
460 | if err != nil {
|
---|
461 | return nil, err
|
---|
462 | }
|
---|
463 |
|
---|
464 | var targets []chatHistoryTarget
|
---|
465 | for _, target := range targetNames {
|
---|
466 | // target is already escaped here
|
---|
467 | targetPath := filepath.Join(rootPath, target)
|
---|
468 | targetDir, err := os.Open(targetPath)
|
---|
469 | if err != nil {
|
---|
470 | return nil, err
|
---|
471 | }
|
---|
472 |
|
---|
473 | entries, err := targetDir.Readdir(0)
|
---|
474 | targetDir.Close()
|
---|
475 | if err != nil {
|
---|
476 | return nil, err
|
---|
477 | }
|
---|
478 |
|
---|
479 | // We use mtime here, which may give imprecise or incorrect results
|
---|
480 | var t time.Time
|
---|
481 | for _, entry := range entries {
|
---|
482 | if entry.ModTime().After(t) {
|
---|
483 | t = entry.ModTime()
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | // The timestamps we get from logs have second granularity
|
---|
488 | t = truncateSecond(t)
|
---|
489 |
|
---|
490 | // Filter out targets that don't fullfil the time bounds
|
---|
491 | if !isTimeBetween(t, start, end) {
|
---|
492 | continue
|
---|
493 | }
|
---|
494 |
|
---|
495 | targets = append(targets, chatHistoryTarget{
|
---|
496 | Name: target,
|
---|
497 | LatestMessage: t,
|
---|
498 | })
|
---|
499 | }
|
---|
500 |
|
---|
501 | // Sort targets by latest message time, backwards or forwards depending on
|
---|
502 | // the order of the time bounds
|
---|
503 | sort.Slice(targets, func(i, j int) bool {
|
---|
504 | t1, t2 := targets[i].LatestMessage, targets[j].LatestMessage
|
---|
505 | if start.Before(end) {
|
---|
506 | return t1.Before(t2)
|
---|
507 | } else {
|
---|
508 | return !t1.Before(t2)
|
---|
509 | }
|
---|
510 | })
|
---|
511 |
|
---|
512 | // Truncate the result if necessary
|
---|
513 | if len(targets) > limit {
|
---|
514 | targets = targets[:limit]
|
---|
515 | }
|
---|
516 |
|
---|
517 | return targets, nil
|
---|
518 | }
|
---|
519 |
|
---|
520 | func truncateDay(t time.Time) time.Time {
|
---|
521 | year, month, day := t.Date()
|
---|
522 | return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
---|
523 | }
|
---|
524 |
|
---|
525 | func truncateSecond(t time.Time) time.Time {
|
---|
526 | year, month, day := t.Date()
|
---|
527 | return time.Date(year, month, day, t.Hour(), t.Minute(), t.Second(), 0, t.Location())
|
---|
528 | }
|
---|
529 |
|
---|
530 | func isTimeBetween(t, start, end time.Time) bool {
|
---|
531 | if end.Before(start) {
|
---|
532 | end, start = start, end
|
---|
533 | }
|
---|
534 | return start.Before(t) && t.Before(end)
|
---|
535 | }
|
---|