-
Notifications
You must be signed in to change notification settings - Fork 2
/
person.go
50 lines (41 loc) · 889 Bytes
/
person.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
package go4git
import (
"fmt"
"strconv"
"strings"
"time"
)
func trims(s string) string {
return strings.Trim(s, " ")
}
func SplitTwoAndTrim(s string, sep string) (string, string) {
xs := strings.SplitN(s, sep, 2)
if len(xs) == 2 {
return trims(xs[0]), trims(xs[1])
} else {
return "", trims(xs[0])
}
}
type Person struct {
Name string
Email string
Time time.Time
}
func (p *Person) String() string {
return fmt.Sprintf("%s <%s> %s", p.Name, p.Email, p.Time)
}
func parseTime(s string) (*time.Time, error) {
ts, _ := SplitTwoAndTrim(s, " ")
i, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return nil, err
}
tm := time.Unix(i, 0)
return &tm, nil
}
func parsePerson(s string) *Person {
name, rest := SplitTwoAndTrim(s, "<")
email, rest := SplitTwoAndTrim(rest, ">")
tm, _ := parseTime(rest)
return &Person{Name: name, Email: email, Time: *tm}
}