~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/dustin/go-humanize/times.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package humanize
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "math"
 
6
        "sort"
 
7
        "time"
 
8
)
 
9
 
 
10
// Seconds-based time units
 
11
const (
 
12
        Minute   = 60
 
13
        Hour     = 60 * Minute
 
14
        Day      = 24 * Hour
 
15
        Week     = 7 * Day
 
16
        Month    = 30 * Day
 
17
        Year     = 12 * Month
 
18
        LongTime = 37 * Year
 
19
)
 
20
 
 
21
// Time formats a time into a relative string.
 
22
//
 
23
// Time(someT) -> "3 weeks ago"
 
24
func Time(then time.Time) string {
 
25
        return RelTime(then, time.Now(), "ago", "from now")
 
26
}
 
27
 
 
28
var magnitudes = []struct {
 
29
        d      int64
 
30
        format string
 
31
        divby  int64
 
32
}{
 
33
        {1, "now", 1},
 
34
        {2, "1 second %s", 1},
 
35
        {Minute, "%d seconds %s", 1},
 
36
        {2 * Minute, "1 minute %s", 1},
 
37
        {Hour, "%d minutes %s", Minute},
 
38
        {2 * Hour, "1 hour %s", 1},
 
39
        {Day, "%d hours %s", Hour},
 
40
        {2 * Day, "1 day %s", 1},
 
41
        {Week, "%d days %s", Day},
 
42
        {2 * Week, "1 week %s", 1},
 
43
        {Month, "%d weeks %s", Week},
 
44
        {2 * Month, "1 month %s", 1},
 
45
        {Year, "%d months %s", Month},
 
46
        {18 * Month, "1 year %s", 1},
 
47
        {2 * Year, "2 years %s", 1},
 
48
        {LongTime, "%d years %s", Year},
 
49
        {math.MaxInt64, "a long while %s", 1},
 
50
}
 
51
 
 
52
// RelTime formats a time into a relative string.
 
53
//
 
54
// It takes two times and two labels.  In addition to the generic time
 
55
// delta string (e.g. 5 minutes), the labels are used applied so that
 
56
// the label corresponding to the smaller time is applied.
 
57
//
 
58
// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"
 
59
func RelTime(a, b time.Time, albl, blbl string) string {
 
60
        lbl := albl
 
61
        diff := b.Unix() - a.Unix()
 
62
 
 
63
        after := a.After(b)
 
64
        if after {
 
65
                lbl = blbl
 
66
                diff = a.Unix() - b.Unix()
 
67
        }
 
68
 
 
69
        n := sort.Search(len(magnitudes), func(i int) bool {
 
70
                return magnitudes[i].d > diff
 
71
        })
 
72
 
 
73
        mag := magnitudes[n]
 
74
        args := []interface{}{}
 
75
        escaped := false
 
76
        for _, ch := range mag.format {
 
77
                if escaped {
 
78
                        switch ch {
 
79
                        case '%':
 
80
                        case 's':
 
81
                                args = append(args, lbl)
 
82
                        case 'd':
 
83
                                args = append(args, diff/mag.divby)
 
84
                        }
 
85
                        escaped = false
 
86
                } else {
 
87
                        escaped = ch == '%'
 
88
                }
 
89
        }
 
90
        return fmt.Sprintf(mag.format, args...)
 
91
}