-
Notifications
You must be signed in to change notification settings - Fork 4
/
date.go
65 lines (54 loc) · 1.38 KB
/
date.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
// Copyright (C) 2014 Constantin Schomburg <me@cschomburg.com>
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package commands
import (
"strconv"
"time"
"github.com/sarifsystems/sarif/pkg/util"
"github.com/sarifsystems/sarif/sarif"
)
func printTime(t time.Time) string {
return t.Local().Format(time.RFC1123) + "\n" +
t.UTC().Format(time.RFC1123) + "\n" +
t.Local().Format(time.RFC3339)
}
func (s *Service) handleDate(msg sarif.Message) {
text := msg.Text
if text == "" {
s.ReplyText(msg, printTime(time.Now()))
return
}
if t := util.ParseTime(text, time.Now()); !t.IsZero() {
s.ReplyText(msg, printTime(t))
return
}
d, err := util.ParseDuration(text)
if err != nil {
s.ReplyBadRequest(msg, err)
return
}
s.ReplyText(msg, printTime(time.Now().Add(d)))
}
func (s *Service) handleUnix(msg sarif.Message) {
text := msg.Text
if text == "" {
s.ReplyText(msg, strconv.FormatInt(time.Now().Unix(), 10))
return
}
if u, err := strconv.ParseInt(text, 10, 64); err == nil {
s.ReplyText(msg, printTime(time.Unix(u, 0)))
return
}
if t := util.ParseTime(text, time.Now()); !t.IsZero() {
s.ReplyText(msg, strconv.FormatInt(t.Unix(), 10))
return
}
d, err := util.ParseDuration(text)
if err != nil {
s.ReplyBadRequest(msg, err)
return
}
s.ReplyText(msg, strconv.FormatInt(time.Now().Add(d).Unix(), 10))
}