summaryrefslogtreecommitdiff
path: root/textproto
diff options
context:
space:
mode:
Diffstat (limited to 'textproto')
-rw-r--r--textproto/types.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/textproto/types.go b/textproto/types.go
index 2109517..9a1731b 100644
--- a/textproto/types.go
+++ b/textproto/types.go
@@ -2,10 +2,13 @@ package textproto
import (
"fmt"
+ "strconv"
"strings"
"time"
)
+// BUG(lukeshu): Only supports the "raw" date format (not "rfc2822" or
+// "now")
type UserTime struct {
Name string
Email string
@@ -28,6 +31,45 @@ func (ut UserTime) String() string {
}
}
+func ParseUserTime(str string) (UserTime, error) {
+ ret := UserTime{}
+ lt := strings.IndexAny(str, "<>")
+ if lt < 0 || str[lt] != '<' {
+ return ret, fmt.Errorf("Missing < in ident string: %v", str)
+ }
+ if lt > 0 {
+ if str[lt-1] != ' ' {
+ return ret, fmt.Errorf("Missing space before < in ident string: %v", str)
+ }
+ ret.Name = str[:lt-1]
+ }
+ gt := lt + 1 + strings.IndexAny(str[lt+1:], "<>")
+ if gt < lt+1 || str[gt] != '>' {
+ return ret, fmt.Errorf("Missing > in ident string: %v", str)
+ }
+ if str[gt+1] != ' ' {
+ return ret, fmt.Errorf("Missing space after > in ident string: %v", str)
+ }
+ ret.Email = str[lt+1 : gt]
+
+ strWhen := str[gt+2:]
+ sp := strings.IndexByte(strWhen, ' ')
+ if sp < 0 {
+ return ret, fmt.Errorf("missing time zone in when: %v", str)
+ }
+ sec, err := strconv.ParseInt(strWhen[:sp], 10, 64)
+ if err != nil {
+ return ret, err
+ }
+ tzt, err := time.Parse("-0700", strWhen[sp+1:])
+ if err != nil {
+ return ret, err
+ }
+ ret.Time = time.Unix(sec, 0).In(tzt.Location())
+
+ return ret, nil
+}
+
type Mode uint32 // 18 bits
var (