From c697f83e5f9c661194053c472ebbcc8bb15ef906 Mon Sep 17 00:00:00 2001 From: dchaofei Date: Wed, 9 Aug 2023 11:27:54 +0800 Subject: [PATCH] fix: msg date (#152) --- wechaty-puppet-service/helper.go | 21 +++++++++++++++++++++ wechaty-puppet-service/puppet_service.go | 6 ------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 wechaty-puppet-service/helper.go diff --git a/wechaty-puppet-service/helper.go b/wechaty-puppet-service/helper.go new file mode 100644 index 0000000..4c1ecbf --- /dev/null +++ b/wechaty-puppet-service/helper.go @@ -0,0 +1,21 @@ +package puppetservice + +import ( + "google.golang.org/protobuf/types/known/timestamppb" + "time" +) + +func grpcTimestampToGoTime(t *timestamppb.Timestamp) time.Time { + // 不同的 puppet 返回的时间格式不一致, 需要做转换兼容 + // padlocal 返回的是秒,puppet-service 当作是毫秒单位转为秒(除以1000),所以这里 t.Seconds 就只剩下七位,另外3为被分配到 t.Nanos 去了 + // https://github.com/wechaty/puppet-service/blob/4de1024ee9b615af6c44674f684a84dd8c11ae9e/src/pure-functions/timestamp.ts#L7-L17 + + // 这里我们判断 t.Seconds 是否为7位来特殊处理 + //TODO(dchaofei): 未来时间戳每增加一位这里就要判断加一位,那就是200多年之后的事情了,到时还有人在用 wechaty 吗?(2023-09-09) + if t.Seconds/10000000 < 1 { + second := t.Seconds*1000 + int64(t.Nanos)/1000000 + return time.Unix(second, 0) + } + + return t.AsTime().Local() +} diff --git a/wechaty-puppet-service/puppet_service.go b/wechaty-puppet-service/puppet_service.go index 8978532..68f03a0 100644 --- a/wechaty-puppet-service/puppet_service.go +++ b/wechaty-puppet-service/puppet_service.go @@ -14,7 +14,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/connectivity" "google.golang.org/grpc/credentials/insecure" - "google.golang.org/protobuf/types/known/timestamppb" "io" "time" ) @@ -645,11 +644,6 @@ func (p *PuppetService) MessageRawPayload(id string) (*schemas.MessagePayload, e return payload, nil } -func grpcTimestampToGoTime(t *timestamppb.Timestamp) time.Time { - second := t.Seconds*1000 + int64(t.Nanos)/1000000 - return time.Unix(second, 0) -} - // MessageSendText ... func (p *PuppetService) MessageSendText(conversationID string, text string, mentionIDList ...string) (string, error) { log.Tracef("PuppetService messageSendText(%s, %s)\n", conversationID, text)