Skip to content

Commit dd4cc4a

Browse files
committed
Add milli seconds as available unit
1 parent c9baa00 commit dd4cc4a

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ nowAgain, err := parser.Since(inTenMinutes, "-10 minutes")
2929

3030
## Available units
3131
The available time units are
32+
* `ms`, `millisecond` or `milliseconds`
3233
* `s`, `second` or `seconds`
3334
* `m`, `minute` or `minutes`
3435
* `h`, `hour` or `hours`
@@ -44,7 +45,7 @@ It's the number that will multiply the time unit:
4445
### Validation
4546
You can use this regular expression to validate your inputs:
4647
```
47-
^((\\+|\\-))?([1-9][0-9]*)\\s?(s|seconds?|m|minutes?|h|hours?|d|days?|w|weeks?)$
48+
^((\+|\-))?([1-9][0-9]*)\s?(ms|milliseconds?|s|seconds?|m|minutes?|h|hours?|d|days?|w|weeks?)$
4849
```
4950

5051
## Scope

parser.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@ import (
88
)
99

1010
const (
11-
Regex = "^((\\+|\\-))?([1-9][0-9]*)\\s?(s|seconds?|m|minutes?|h|hours?|d|days?|w|weeks?)$"
11+
Regex = "^((\\+|\\-))?([1-9][0-9]*)\\s?(ms|milliseconds?|s|seconds?|m|minutes?|h|hours?|d|days?|w|weeks?)$"
1212
Day = time.Hour * 24
1313
Week = Day * 7
1414
)
1515

16-
var compiledRegex *regexp.Regexp
16+
var compiledRegex *regexp.Regexp
17+
18+
var unitMap = map[string]time.Duration{
19+
"ms": time.Millisecond,
20+
"millisecond": time.Millisecond,
21+
"milliseconds": time.Millisecond,
22+
"s": time.Second,
23+
"second": time.Second,
24+
"seconds": time.Second,
25+
"m": time.Minute,
26+
"minute": time.Minute,
27+
"minutes": time.Minute,
28+
"h": time.Hour,
29+
"hour": time.Hour,
30+
"hours": time.Hour,
31+
"d": Day,
32+
"day": Day,
33+
"days": Day,
34+
"w": Week,
35+
"week": Week,
36+
"weeks": Week,
37+
}
1738

1839
// Parser is the service that will provide the package functionality
1940
type Parser struct {}
@@ -28,6 +49,7 @@ func New() *Parser {
2849
// The input string must contain a time unit and a signed multiplier ('+' sign by default if omitted)
2950
//
3051
// The accepted time unit are:
52+
// - `ms`, `millisecond` or `milliseconds`
3153
// - `s`, `second` or `seconds`
3254
// - `m`, `minute` or `minutes`
3355
// - `h`, `hour` or `hours`
@@ -94,20 +116,12 @@ func (p *Parser) getMultiplier(sign string, multiplier string) (time.Duration, e
94116
}
95117

96118
func (p *Parser) getDuration(unit string, multiplier time.Duration) (time.Duration, error) {
97-
switch unit[:1] {
98-
case "s":
99-
return time.Second * multiplier, nil
100-
case "m":
101-
return time.Minute * multiplier, nil
102-
case "h":
103-
return time.Hour * multiplier, nil
104-
case "d":
105-
return Day * multiplier, nil
106-
case "w":
107-
return Week * multiplier, nil
119+
duration, found := unitMap[unit]
120+
if !found {
121+
return 0, fmt.Errorf("The duration unit '%s' is not supported", unit)
108122
}
109123

110-
return 0, fmt.Errorf("The duration unit '%s' is not supported", unit)
124+
return duration * multiplier, nil
111125
}
112126

113127
func (p *Parser) getRegex() (*regexp.Regexp, error) {

parser_test.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ func TestItParsePositiveDurationsCorrectly(t *testing.T) {
1010
parser := after.New()
1111

1212
valid := map[string]time.Duration{
13-
"1s": time.Second,
14-
"+1 second": time.Second,
15-
"20seconds": time.Second * 20,
16-
"1m": time.Minute,
17-
"+1 minute": time.Minute,
18-
"20 minutes": time.Minute * 20,
19-
"1h": time.Hour,
20-
"+1 hour": time.Hour,
21-
"20hours": time.Hour * 20,
22-
"1d": time.Hour * 24,
23-
"+1 day": time.Hour * 24,
24-
"20 days": time.Hour * 24 * 20,
25-
"1w": time.Hour * 24 * 7,
26-
"+1 week": time.Hour * 24 * 7,
27-
"20 weeks": time.Hour * 24 * 7 * 20,
13+
"1 millisecond": time.Millisecond,
14+
"+1ms": time.Millisecond,
15+
"20 milliseconds": time.Millisecond * 20,
16+
"1s": time.Second,
17+
"+1 second": time.Second,
18+
"20seconds": time.Second * 20,
19+
"1m": time.Minute,
20+
"+1 minute": time.Minute,
21+
"20 minutes": time.Minute * 20,
22+
"1h": time.Hour,
23+
"+1 hour": time.Hour,
24+
"20hours": time.Hour * 20,
25+
"1d": time.Hour * 24,
26+
"+1 day": time.Hour * 24,
27+
"20 days": time.Hour * 24 * 20,
28+
"1w": time.Hour * 24 * 7,
29+
"+1 week": time.Hour * 24 * 7,
30+
"20 weeks": time.Hour * 24 * 7 * 20,
2831
}
2932

3033
for input, expected := range valid {
@@ -43,21 +46,24 @@ func TestItParseNegativeDurationsCorrectly(t *testing.T) {
4346
parser := after.New()
4447

4548
valid := map[string]time.Duration{
46-
"-1s": -time.Second,
47-
"-1 second": -time.Second,
48-
"-20seconds": -time.Second * 20,
49-
"-1m": -time.Minute,
50-
"-1 minute": -time.Minute,
51-
"-20 minutes": -time.Minute * 20,
52-
"-1h": -time.Hour,
53-
"-1 hour": -time.Hour,
54-
"-20hours": -time.Hour * 20,
55-
"-1d": -time.Hour * 24,
56-
"-1 day": -time.Hour * 24,
57-
"-20 days": -time.Hour * 24 * 20,
58-
"-1w": -time.Hour * 24 * 7,
59-
"-1 week": -time.Hour * 24 * 7,
60-
"-20 weeks": -time.Hour * 24 * 7 * 20,
49+
"-1ms": -time.Millisecond,
50+
"-1 millisecond": -time.Millisecond,
51+
"-20millisecond": -time.Millisecond * 20,
52+
"-1s": -time.Second,
53+
"-1 second": -time.Second,
54+
"-20seconds": -time.Second * 20,
55+
"-1m": -time.Minute,
56+
"-1 minute": -time.Minute,
57+
"-20 minutes": -time.Minute * 20,
58+
"-1h": -time.Hour,
59+
"-1 hour": -time.Hour,
60+
"-20hours": -time.Hour * 20,
61+
"-1d": -time.Hour * 24,
62+
"-1 day": -time.Hour * 24,
63+
"-20 days": -time.Hour * 24 * 20,
64+
"-1w": -time.Hour * 24 * 7,
65+
"-1 week": -time.Hour * 24 * 7,
66+
"-20 weeks": -time.Hour * 24 * 7 * 20,
6167
}
6268

6369
for input, expected := range valid {

0 commit comments

Comments
 (0)