File: past_time.go

package info (click to toggle)
golang-github-olebedev-when 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,939 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package en

import (
	"regexp"
	"strconv"
	"strings"
	"time"

	"github.com/AlekSi/pointer"
	"github.com/olebedev/when/rules"
	"github.com/pkg/errors"
)

func PastTime(s rules.Strategy) rules.Rule {
	overwrite := s == rules.Override

	return &rules.F{
		RegExp: regexp.MustCompile(
			"(?i)(?:\\W|^)\\s*" +
				"(" + INTEGER_WORDS_PATTERN + "|[0-9]+|an?(?:\\s*few)?|half(?:\\s*an?)?)\\s*" +
				"(seconds?|min(?:ute)?s?|hours?|days?|weeks?|months?|years?) (ago)\\s*" +
				"(?:\\W|$)"),
		Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {

			numStr := strings.TrimSpace(m.Captures[0])

			var num int
			var err error

			if n, ok := INTEGER_WORDS[numStr]; ok {
				num = n
			} else if numStr == "a" || numStr == "an" {
				num = 1
			} else if strings.Contains(numStr, "few") {
				num = 3
			} else if strings.Contains(numStr, "half") {
				// pass
			} else {
				num, err = strconv.Atoi(numStr)
				if err != nil {
					return false, errors.Wrapf(err, "convert '%s' to int", numStr)
				}
			}

			exponent := strings.TrimSpace(m.Captures[1])

			if !strings.Contains(numStr, "half") {
				switch {
				case strings.Contains(exponent, "second"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(time.Duration(num) * time.Second)
					}
				case strings.Contains(exponent, "min"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(time.Duration(num) * time.Minute)
					}
				case strings.Contains(exponent, "hour"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(time.Duration(num) * time.Hour)
					}
				case strings.Contains(exponent, "day"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(time.Duration(num) * 24 * time.Hour)
					}
				case strings.Contains(exponent, "week"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(time.Duration(num) * 7 * 24 * time.Hour)
					}
				case strings.Contains(exponent, "month"):
					if c.Month == nil || overwrite {
						c.Month = pointer.ToInt((int(ref.Month()) - num) % 12)
					}
				case strings.Contains(exponent, "year"):
					if c.Year == nil || overwrite {
						c.Year = pointer.ToInt(ref.Year() - num)
					}
				}
			} else {
				switch {
				case strings.Contains(exponent, "hour"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(30 * time.Minute)
					}
				case strings.Contains(exponent, "day"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(12 * time.Hour)
					}
				case strings.Contains(exponent, "week"):
					if c.Duration == 0 || overwrite {
						c.Duration = -(7 * 12 * time.Hour)
					}
				case strings.Contains(exponent, "month"):
					if c.Duration == 0 || overwrite {
						// 2 weeks
						c.Duration = -(14 * 24 * time.Hour)
					}
				case strings.Contains(exponent, "year"):
					if c.Month == nil || overwrite {
						c.Month = pointer.ToInt((int(ref.Month()) - 6) % 12)
					}
				}
			}

			return true, nil
		},
	}
}