-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for the RU date format #46
Merged
+266
−29
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
dbed0f7
add ru date
rturovtsev 55ecc30
fix
rturovtsev 249ad75
fix
rturovtsev b39555c
fix dot date without time
rturovtsev 148e583
add date without year
rturovtsev 93f4ab7
fix tests
rturovtsev f047d79
revert test zh/casual_date
rturovtsev 47cb3e8
fix test
rturovtsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package ru | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/olebedev/when/rules" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// https://go.dev/play/p/YsVdaraCwIP | ||
|
||
func Date(s rules.Strategy) rules.Rule { | ||
return &rules.F{ | ||
RegExp: regexp.MustCompile(`(?i)(?:\b|^)(\d{1,2})\s*(` + MONTHS_PATTERN + `)(?:\s*(\d{4}))?(?:\s*в\s*(\d{1,2}):(\d{2}))?(?:\b|$)`), | ||
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) { | ||
if (c.Day != nil || c.Month != nil || c.Year != nil) || s != rules.Override { | ||
return false, nil | ||
} | ||
|
||
day, err := strconv.Atoi(m.Captures[0]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "date rule: day") | ||
} | ||
|
||
month, ok := MONTHS[strings.ToLower(m.Captures[1])] | ||
if !ok { | ||
return false, errors.New("date rule: invalid month") | ||
} | ||
|
||
year := time.Now().Year() | ||
if m.Captures[2] != "" { | ||
year, err = strconv.Atoi(m.Captures[2]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "date rule: year") | ||
} | ||
} | ||
|
||
hour, minute := 0, 0 | ||
if m.Captures[3] != "" && m.Captures[4] != "" { | ||
hour, err = strconv.Atoi(m.Captures[3]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "date rule: hour") | ||
} | ||
minute, err = strconv.Atoi(m.Captures[4]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "date rule: minute") | ||
} | ||
} | ||
|
||
c.Day = &day | ||
c.Month = pointerToInt(int(month)) | ||
c.Year = &year | ||
c.Hour = &hour | ||
c.Minute = &minute | ||
|
||
return true, nil | ||
}, | ||
} | ||
} | ||
|
||
func pointerToInt(v int) *int { | ||
return &v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ru_test | ||
|
||
import ( | ||
"github.com/olebedev/when" | ||
"github.com/olebedev/when/rules" | ||
"github.com/olebedev/when/rules/ru" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDate(t *testing.T) { | ||
w := when.New(nil) | ||
w.Add(ru.Date(rules.Override)) | ||
|
||
fixt := []Fixture{ | ||
// Simple dates | ||
{"встреча 15 января 2024", 15, "15 января 2024", time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC).Sub(null)}, | ||
{"5 марта 2025 запланирована встреча", 0, "5 марта 2025", time.Date(2025, 3, 5, 0, 0, 0, 0, time.UTC).Sub(null)}, | ||
{"31 декабря 2023", 0, "31 декабря 2023", time.Date(2023, 12, 31, 0, 0, 0, 0, time.UTC).Sub(null)}, | ||
|
||
// Dates with time | ||
{"15 января 2024 в 9:30", 0, "15 января 2024 в 9:30", time.Date(2024, 1, 15, 9, 30, 0, 0, time.UTC).Sub(null)}, | ||
{"5 марта 2025 в 15:00 запланирована встреча", 0, "5 марта 2025 в 15:00", time.Date(2025, 3, 5, 15, 0, 0, 0, time.UTC).Sub(null)}, | ||
{"31 декабря 2023 в 23:59", 0, "31 декабря 2023 в 23:59", time.Date(2023, 12, 31, 23, 59, 0, 0, time.UTC).Sub(null)}, | ||
} | ||
|
||
ApplyFixtures(t, "ru.Date", w, fixt) | ||
} | ||
|
||
func TestDateNil(t *testing.T) { | ||
w := when.New(nil) | ||
w.Add(ru.Date(rules.Override)) | ||
|
||
fixt := []Fixture{ | ||
{"это текст без даты", 0, "", 0}, | ||
{"15", 0, "", 0}, | ||
{"15 чего-то", 0, "", 0}, | ||
} | ||
|
||
ApplyFixturesNil(t, "ru.Date nil", w, fixt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ru | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/olebedev/when/rules" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// https://go.dev/play/p/vRzLhHHupUJ | ||
|
||
func DotDateTime(s rules.Strategy) rules.Rule { | ||
return &rules.F{ | ||
RegExp: regexp.MustCompile(`(?i)(?:^|\b)(\d{2})\.(\d{2})\.(\d{4})(?:\s+(\d{2}):(\d{2}))?(?:\b|$)`), | ||
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) { | ||
if (c.Day != nil || c.Month != nil || c.Year != nil || c.Hour != nil || c.Minute != nil) && s != rules.Override { | ||
return false, nil | ||
} | ||
|
||
day, err := strconv.Atoi(m.Captures[0]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "dot date time rule: day") | ||
} | ||
|
||
month, err := strconv.Atoi(m.Captures[1]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "dot date time rule: month") | ||
} | ||
|
||
year, err := strconv.Atoi(m.Captures[2]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "dot date time rule: year") | ||
} | ||
|
||
hour, minute := 0, 0 | ||
if m.Captures[3] != "" && m.Captures[4] != "" { | ||
hour, err = strconv.Atoi(m.Captures[3]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "dot date time rule: hour") | ||
} | ||
minute, err = strconv.Atoi(m.Captures[4]) | ||
if err != nil { | ||
return false, errors.Wrap(err, "dot date time rule: minute") | ||
} | ||
} | ||
|
||
if day > 0 && day <= 31 && month > 0 && month <= 12 { | ||
c.Day = &day | ||
c.Month = &month | ||
c.Year = &year | ||
c.Hour = &hour | ||
c.Minute = &minute | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ru_test | ||
|
||
import ( | ||
"github.com/olebedev/when" | ||
"github.com/olebedev/when/rules" | ||
"github.com/olebedev/when/rules/ru" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDotDateTime(t *testing.T) { | ||
w := when.New(nil) | ||
w.Add(ru.DotDateTime(rules.Override)) | ||
|
||
fixt := []Fixture{ | ||
// Basic date/time formats | ||
{"встреча 15.01.2024 09:30", 15, "15.01.2024 09:30", time.Date(2024, 1, 15, 9, 30, 0, 0, time.UTC).Sub(null)}, | ||
{"05.03.2025 15:00 запланирована встреча", 0, "05.03.2025 15:00", time.Date(2025, 3, 5, 15, 0, 0, 0, time.UTC).Sub(null)}, | ||
{"31.12.2023 23:59", 0, "31.12.2023 23:59", time.Date(2023, 12, 31, 23, 59, 0, 0, time.UTC).Sub(null)}, | ||
} | ||
|
||
ApplyFixtures(t, "ru.DateTime", w, fixt) | ||
} | ||
|
||
func TestDotDateTimeNil(t *testing.T) { | ||
w := when.New(nil) | ||
w.Add(ru.DotDateTime(rules.Override)) | ||
|
||
fixt := []Fixture{ | ||
{"это текст без даты и времени", 0, "", 0}, | ||
{"15.01", 0, "", 0}, | ||
{"32.01.2024 15:00", 0, "", 0}, // некорректный день | ||
{"15.13.2024 15:00", 0, "", 0}, // некорректный месяц | ||
} | ||
|
||
ApplyFixturesNil(t, "ru.DateTime nil", w, fixt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is occasional, right? If so, can you please move it back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have restored the changes, although with this version, my tests in zh/casual_date_test are currently not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the reversion may have been a mistake, as my tests are not passing in zh/casual_date_test. Could we consider removing the reversion to ensure the tests function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rturovtsev I looked into it and found that it is a bug. You can remove this line safely :)
I'm not sure why the original author in PR #30 added this line, but it is this line that fails the test.
The function
zh_test.ApplyFixtures
uses the time2022/03/14 00:00:00 UTC
asnow
, but this line uses a new variable that shadows it. We are now in 2024, a leap year, so the result is 86,400 seconds more than "expected" (in the year 2022). 😂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rturovtsev, then ca you please remove this line then so all the test are passing.
@RexSkz, thanks for clarification, I was not too sure what is going on on that side of the project. I should have noticed this earlier, my bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@olebedev Hi! Can you tell me if there are plans to create a new release?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi there, no plans whatsoever, but I will roll this change out as a new tag soon-ish, in a matter of one or two days, bear with me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rturovtsev, here you go https://github.com/olebedev/when/releases/tag/v1.1.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you =)