Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ customCommandPrefix:
命令1: "Q"
命令2: ""

timezone: Asia/Shanghai # 使用TZ database name自定义通知消息时间戳的时区,默认为部署服务器的本地时区

logLevel: info # 日志等级
```

Expand Down
16 changes: 12 additions & 4 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package utils

import (
"fmt"
"github.com/guonaihong/gout"
jsoniter "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
"io/fs"
"net/url"
"path/filepath"
Expand All @@ -14,6 +11,11 @@ import (
"strconv"
"strings"
"time"

"github.com/Sora233/MiraiGo-Template/config"
"github.com/guonaihong/gout"
jsoniter "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
Expand Down Expand Up @@ -162,7 +164,7 @@ func PrefixMatch(opts []string, prefix string) (string, bool) {
)
for _, opt := range opts {
if strings.HasPrefix(opt, prefix) {
if found == true {
if found {
return "", false
}
found = true
Expand All @@ -178,6 +180,12 @@ func UnquoteString(s string) (string, error) {

func TimestampFormat(ts int64) string {
t := time.Unix(ts, 0)
if tzStr := config.GlobalConfig.GetString("timezone"); tzStr != "" {
loc, _ := time.LoadLocation(tzStr)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有些服务器可能没装额外的tzinfo,在这里如果有报错希望能够有日志提醒

if loc != nil {
t = t.In(loc)
}
}
return t.Format("2006-01-02 15:04:05")
}

Expand Down
22 changes: 19 additions & 3 deletions utils/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package utils

import (
"github.com/Sora233/DDBOT/internal/test"
"github.com/guonaihong/gout"
"github.com/stretchr/testify/assert"
"html"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"time"

"github.com/Sora233/DDBOT/internal/test"
"github.com/Sora233/MiraiGo-Template/config"
"github.com/guonaihong/gout"
"github.com/stretchr/testify/assert"
)

func TestFilePathWalkDir(t *testing.T) {
Expand Down Expand Up @@ -241,3 +243,17 @@ func TestGroupLogFields(t *testing.T) {
func TestFriendLogFields(t *testing.T) {
assert.NotNil(t, FriendLogFields(1))
}

func TestTimestampFormat(t *testing.T) {
config.GlobalConfig.Viper.Set("timezone", "Asia/Shanghai")
assert.EqualValues(t, "1970-01-01 08:00:00", TimestampFormat(0))
config.GlobalConfig.Viper.Set("timezone", "America/New York")
assert.EqualValues(t, "1969-12-31 19:00:00", TimestampFormat(0))

localNow := time.Now()
expectedStr := localNow.Format("2006-01-02 15:04:05")
config.GlobalConfig.Viper.Set("timezone", "nowhere")
assert.EqualValues(t, expectedStr, TimestampFormat(localNow.Unix()))
config.GlobalConfig.Viper.Set("timezone", "")
assert.EqualValues(t, expectedStr, TimestampFormat(localNow.Unix()))
}