瀏覽代碼

Prepare for more precise date formats

Lucas Stadler 9 年之前
父節點
當前提交
7d2bf97373
共有 1 個文件被更改,包括 19 次插入8 次删除
  1. 19 8
      go/remind/remind.go

+ 19 - 8
go/remind/remind.go

@ -39,6 +39,9 @@ func parseTime(s string) (time.Time, error) {
39 39
	now := time.Now().Round(time.Second)
40 40
41 41
	parts := strings.Fields(s)
42
	// more indicates the parts index where there might be "more"
43
	// parsable data
44
	more := 0
42 45
43 46
	if len(parts) == 0 {
44 47
		return t, fmt.Errorf("empty date spec")
@ -46,15 +49,17 @@ func parseTime(s string) (time.Time, error) {
46 49
47 50
	switch parts[0] {
48 51
	case "today":
49
		if len(parts) == 1 {
50
			return now, nil
52
		if len(parts) >= 1 {
53
			t = now
54
			more = 1
51 55
		}
52 56
	case "tomorrow":
53
		if len(parts) == 1 {
54
			return now.AddDate(0, 0, 1), nil
57
		if len(parts) >= 1 {
58
			t = now.AddDate(0, 0, 1)
59
			more = 1
55 60
		}
56 61
	case "in":
57
		if len(parts) == 3 {
62
		if len(parts) >= 3 {
58 63
			n, err := parseNumber(parts[1])
59 64
			if err != nil {
60 65
				return t, err
@ -63,20 +68,26 @@ func parseTime(s string) (time.Time, error) {
63 68
			if err != nil {
64 69
				return t, err
65 70
			}
66
			return modifier(n, now), nil
71
			t = modifier(n, now)
72
			more = 3
67 73
		}
68 74
	case "next":
69
		if len(parts) == 2 {
75
		if len(parts) >= 2 {
70 76
			modifier, err := parseModifier(parts[1])
71 77
			if err != nil {
72 78
				return t, err
73 79
			}
74
			return modifier(1, now), nil
80
			t = modifier(1, now)
81
			more = 2
75 82
		}
76 83
	default:
77 84
		return t, fmt.Errorf("unknown date spec '%s'", s)
78 85
	}
79 86
87
	if len(parts) == more {
88
		return t, nil
89
	}
90
80 91
	return t, fmt.Errorf("unknown date spec '%s' (unexpected)", s)
81 92
}
82 93