-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_item.go
More file actions
68 lines (53 loc) · 1.67 KB
/
Copy pathtime_item.go
File metadata and controls
68 lines (53 loc) · 1.67 KB
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
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[time_item.go]
// (c) balarabe@protonmail.com License: GPLv3
// -----------------------------------------------------------------------------
package main
import (
"fmt"
"time"
)
// TimeItem _ _
type TimeItem struct {
Time time.Time
Text string
Count int
Spent time.Duration
}
// String _ _
func (ob *TimeItem) String() string {
date := ob.Time.Format("2006-01-02")
hours := ob.Spent.Hours()
return fmt.Sprintf("%s %7.2f %11s: %s", date, hours, ob.Spent, ob.Text)
}
// -----------------------------------------------------------------------------
// TimeItemsByTime _ _
// use sort.Sort(TimeItemsByTime(ar)) to sort
type TimeItemsByTime []TimeItem
// Len _ _
func (a TimeItemsByTime) Len() int { return len(a) }
// Swap _ _
func (a TimeItemsByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Less _ _
func (a TimeItemsByTime) Less(i, j int) bool {
return a[i].Time.Sub(a[j].Time) < 0
}
// -----------------------------------------------------------------------------
// TimeItemsByDateAndDescSpent _ _
// use sort.Sort(TimeItemsByDateAndDescSpent(ar)) to sort
type TimeItemsByDateAndDescSpent []TimeItem
// Len _ _
func (a TimeItemsByDateAndDescSpent) Len() int { return len(a) }
// Swap _ _
func (a TimeItemsByDateAndDescSpent) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Less _ _
func (a TimeItemsByDateAndDescSpent) Less(i, j int) bool {
if a[i].Time.Sub(a[j].Time) < 0 {
return true
}
if a[i].Time == a[j].Time {
return a[i].Spent > a[j].Spent // descending
}
return false
}
// end