blob: dc4ce2d7e50656f88eaba6119f5861b1b0e40bde (
plain)
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
  | 
package cobol
import (
	"fmt"
	"time"
)
// this math isn't perfect but I don't care right now
// use time.
func FormatDuration(d time.Duration) string {
	result := ""
	// check if it's more than a year
	years := int(d.Hours()) / (24 * 365)
	if years > 0 {
		result += fmt.Sprintf("%3d Y", years)
		return result
	}
	/* was terrible
	// check if it's more than a day
	if days > 45 {
		months := int(d.Hours()) % 12
		result += fmt.Sprintf("%2d M", months)
		return result
	}
	*/
	days := int(d.Hours()) / 24
	if days >= 2 {
		result += fmt.Sprintf("%3d d", days)
		return result
	}
	// check if it's more than an hour
	hours := int(d.Hours()) % 24
	if hours >= 2 {
		result += fmt.Sprintf("%3d h", hours)
		return result
	}
	// check if it's more than a minute
	minutes := int(d.Minutes()) % 60
	if minutes >= 2 {
		result += fmt.Sprintf("%3d m", minutes)
		return result
	}
	// check if it's more than a second
	seconds := int(d.Seconds()) % 60
	if seconds > 0 {
		result += fmt.Sprintf("%3d s", seconds)
		return result
	}
	// report in milliseconds
	ms := int(d.Milliseconds())
	if ms >= 100 {
		// todo: print .3s, etc ?
	}
	if ms > 0 {
		/*
			if ms > 99 {
				result += fmt.Sprintf("%-3dm", ms)
				return result
			}
		*/
		result += fmt.Sprintf("%3dms", ms)
		return result
	}
	// report in milliseconds
	mc := int(d.Microseconds())
	if mc > 0 {
		result += fmt.Sprintf("%3dmc", mc)
		return result
	}
	ns := int(d.Nanoseconds())
	result += fmt.Sprintf("%3dns", ns)
	return result
}
  |