-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasicsdate.go
64 lines (50 loc) · 1.08 KB
/
basicsdate.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
package msgsvr
import (
"fmt"
"strconv"
"strings"
"github.com/kralamoure/retroproto"
)
type BasicsDate struct {
Year int
Month int
Day int
}
func NewBasicsDate(extra string) (BasicsDate, error) {
var m BasicsDate
if err := m.Deserialize(extra); err != nil {
return BasicsDate{}, fmt.Errorf("could not deserialize message: %w", err)
}
return m, nil
}
func (m BasicsDate) MessageId() retroproto.MsgSvrId {
return retroproto.BasicsDate
}
func (m BasicsDate) MessageName() string {
return "BasicsDate"
}
func (m BasicsDate) Serialized() (string, error) {
return fmt.Sprintf("%d|%d|%d", m.Year, m.Month-1, m.Day), nil
}
func (m *BasicsDate) Deserialize(extra string) error {
sli := strings.Split(extra, "|")
if len(sli) < 3 {
return retroproto.ErrInvalidMsg
}
year, err := strconv.ParseInt(sli[0], 10, 32)
if err != nil {
return err
}
m.Year = int(year)
month, err := strconv.ParseInt(sli[1], 10, 32)
if err != nil {
return err
}
m.Month = int(month + 1)
day, err := strconv.ParseInt(sli[2], 10, 32)
if err != nil {
return err
}
m.Day = int(day)
return nil
}