From a14d526dcde987c64a38ef7b8894d22690f16f9b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 12 Sep 2015 21:50:15 -0700 Subject: [PATCH] Humanize time.duration properly --- share-main.go | 69 +++++++++++++++++++-------------------------------- share.go | 4 +-- time.go | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 time.go diff --git a/share-main.go b/share-main.go index 9fef6c9b..c9c3c365 100644 --- a/share-main.go +++ b/share-main.go @@ -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 diff --git a/share.go b/share.go index a314b874..6a777cd6 100644 --- a/share.go +++ b/share.go @@ -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 diff --git a/time.go b/time.go new file mode 100644 index 00000000..2923de6f --- /dev/null +++ b/time.go @@ -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)) +}