fix: ввод даты, ввод формата
This commit is contained in:
@@ -8,6 +8,9 @@ import (
|
||||
|
||||
"github.com/NicoNex/echotron/v3"
|
||||
"github.com/TelegramExchange/tgex-backend/tg_bot/bot"
|
||||
"github.com/olebedev/when"
|
||||
"github.com/olebedev/when/rules/common"
|
||||
"github.com/olebedev/when/rules/ru"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -381,6 +384,7 @@ func (s *DateTimePicker) handleDaySelect(payload string, b *bot.Bot) {
|
||||
s.view = pickerViewTimeCombined
|
||||
s.selectedHour = nil
|
||||
s.selectedMinute = nil
|
||||
s.Enter(b, bot.EditMessage)
|
||||
} else {
|
||||
// Сразу подтверждаем выбор для даты без времени
|
||||
s.confirmSelection(b)
|
||||
@@ -629,192 +633,52 @@ func (s *DateTimePicker) Handle(_ *bot.Bot, _ *echotron.Update) { return }
|
||||
|
||||
func (s *DateTimePicker) Exit() {}
|
||||
|
||||
var whenParser *when.Parser
|
||||
|
||||
func init() {
|
||||
whenParser = when.New(nil)
|
||||
whenParser.Add(ru.All...)
|
||||
whenParser.Add(common.All...)
|
||||
}
|
||||
|
||||
var (
|
||||
timeIndicator = regexp.MustCompile(`\d{1,2}[:.]\d{2}|утр|вечер|днём|ночь|час|минут|полдень|полночь`)
|
||||
dateIndicator = regexp.MustCompile(`\d{1,2}[./-]\d|сегодня|завтра|вчера|послезавтра|понедельн|вторник|сред[уыа]|четверг|пятниц|суббот|воскресен|янв|фев|мар|апр|ма[йя]|июн|июл|авг|сен|окт|ноя|дек|через.*дн|через.*недел|через.*месяц|через.*год`)
|
||||
)
|
||||
|
||||
func detectTime(matched string) bool {
|
||||
return timeIndicator.MatchString(matched)
|
||||
}
|
||||
|
||||
func detectDate(matched string) bool {
|
||||
return dateIndicator.MatchString(matched)
|
||||
}
|
||||
|
||||
func parseDateTimeInput(
|
||||
input string,
|
||||
currentDate *time.Time,
|
||||
currentHour *int,
|
||||
currentMinute *int,
|
||||
_ *time.Time,
|
||||
_ *int,
|
||||
_ *int,
|
||||
allowPast bool,
|
||||
) (time.Time, bool, bool) {
|
||||
text := strings.ToLower(strings.TrimSpace(input))
|
||||
text = strings.ReplaceAll(text, ",", " ")
|
||||
text = strings.ReplaceAll(text, " ", " ")
|
||||
|
||||
now := time.Now().In(MskLocation)
|
||||
baseDate := now
|
||||
if currentDate != nil {
|
||||
baseDate = currentDate.In(MskLocation)
|
||||
}
|
||||
|
||||
date, dateOk := parseDate(text, baseDate)
|
||||
hour, minute, timeOk := parseTime(text, currentHour, currentMinute)
|
||||
|
||||
if !dateOk && !timeOk {
|
||||
r, err := whenParser.Parse(input, now)
|
||||
if err != nil || r == nil {
|
||||
return time.Time{}, false, false
|
||||
}
|
||||
if !dateOk {
|
||||
date = time.Date(baseDate.Year(), baseDate.Month(), baseDate.Day(), 0, 0, 0, 0, MskLocation)
|
||||
}
|
||||
if !timeOk {
|
||||
hour = 0
|
||||
minute = 0
|
||||
}
|
||||
|
||||
value := time.Date(date.Year(), date.Month(), date.Day(), hour, minute, 0, 0, MskLocation)
|
||||
if !allowPast && value.Before(now) {
|
||||
result := r.Time.In(MskLocation)
|
||||
matched := strings.ToLower(r.Text)
|
||||
|
||||
hasDate := detectDate(matched)
|
||||
hasTime := detectTime(matched)
|
||||
|
||||
if !allowPast && result.Before(now) {
|
||||
return time.Time{}, false, false
|
||||
}
|
||||
return value, dateOk, timeOk
|
||||
}
|
||||
|
||||
func parseDate(text string, baseDate time.Time) (time.Time, bool) {
|
||||
if strings.Contains(text, "сегодня") {
|
||||
return baseDate, true
|
||||
}
|
||||
if strings.Contains(text, "завтра") {
|
||||
return baseDate.AddDate(0, 0, 1), true
|
||||
}
|
||||
if strings.Contains(text, "послезавтра") {
|
||||
return baseDate.AddDate(0, 0, 2), true
|
||||
}
|
||||
|
||||
if date := parseNumericDate(text); !date.IsZero() {
|
||||
return date, true
|
||||
}
|
||||
if date := parseWordDate(text); !date.IsZero() {
|
||||
return date, true
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
func parseNumericDate(text string) time.Time {
|
||||
reYMD := regexp.MustCompile(`\b(\d{4})[./-](\d{1,2})[./-](\d{1,2})\b`)
|
||||
if match := reYMD.FindStringSubmatch(text); len(match) == 4 {
|
||||
year := parseInt(match[1])
|
||||
month := parseInt(match[2])
|
||||
day := parseInt(match[3])
|
||||
return safeDate(year, month, day)
|
||||
}
|
||||
|
||||
reDMY := regexp.MustCompile(`\b(\d{1,2})[./-](\d{1,2})(?:[./-](\d{2,4}))?\b`)
|
||||
if match := reDMY.FindStringSubmatch(text); len(match) >= 3 {
|
||||
day := parseInt(match[1])
|
||||
month := parseInt(match[2])
|
||||
year := time.Now().In(MskLocation).Year()
|
||||
if len(match) == 4 && match[3] != "" {
|
||||
year = parseInt(match[3])
|
||||
if year < 100 {
|
||||
year += 2000
|
||||
}
|
||||
}
|
||||
return safeDate(year, month, day)
|
||||
}
|
||||
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func parseWordDate(text string) time.Time {
|
||||
monthMap := map[string]time.Month{
|
||||
"янв": 1, "январ": 1, "января": 1,
|
||||
"фев": 2, "феврал": 2, "февраля": 2,
|
||||
"мар": 3, "марта": 3,
|
||||
"апр": 4, "апрел": 4, "апреля": 4,
|
||||
"май": 5, "мая": 5,
|
||||
"июн": 6, "июня": 6,
|
||||
"июл": 7, "июля": 7,
|
||||
"авг": 8, "август": 8, "августа": 8,
|
||||
"сен": 9, "сент": 9, "сентябр": 9, "сентября": 9,
|
||||
"окт": 10, "октябр": 10, "октября": 10,
|
||||
"ноя": 11, "ноябр": 11, "ноября": 11,
|
||||
"дек": 12, "декабр": 12, "декабря": 12,
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`\b(\d{1,2})\s+([a-zа-яё]+)(?:\s+(\d{2,4}))?\b`)
|
||||
match := re.FindStringSubmatch(text)
|
||||
if len(match) == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
day := parseInt(match[1])
|
||||
monthToken := match[2]
|
||||
var month time.Month
|
||||
for key, value := range monthMap {
|
||||
if strings.HasPrefix(monthToken, key) {
|
||||
month = value
|
||||
break
|
||||
}
|
||||
}
|
||||
if month == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
year := time.Now().In(MskLocation).Year()
|
||||
if len(match) == 4 && match[3] != "" {
|
||||
year = parseInt(match[3])
|
||||
if year < 100 {
|
||||
year += 2000
|
||||
}
|
||||
}
|
||||
|
||||
return safeDate(year, int(month), day)
|
||||
}
|
||||
|
||||
func parseTime(text string, currentHour *int, currentMinute *int) (int, int, bool) {
|
||||
re := regexp.MustCompile(`\b(\d{1,2})[:.](\d{2})\b`)
|
||||
if match := re.FindStringSubmatch(text); len(match) == 3 {
|
||||
hour := parseInt(match[1])
|
||||
minute := parseInt(match[2])
|
||||
if hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 {
|
||||
return hour, minute, true
|
||||
}
|
||||
}
|
||||
|
||||
reCompact := regexp.MustCompile(`\b(\d{1,2})\s+(\d{2})\b`)
|
||||
if match := reCompact.FindStringSubmatch(text); len(match) == 3 {
|
||||
hour := parseInt(match[1])
|
||||
minute := parseInt(match[2])
|
||||
if hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 {
|
||||
return hour, minute, true
|
||||
}
|
||||
}
|
||||
|
||||
reHour := regexp.MustCompile(`\b(\d{1,2})\s*(?:ч|час|часов)\b`)
|
||||
if match := reHour.FindStringSubmatch(text); len(match) == 2 {
|
||||
hour := parseInt(match[1])
|
||||
if hour >= 0 && hour <= 23 {
|
||||
minute := 0
|
||||
if currentMinute != nil {
|
||||
minute = *currentMinute
|
||||
}
|
||||
return hour, minute, true
|
||||
}
|
||||
}
|
||||
|
||||
reJustHour := regexp.MustCompile(`\b(\d{1,2})\b`)
|
||||
if match := reJustHour.FindStringSubmatch(text); len(match) == 2 && strings.Contains(text, "в") {
|
||||
hour := parseInt(match[1])
|
||||
if hour >= 0 && hour <= 23 {
|
||||
minute := 0
|
||||
if currentMinute != nil {
|
||||
minute = *currentMinute
|
||||
}
|
||||
return hour, minute, true
|
||||
}
|
||||
}
|
||||
|
||||
if currentHour != nil && currentMinute != nil {
|
||||
return *currentHour, *currentMinute, true
|
||||
}
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
func safeDate(year, month, day int) time.Time {
|
||||
if month < 1 || month > 12 {
|
||||
return time.Time{}
|
||||
}
|
||||
if day < 1 || day > 31 {
|
||||
return time.Time{}
|
||||
}
|
||||
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, MskLocation)
|
||||
return result, hasDate, hasTime
|
||||
}
|
||||
|
||||
func WeekdayName(day time.Weekday) string {
|
||||
|
||||
Reference in New Issue
Block a user