/
nanoseconds.go
123 lines (114 loc) · 5.21 KB
/
nanoseconds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2015 Sevki <s@sevki.org>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package time // import "sevki.org/x/time"
import (
"fmt"
"time"
)
// NsReadable takes in a nanosecond duration and converts it to it's least decimalled unit
// there is also a lot of rounding going on here
func NsReadable(ns int64) string {
t := float64(ns)
unit := "t"
switch {
case t > float64(time.Minute.Nanoseconds())/100:
unit = "m"
return fmt.Sprintf("%.2f%s", t/float64(time.Minute.Nanoseconds()), unit)
case t > float64(time.Second.Nanoseconds())/100:
unit = "s"
return fmt.Sprintf("%.2f%s", t/float64(time.Second.Nanoseconds()), unit)
case t > 10000:
unit = "ms"
return fmt.Sprintf("%.2f%s", t/10000, unit)
case t > 10:
unit = "μs"
return fmt.Sprintf("%.2f%s", t/10, unit)
default:
return fmt.Sprintf("%.2f%s", t, unit)
}
}
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
// InWords returns time in words after subtracting it from now.
func InWords(t time.Time) string {
now := time.Now()
d := now.Sub(t)
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")
}
}
// 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")
}
}