Skip to content

Commit

Permalink
Merge pull request #5 from tada-team/half-dur
Browse files Browse the repository at this point in the history
Add part of time processing
  • Loading branch information
sdfsdhgjkbmnmxc authored Jun 6, 2021
2 parents 412ae0f + 0991617 commit 21c5d41
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
13 changes: 13 additions & 0 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ func forceInt64(s string) int64 {
return val
}

func forceFloat64(s string) float64 {
s = strings.TrimSpace(s)
s = strings.TrimLeft(s, "0")
if s == "" {
return 0
}
val, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0
}
return val
}

func normalizeStrings(ss []string) []string {
if len(ss) <= 1 {
return ss
Expand Down
29 changes: 23 additions & 6 deletions dateparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,22 @@ func TestDateparse(t *testing.T) {
time.Date(dt.Year(), dt.Month(), dt.Day()+1, 10, 0, 0, 0, dt.Location()),
"тест",
},
"через три минуты тест": {
dt.Add(time.Minute * 3),
"тест",
},
"через пол часа тест": {
dt.Add(time.Minute * 30),
"тест",
},
"через четверть минуты тест": {
dt.Add(time.Second * 15),
"тест",
},
"через полчаса тест": {
dt.Add(time.Minute * 30),
"тест",
},
// FIXME:
//"в субботу в 11 утра": {
// time.Date(dt.Year(), dt.Month(), dt.Day()+7, 11, 0, 0, 0, dt.Location()),
Expand Down Expand Up @@ -627,18 +643,19 @@ func TestDateparse(t *testing.T) {
// "",
//},
} {
got, msg := Parse(k, &Opts{Now: dt})
if got.IsZero() || !got.Equal(want.date) || msg != want.message {
t.Errorf("dateparse error on '%s': got '%s' (comment: '%s') want '%s' (comment: '%s')", k, got, msg, want.date, want.message)
continue
}
t.Run(k, func(t *testing.T) {
got, msg := Parse(k, &Opts{Now: dt})
if got.IsZero() || !got.Equal(want.date) || msg != want.message {
t.Errorf("dateparse error on '%s': got '%s' (comment: '%s') want '%s' (comment: '%s')", k, got, msg, want.date, want.message)
}
})
}
}

// BenchmarkParse-12 15781 76097 ns/op 494 B/op 14 allocs/op
// ==>
// BenchmarkParse-12 16088 75671 ns/op 439 B/op 8 allocs/op
func BenchmarkParse(b *testing.B ) {
func BenchmarkParse(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Parse("сегодня в 18", nil)
Expand Down
37 changes: 32 additions & 5 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

var (
seconds = `сек|секунд|sec|seconds`
minutes = `мин|минут|минуту|min `
minutes = `мин|минут|минуту|минуты|min`
hours = `часов|hours|hour|часа|час`
days = `дней|дня|days`
weeksWords = `недель|неделю|недели|неделя|weeks|week`
Expand All @@ -25,6 +25,8 @@ var (
)

var (
quarter = `quarter|четверть`
half = `half|пол`
one = `one|один`
two = `two|два`
three = `three|три`
Expand All @@ -35,14 +37,18 @@ var (
eight = `eight|восемь`
nine = `nine|девять`
ten = `ten|десять`
wordNumbers = strings.Join([]string{one, two, three, four, five, six, seven, eight, nine, ten}, "|")
wordNumbers = strings.Join([]string{quarter, half, one, two, three, four, five, six, seven, eight, nine, ten}, "|")
)

func checkWordNumber(s string) int64 {
if v := forceInt64(s); v != 0 {
func checkWordNumber(s string) float64 {
if v := forceFloat64(s); v != 0 {
return v
}
switch {
case strings.Contains(quarter, s):
return 0.25
case strings.Contains(half, s):
return 0.5
case strings.Contains(one, s):
return 1
case strings.Contains(two, s):
Expand Down Expand Up @@ -96,6 +102,28 @@ func durationParse(bits []string, opts Opts) (dur time.Duration) {
return
}
word := strings.TrimSpace(bits[1])
if v < 1 && v >= 0 {
div := time.Duration(1 / v)
switch {
case strings.Contains(seconds, word):
return time.Second / div
case strings.Contains(minutes, word):
return time.Minute / div
case strings.Contains(hours, word):
return time.Hour / div
case strings.Contains(days, word):
return time.Hour * 12
case strings.Contains(weeksWords, word):
return time.Hour * 12 * 7
case strings.Contains(monthsWords, word):
return time.Hour * 12 * 31 // XXX:
case strings.Contains(years, word):
return time.Hour * 12 * 365 // XXX:
default:
return durationParse(bits[:1], opts)
}
}

switch {
case strings.Contains(seconds, word):
return time.Duration(v) * time.Second
Expand All @@ -113,7 +141,6 @@ func durationParse(bits []string, opts Opts) (dur time.Duration) {
return time.Duration(v) * time.Hour * 24 * 365 // XXX:
default:
return durationParse(bits[:1], opts)

}
}
return
Expand Down
26 changes: 26 additions & 0 deletions duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dateparse

import (
"testing"
)

func TestCheckWordNumber(t *testing.T) {
for _, tt := range []struct {
input string
output float64
}{
{
"пол",
0.5,
},
{
"четверть",
0.25,
},
} {
result := checkWordNumber(tt.input)
if result != tt.output {
t.Errorf("%v != %v", result, tt.output)
}
}
}

0 comments on commit 21c5d41

Please sign in to comment.