Skip to content

Commit

Permalink
Make durations more human friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed May 31, 2015
1 parent fef6a6b commit 975c93d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
26 changes: 26 additions & 0 deletions LICENSE.txt
Expand Up @@ -11,3 +11,29 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


timefuncs license
----------------------------------------------------------------------
Copyright (c) 2013, Sevki Hasirci
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

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

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 HOLDER 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.
4 changes: 2 additions & 2 deletions cmd/ledger/print.go
Expand Up @@ -31,8 +31,8 @@ func PrintStats(generalLedger []*ledger.Transaction) {

days := math.Floor(endDate.Sub(startDate).Hours() / 24)

fmt.Printf("%-25s : %s to %s (%s)\n", "Transactions span", startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), endDate.Sub(startDate))
fmt.Printf("%-25s : %s\n", "Since last post", time.Since(endDate))
fmt.Printf("%-25s : %s to %s (%s)\n", "Transactions span", startDate.Format("2006-01-02"), endDate.Format("2006-01-02"), DurationInWords(endDate.Sub(startDate)))
fmt.Printf("%-25s : %s\n", "Since last post", DurationInWords(time.Since(endDate)))
fmt.Printf("%-25s : %d, (%.1f per day)\n", "Transactions", len(generalLedger), float64(len(generalLedger))/days)
fmt.Printf("%-25s : %d\n", "Payees", len(payees))
fmt.Printf("%-25s : %d\n", "Referenced Accounts", len(accounts))
Expand Down
61 changes: 61 additions & 0 deletions cmd/ledger/timefuncs.go
@@ -0,0 +1,61 @@
// Copyright 2013 Sevki Hasirci . All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Original location: "github.com/sevki/fathertime"

package main

import (
"fmt"
"time"
)

const lssthnd = "less than %d %s"
const lssthns = "less than a %s"
const aboutnd = "about %d %s"
const day time.Duration = 86400000000000
const month time.Duration = 2628000000001209
const year time.Duration = 31535999999964780

/*
DurationInWords returns duration in words.
*/
func DurationInWords(d time.Duration) string {

if d >= time.Second && d <= (time.Second*4) {
return fmt.Sprintf(lssthnd, 5, "seconds")
} else if d >= (time.Second*5) && d < (time.Second*10) {
return fmt.Sprintf(lssthnd, 10, "seconds")
} else if d >= (time.Second*10) && d < (time.Second*20) {
return fmt.Sprintf(lssthnd, 20, "seconds")
} else if d >= (time.Second*20) && d < (time.Second*40) {
return "half a minute"
} else if d >= (time.Second*40) && d < (time.Second*60) {
return fmt.Sprintf(lssthns, "minute")
} else if d >= (time.Second*60) && d < time.Minute+(time.Second*30) {
return "1 minute"
} else if d >= time.Minute+(time.Second*30) && d < (time.Minute*44)+(time.Second*30) {
return fmt.Sprintf("%d minutes", (d / time.Minute))
} else if d >= (time.Minute*44)+(time.Second*30) && d < (time.Minute*89)+(time.Second*30) {
return fmt.Sprintf(aboutnd, d/time.Hour, "hour")
} else if d >= (time.Minute*89)+(time.Second*30) && d < (time.Hour*29)+(time.Minute*59)+(time.Second*30) {
return fmt.Sprintf(aboutnd, (d / time.Hour), "hours")
} else if d >= (time.Hour*23)+(time.Minute*59)+(time.Second*30) && d < (time.Hour*41)+(time.Minute*59)+(time.Second*30) {
return "1 day"
} else if d >= (time.Hour*41)+(time.Minute*59)+(time.Second*30) && d < (day*29)+(time.Hour*23)+(time.Minute*59)+(time.Second*30) {
return fmt.Sprintf("%d days", d/(time.Hour*24))
} else if d >= (day*29)+(time.Hour*23)+(time.Minute*59)+(time.Second*30) && d < (day*59)+(time.Hour*23)+(time.Minute*59)+(time.Second*30) {
return fmt.Sprintf(aboutnd, 1, "month")
} else if d >= (day*59)+(time.Hour*23)+(time.Minute*59)+(time.Second*30) && d < (year) {
return fmt.Sprintf(aboutnd, d/month+1, "months")
} else if d >= year && d < year+(3*month) {
return fmt.Sprintf(aboutnd, 1, "year")
} else if d >= year+(3*month) && d < year+(9*month) {
return "over 1 year"
} else if d >= year+(9*month) && d < (year*2) {
return "almost 2 years"
} else {
return fmt.Sprintf(aboutnd, d/year, "years")
}
}

0 comments on commit 975c93d

Please sign in to comment.