-
Notifications
You must be signed in to change notification settings - Fork 939
/
currenttime.go
52 lines (45 loc) · 1.46 KB
/
currenttime.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
package currenttime
import (
"strings"
"time"
"github.com/jonas747/dcmd"
"github.com/jonas747/yagpdb/commands"
"github.com/tkuchiki/go-timezone"
)
var Command = &commands.YAGCommand{
CmdCategory: commands.CategoryTool,
Name: "CurrentTime",
Aliases: []string{"ctime", "gettime"},
Description: "Shows current time in different timezones. [Available timezones](https://pastebin.com/ZqSPUhc7)",
ArgumentCombos: [][]int{[]int{1}, []int{0}, []int{}},
Arguments: []*dcmd.ArgDef{
{Name: "Zone", Type: dcmd.String},
{Name: "Offset", Type: dcmd.Int},
},
RunFunc: cmdFuncCurrentTime,
}
func cmdFuncCurrentTime(data *dcmd.Data) (interface{}, error) {
const format = "Mon Jan 02 15:04:05 (UTC -07:00)"
now := time.Now()
if data.Args[0].Value != nil {
tzName := data.Args[0].Str()
names, err := timezone.GetTimezones(strings.ToUpper(data.Args[0].Str()))
if err == nil && len(names) > 0 {
tzName = names[0]
}
location, err := time.LoadLocation(tzName)
if err != nil {
if offset, ok := customTZOffsets[strings.ToUpper(tzName)]; ok {
location = time.FixedZone(tzName, int(offset*60*60))
} else {
return "Unknown timezone :(", err
}
}
return now.In(location).Format(format), nil
} else if data.Args[1].Value != nil {
location := time.FixedZone("", data.Args[1].Int()*60*60)
return now.In(location).Format(format), nil
}
// No offset of zone specified, just return the bots location
return now.Format(format), nil
}