-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from unistack-org/timeDuration
config/default: handle time.Duration
- Loading branch information
Showing
4 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package time | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type Duration int64 | ||
|
||
func ParseDuration(s string) (time.Duration, error) { | ||
if s == "" { | ||
return 0, fmt.Errorf(`time: invalid duration "` + s + `"`) | ||
} | ||
|
||
//var sb strings.Builder | ||
/* | ||
for i, r := range s { | ||
switch r { | ||
case 'd': | ||
n, err := strconv.Atoi(s[idx:i]) | ||
if err != nil { | ||
return 0, errors.New("time: invalid duration " + s) | ||
} | ||
s[idx:i] = fmt.Sprintf("%d", n*24) | ||
default: | ||
sb.WriteRune(r) | ||
} | ||
} | ||
*/ | ||
var td time.Duration | ||
var err error | ||
switch s[len(s)-1] { | ||
case 's', 'm', 'h': | ||
td, err = time.ParseDuration(s) | ||
case 'd': | ||
if td, err = time.ParseDuration(s[:len(s)-1] + "h"); err == nil { | ||
td *= 24 | ||
} | ||
case 'y': | ||
if td, err = time.ParseDuration(s[:len(s)-1] + "h"); err == nil { | ||
year := time.Date(time.Now().Year(), time.December, 31, 0, 0, 0, 0, time.Local) | ||
days := year.YearDay() | ||
td *= 24 * time.Duration(days) | ||
} | ||
} | ||
|
||
return td, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package time | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseDuration(t *testing.T) { | ||
var td time.Duration | ||
var err error | ||
t.Skip() | ||
td, err = ParseDuration("14d4h") | ||
if err != nil { | ||
t.Fatalf("ParseDuration error: %v", err) | ||
} | ||
if td.String() != "336h0m0s" { | ||
t.Fatalf("ParseDuration 14d != 336h0m0s : %s", td.String()) | ||
} | ||
|
||
td, err = ParseDuration("1y") | ||
if err != nil { | ||
t.Fatalf("ParseDuration error: %v", err) | ||
} | ||
if td.String() != "8760h0m0s" { | ||
t.Fatalf("ParseDuration 1y != 8760h0m0s : %s", td.String()) | ||
} | ||
} |