File: casual_date.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 (38 lines) | stat: -rw-r--r-- 923 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
package ru

import (
	"regexp"
	"strings"
	"time"

	"github.com/olebedev/when/rules"
)

// https://play.golang.org/p/QrFtjmjUoJ

func CasualDate(s rules.Strategy) rules.Rule {
	return &rules.F{
		RegExp: regexp.MustCompile("(?i)(?:\\P{L}|^)" +
			"((?:до|прямо)\\s+)?" +
			"(сейчас|сегодня|завтра|вчера)" +
			"(?:\\P{L}|$)"),
		Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {
			lower := strings.ToLower(strings.TrimSpace(m.String()))

			switch {
			case strings.Contains(lower, "сегодня"):
				// c.Hour = pointer.ToInt(18)
			case strings.Contains(lower, "завтра"):
				if c.Duration == 0 || s == rules.Override {
					c.Duration += time.Hour * 24
				}
			case strings.Contains(lower, "вчера"):
				if c.Duration == 0 || s == rules.Override {
					c.Duration -= time.Hour * 24
				}
			}

			return true, nil
		},
	}
}