Skip to content

Commit 741dffc

Browse files
committed
V2 release
This version use the same time.ParseDuration from standard Go, but support for days and weeks where a day is 24 hour.
1 parent 0bc4c5a commit 741dffc

File tree

5 files changed

+41
-52
lines changed

5 files changed

+41
-52
lines changed

LICENSE

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
The MIT License (MIT)
1+
Copyright (c) 2009 The Go Authors. All rights reserved.
22

3-
Copyright (c) 2020 Santiago De la Cruz
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
46

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google Inc. nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
1116

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+7-21
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
This package allows to get a time.Duration from a string. The string can be a string retorned for time.Duration or a similar string with weeks or days too!.
44

5-
<a href="https://goreportcard.com/report/github.com/xhit/go-str2duration"><img src="https://goreportcard.com/badge/github.com/xhit/go-str2duration" alt="Go Report Card"></a>
6-
<a href="https://pkg.go.dev/github.com/xhit/go-str2duration?tab=doc"><img src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white" alt="go.dev"></a>
5+
<a href="https://goreportcard.com/report/github.com/xhit/go-str2duration/v2"><img src="https://goreportcard.com/badge/github.com/xhit/go-str2duration" alt="Go Report Card"></a>
6+
<a href="https://pkg.go.dev/github.com/xhit/go-str2duration/v2?tab=doc"><img src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white" alt="go.dev"></a>
77

88
## Download
99

1010
```bash
11-
go get github.com/xhit/go-str2duration
11+
go get github.com/xhit/go-str2duration/v2
1212
```
1313

1414
## Features
@@ -18,36 +18,25 @@ Go String To Duration supports this strings conversions to duration:
1818
- A string more readable like 1w2d6h3ns (1 week 2 days 6 hours and 3 nanoseconds).
1919
- `µs` and `us` are microsecond.
2020

21+
It's the same `time.ParseDuration` standard function in Go, but with days and week support.
22+
2123
**Note**: a day is 24 hour.
2224

2325
If you don't need days and weeks, use [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration).
2426

25-
## Use cases
26-
27-
- Imagine you save the output of time.Duration strings in a database, file, etc... and you need to convert again to time.Duration. Now you can!
28-
- Set a more precise duration in a configuration file for wait, timeouts, measure, etc...
29-
3027
## Usage
3128

3229
```go
3330
package main
3431

3532
import (
3633
"fmt"
37-
str2duration "github.com/xhit/go-str2duration"
34+
str2duration "github.com/xhit/go-str2duration/v2"
3835
"os"
3936
"time"
4037
)
4138

4239
func main() {
43-
44-
/*
45-
If DisableCheck is true then when input string is
46-
is invalid the time.Duration returned is always 0s and err is always nil.
47-
By default DisableCheck is false.
48-
*/
49-
50-
//str2duration.DisableCheck = true
5140

5241
for i, tt := range []struct {
5342
dur string
@@ -79,13 +68,10 @@ func main() {
7968
{"2d3s96ns", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
8069
{"1w2d3s96ns", time.Duration(168*time.Hour + 48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
8170

82-
//And can be case insensitive
83-
{"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
84-
8571
{"10s1us693ns", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)},
8672

8773
} {
88-
durationFromString, err := str2duration.Str2Duration(tt.dur)
74+
durationFromString, err := str2duration.ParseDuration(tt.dur)
8975
if err != nil {
9076
panic(err)
9177

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/xhit/go-str2duration
1+
module github.com/xhit/go-str2duration/v2
22

3-
go 1.14
3+
go 1.13

str2duration.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright 2010 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in https://raw.githubusercontent.com/golang/go/master/LICENSE
4+
15
package str2duration
26

37
import (
@@ -18,12 +22,12 @@ var unitMap = map[string]int64{
1822
"w": int64(time.Hour) * 168,
1923
}
2024

21-
// Str2Duration parses a duration string.
25+
// ParseDuration parses a duration string.
2226
// A duration string is a possibly signed sequence of
2327
// decimal numbers, each with optional fraction and a unit suffix,
2428
// such as "300ms", "-1.5h" or "2h45m".
2529
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d", "w".
26-
func Str2Duration(s string) (time.Duration, error) {
30+
func ParseDuration(s string) (time.Duration, error) {
2731
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
2832
orig := s
2933
var d int64

str2duration_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,8 @@ func TestParseString(t *testing.T) {
3838
{"2d3s96ns", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
3939
{"1w2d3s96ns", time.Duration(168*time.Hour + 48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
4040
{"1w2d3s3µs96ns", time.Duration(168*time.Hour + 48*time.Hour + 3*time.Second + 3*time.Microsecond + 96*time.Nanosecond)},
41-
42-
//And can be case insensitive
43-
{"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
44-
45-
//This cases are invalid
46-
{"2.3D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
47-
{"2D3S3.66.SMS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
4841
} {
49-
durationFromString, err := Str2Duration(tt.dur)
42+
durationFromString, err := ParseDuration(tt.dur)
5043
if err != nil {
5144
t.Logf("index %d -> in: %s returned: %s\tnot equal to %s", i, tt.dur, err.Error(), tt.expected.String())
5245

@@ -90,7 +83,7 @@ func TestParseDuration(t *testing.T) {
9083
time.Duration(61 * time.Second),
9184
time.Duration(time.Microsecond + 16*time.Nanosecond),
9285
} {
93-
durationFromString, _ := Str2Duration(duration.String())
86+
durationFromString, _ := ParseDuration(duration.String())
9487
if duration.String() != durationFromString.String() {
9588
t.Errorf("index %d -> %s not equal to %s", i, duration.String(), durationFromString.String())
9689
}

0 commit comments

Comments
 (0)