-
Notifications
You must be signed in to change notification settings - Fork 5
/
offset.go
51 lines (40 loc) · 953 Bytes
/
offset.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
package rfc5322
import (
"fmt"
"strings"
"time"
"github.com/ProtonMail/go-rfc5322/parser"
)
type offset struct {
rep string
value int
}
func (w *walker) EnterOffset(ctx *parser.OffsetContext) {
text := ctx.GetText()
// NOTE: RFC5322 date-time should always begin with + or -
// but we relax that requirement a bit due to many messages
// in the wild that skip the +; we add the "+" if missing.
if !strings.HasPrefix(text, "+") && !strings.HasPrefix(text, "-") {
text = "+" + text
}
sgn := text[0:1]
hrs := text[1:3]
min := text[3:5]
dur, err := time.ParseDuration(fmt.Sprintf("%v%vh%vm", sgn, hrs, min))
if err != nil {
w.err = err
}
w.enter(&offset{
rep: text,
value: int(dur.Seconds()),
})
}
func (w *walker) ExitOffset(ctx *parser.OffsetContext) {
type withOffset interface {
withOffset(*offset)
}
res := w.exit().(*offset)
if parent, ok := w.parent().(withOffset); ok {
parent.withOffset(res)
}
}