-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
53 lines (45 loc) · 1.01 KB
/
utils.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
package ticketswitch
import (
"bytes"
"encoding/json"
"time"
)
var (
// IndentOutput controls wether or not JSON output is indented.
IndentOutput = true
// EscapeHTML controls wether json output is HTML escaped.
EscapeHTML = false
)
// marshal is a custom json marshaller that conditionally turns off html
// escaping and applies indentation.
func marshal(v interface{}) ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
e := json.NewEncoder(buf)
if IndentOutput {
e.SetIndent("", " ")
}
if EscapeHTML {
e.SetEscapeHTML(false)
}
err := e.Encode(v)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// DateRange returns a string representation of a date range between two
// time.Times
func DateRange(from, to time.Time) string {
if from.IsZero() && to.IsZero() {
return ""
}
buf := bytes.NewBufferString("")
if !from.IsZero() {
buf.WriteString(from.Format("20060102"))
}
buf.WriteString(":")
if !to.IsZero() {
buf.WriteString(to.Format("20060102"))
}
return buf.String()
}