Humanize time.duration properly

This commit is contained in:
Harshavardhana
2015-09-12 21:51:29 -07:00
parent c60c48ad73
commit a14d526dcd
3 changed files with 74 additions and 46 deletions
+25 -44
View File
@@ -64,43 +64,21 @@ EXAMPLES:
`,
}
// shareMessage implements extended version of time.Duration, .Days() for convenience
type shareMessage struct {
// ShareMessage is container for share command on success and failure messages
type ShareMessage struct {
Expiry time.Duration
URL string
}
func (s shareMessage) Days() float64 {
return s.Expiry.Hours() / 24
}
func (s shareMessage) Seconds() float64 {
return s.Expiry.Seconds()
}
func (s shareMessage) Hours() float64 {
return s.Expiry.Hours()
}
func (s shareMessage) GetDuration() time.Duration {
return s.Expiry
}
func (s shareMessage) String() string {
func (s ShareMessage) String() string {
if !globalJSONFlag {
durationString := func() string {
if s.Expiry.Hours() > 24 {
return fmt.Sprintf("%dd", int64(s.Days()))
}
return s.Expiry.String()
}
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n URL: %s", durationString(), s.URL))
return console.Colorize("Share", fmt.Sprintf("Expiry: %s\n URL: %s", humanizeDuration(s.Expiry), s.URL))
}
shareMessageBytes, err := json.Marshal(struct {
Expires time.Duration `json:"expireSeconds"`
Expiry time.Duration `json:"expireSeconds"`
SharedURL string `json:"sharedURL"`
}{
Expires: time.Duration(s.Seconds()),
Expiry: time.Duration(s.Expiry.Seconds()),
SharedURL: s.URL,
})
fatalIf(probe.NewError(err), "Failed to marshal into JSON.")
@@ -174,7 +152,7 @@ func mainShare(ctx *cli.Context) {
targetURL := getAliasURL(url, config.Aliases)
// if recursive strip off the "..."
err := doShareURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), shareMessage{Expiry: expires})
err := doShareURL(stripRecursiveURL(targetURL), isURLRecursive(targetURL), expires)
fatalIf(err.Trace(targetURL), "Unable to generate URL for sharing.")
}
@@ -185,26 +163,26 @@ func doShareList() *probe.Error {
return err.Trace()
}
for url, data := range sURLs.URLs {
if time.Since(data.Date) > data.Message.GetDuration() {
if time.Since(data.Date) > data.Message.Expiry {
delete(sURLs.URLs, url)
continue
}
expiresIn := data.Message.GetDuration() - time.Since(data.Date)
expiry := data.Message.Expiry - time.Since(data.Date)
if !globalJSONFlag {
msg := console.Colorize("Share", "Shared URL: ")
msg := console.Colorize("Share", "Name: ")
msg += console.Colorize("URL", url+"\n")
msg += console.Colorize("Share", "Expires-In: ")
msg += console.Colorize("Expires", expiresIn)
msg += console.Colorize("Share", "Expiry: ")
msg += console.Colorize("Expires", humanizeDuration(expiry))
msg += "\n"
console.Println(msg)
continue
}
shareListBytes, err := json.Marshal(struct {
ExpiresIn time.Duration `json:"expiresIn"`
URL string `json:"url"`
Expiry time.Duration `json:"expiry"`
URL string `json:"url"`
}{
ExpiresIn: time.Duration(expiresIn.Seconds()),
URL: url,
Expiry: time.Duration(expiry.Seconds()),
URL: url,
})
if err != nil {
return probe.NewError(err)
@@ -218,7 +196,7 @@ func doShareList() *probe.Error {
}
// doShareURL share files from target
func doShareURL(targetURL string, recursive bool, expires shareMessage) *probe.Error {
func doShareURL(targetURL string, recursive bool, expires time.Duration) *probe.Error {
shareDate := time.Now().UTC()
sURLs, err := loadSharedURLsV1()
if err != nil {
@@ -246,19 +224,22 @@ func doShareURL(targetURL string, recursive bool, expires shareMessage) *probe.E
return err.Trace()
}
var sharedURL string
sharedURL, err = newClnt.Share(expires.GetDuration())
sharedURL, err = newClnt.Share(expires)
if err != nil {
return err.Trace()
}
expires.URL = sharedURL
shareMessage := ShareMessage{
Expiry: expires,
URL: sharedURL,
}
sURLs.URLs[newClnt.URL().String()] = struct {
Date time.Time
Message shareMessage
Message ShareMessage
}{
Date: shareDate,
Message: expires,
Message: shareMessage,
}
console.Println(expires)
console.Println(shareMessage)
}
saveSharedURLsV1(sURLs)
return nil
+2 -2
View File
@@ -29,7 +29,7 @@ type sharedURLs struct {
Version string
URLs map[string]struct {
Date time.Time
Message shareMessage
Message ShareMessage
}
}
@@ -38,7 +38,7 @@ func newSharedURLs() *sharedURLs {
Version: "1.0.0",
URLs: make(map[string]struct {
Date time.Time
Message shareMessage
Message ShareMessage
}),
}
return s
+47
View File
@@ -0,0 +1,47 @@
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"math"
"time"
)
// humanizeDuration humanizes time.Duration output to a meaningful value,
// golang's default ``time.Duration`` output is badly formatted and unreadable.
func humanizeDuration(duration time.Duration) string {
if duration.Seconds() < 60.0 {
return fmt.Sprintf("%d seconds", int64(duration.Seconds()))
}
if duration.Minutes() < 60.0 {
remainingSeconds := math.Mod(duration.Seconds(), 60)
return fmt.Sprintf("%d minutes %d seconds", int64(duration.Minutes()), int64(remainingSeconds))
}
if duration.Hours() < 24.0 {
remainingMinutes := math.Mod(duration.Minutes(), 60)
remainingSeconds := math.Mod(duration.Seconds(), 60)
return fmt.Sprintf("%d hours %d minutes %d seconds",
int64(duration.Hours()), int64(remainingMinutes), int64(remainingSeconds))
}
remainingHours := math.Mod(duration.Hours(), 24)
remainingMinutes := math.Mod(duration.Minutes(), 60)
remainingSeconds := math.Mod(duration.Seconds(), 60)
return fmt.Sprintf("%d days %d hours %d minutes %d seconds",
int64(duration.Hours()/24), int64(remainingHours),
int64(remainingMinutes), int64(remainingSeconds))
}