-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathguild_news_item.go
57 lines (49 loc) · 1.12 KB
/
guild_news_item.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
package wow
import (
"errors"
"time"
"fmt"
)
type GuildNewsItem struct {
Type string
Character string
Timestamp uint64
ItemId int
item *Item
Achievement *Achievement
}
func (g *GuildNewsItem) Time() time.Time{
return time.Unix(int64(g.Timestamp)/1000, int64(g.Timestamp)%1000)
}
func (g *GuildNewsItem) Ago() time.Duration {
return time.Now().Sub(g.Time())
}
func (g *GuildNewsItem) HowLongAgo() string {
d := g.Ago()
if d.Hours() > 2 {
return fmt.Sprintf("%d hours ago", int(d.Hours()))
}
if d.Minutes() > 2 {
return fmt.Sprintf("%d minutes ago", int(d.Minutes()))
}
if d.Seconds() > 1 {
return fmt.Sprintf("%d seconds ago", int(d.Seconds()))
}
return fmt.Sprintf("%d nanoseconds ago", int(d.Nanoseconds()))
}
func (g *GuildNewsItem) Item() (*Item, error) {
var err error
if g.item != nil {
return g.item, nil
}
if g.ItemId != 0 {
client := CurrentApiClient()
if client != nil {
g.item, err = client.GetItem(g.ItemId)
return g.item, err
} else {
return nil, errors.New("No API client created. Create one via NewApiClient")
}
}
return nil, errors.New("No ItemId set")
}