mirror of
https://github.com/MontFerret/ferret.git
synced 2025-03-05 15:16:07 +02:00
fixed a bug when adding/subtrating did not take an amount of units (#181)
This commit is contained in:
parent
f748c308cc
commit
1866cb1e31
@ -109,13 +109,13 @@ func addUnit(dt values.DateTime, amount int, unit string) (values.DateTime, erro
|
||||
case "d", "day", "days":
|
||||
return values.NewDateTime(dt.AddDate(0, 0, amount*1)), nil
|
||||
case "h", "hour", "hours":
|
||||
return values.NewDateTime(dt.Add(time.Hour)), nil
|
||||
return values.NewDateTime(dt.Add(time.Duration(amount) * time.Hour)), nil
|
||||
case "i", "minute", "minutes":
|
||||
return values.NewDateTime(dt.Add(time.Minute)), nil
|
||||
return values.NewDateTime(dt.Add(time.Duration(amount) * time.Minute)), nil
|
||||
case "s", "second", "seconds":
|
||||
return values.NewDateTime(dt.Add(time.Second)), nil
|
||||
return values.NewDateTime(dt.Add(time.Duration(amount) * time.Second)), nil
|
||||
case "f", "millisecond", "milliseconds":
|
||||
return values.NewDateTime(dt.Add(time.Millisecond)), nil
|
||||
return values.NewDateTime(dt.Add(time.Duration(amount) * time.Millisecond)), nil
|
||||
}
|
||||
return values.DateTime{}, errors.Errorf("no such unit '%s'", unit)
|
||||
}
|
||||
|
@ -111,6 +111,42 @@ func TestDateAdd(t *testing.T) {
|
||||
values.NewString("year"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "+2 hours",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T17:04:05Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(2),
|
||||
values.NewString("h"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "+20 minutes",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T15:24:05Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(20),
|
||||
values.NewString("i"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "+30 seconds",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T15:04:35Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(30),
|
||||
values.NewString("s"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "+1000 milliseconds",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T15:04:06Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(1000),
|
||||
values.NewString("f"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
@ -214,6 +250,42 @@ func TestDateSubtract(t *testing.T) {
|
||||
values.NewString("year"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "-2 hours",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T13:04:05Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(2),
|
||||
values.NewString("h"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "-20 minutes",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T14:44:05Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(20),
|
||||
values.NewString("i"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "-30 seconds",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T15:03:35Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(30),
|
||||
values.NewString("s"),
|
||||
},
|
||||
},
|
||||
&testCase{
|
||||
Name: "-1000 milliseconds",
|
||||
Expected: mustDefaultLayoutDt("1999-02-07T15:04:04Z"),
|
||||
Args: []core.Value{
|
||||
mustDefaultLayoutDt("1999-02-07T15:04:05Z"),
|
||||
values.NewInt(1000),
|
||||
values.NewString("f"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
|
Loading…
x
Reference in New Issue
Block a user