-
Notifications
You must be signed in to change notification settings - Fork 1
/
date.go
53 lines (46 loc) · 847 Bytes
/
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
package nmea
import (
"fmt"
"time"
)
type Date struct {
Year int
Month time.Month
Day int
}
func (d Date) String() string {
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}
func (t *Tokenizer) CommaDate() Date {
t.Comma()
return t.Date()
}
func (t *Tokenizer) CommaOptionalDate() Optional[Date] {
t.Comma()
return t.OptionalDate()
}
func (t *Tokenizer) Date() Date {
day := t.DecimalDigits(2)
month := time.Month(t.DecimalDigits(2))
year := 1900 + t.DecimalDigits(2)
if year < 1993 {
year += 100
}
return Date{
Year: year,
Month: month,
Day: day,
}
}
func (t *Tokenizer) OptionalDate() Optional[Date] {
if t.err != nil {
return Optional[Date]{}
}
if t.pos == len(t.data) {
return Optional[Date]{}
}
if t.data[t.pos] == ',' {
return Optional[Date]{}
}
return NewOptional(t.Date())
}