From f7d3dc108ff6ad6913905e17ec4ebbf69fc1174e Mon Sep 17 00:00:00 2001 From: Bobo Date: Mon, 7 Jul 2025 10:33:58 +0800 Subject: [PATCH] feat: Timestamp Conversions. --- timeutil/trans.go | 64 ++++++++++++++++++++++++++++++++++++++++++ timeutil/trans_test.go | 26 +++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/timeutil/trans.go b/timeutil/trans.go index 7df27bb..916e91d 100644 --- a/timeutil/trans.go +++ b/timeutil/trans.go @@ -296,3 +296,67 @@ func DurationpbToString(in *durationpb.Duration) *string { return trans.Ptr(in.AsDuration().String()) } + +// TimestampToSeconds 将 timestamppb.Timestamp 转换为秒 +func TimestampToSeconds(ts *timestamppb.Timestamp) int64 { + if ts == nil { + return 0 + } + return ts.AsTime().Unix() +} + +// SecondsToTimestamp 将秒转换为 timestamppb.Timestamp +func SecondsToTimestamp(seconds *int64) *timestamppb.Timestamp { + if seconds == nil { + return nil + } + return timestamppb.New(time.Unix(*seconds, 0)) +} + +func TimestampToMilliseconds(ts *timestamppb.Timestamp) int64 { + if ts == nil { + return 0 + } + + return ts.AsTime().UnixMilli() +} + +func MillisecondsToTimestamp(milliseconds *int64) *timestamppb.Timestamp { + if milliseconds == nil { + return nil + } + + return timestamppb.New(time.UnixMilli(*milliseconds)) +} + +func TimestampToMicroseconds(ts *timestamppb.Timestamp) int64 { + if ts == nil { + return 0 + } + + return ts.AsTime().UnixMicro() +} + +func MicrosecondsToTimestamp(microseconds *int64) *timestamppb.Timestamp { + if microseconds == nil { + return nil + } + + return timestamppb.New(time.UnixMicro(*microseconds)) +} + +// TimestampToNanoseconds 将 timestamppb.Timestamp 转换为纳秒 +func TimestampToNanoseconds(ts *timestamppb.Timestamp) int64 { + if ts == nil { + return 0 + } + return ts.AsTime().UnixNano() +} + +// NanosecondsToTimestamp 将纳秒转换为 timestamppb.Timestamp +func NanosecondsToTimestamp(nanoseconds *int64) *timestamppb.Timestamp { + if nanoseconds == nil { + return nil + } + return timestamppb.New(time.Unix(0, *nanoseconds)) +} diff --git a/timeutil/trans_test.go b/timeutil/trans_test.go index 505a837..79207ea 100644 --- a/timeutil/trans_test.go +++ b/timeutil/trans_test.go @@ -442,3 +442,29 @@ func TestDurationpbToString(t *testing.T) { result = DurationpbToString(nil) assert.Nil(t, result) } + +func TestTimestampConversions(t *testing.T) { + // 测试秒转换 + seconds := int64(1672531200) + ts := SecondsToTimestamp(&seconds) + assert.NotNil(t, ts) + assert.Equal(t, seconds, TimestampToSeconds(ts)) + + // 测试毫秒转换 + milliseconds := int64(1672531200123) + ts = MillisecondsToTimestamp(&milliseconds) + assert.NotNil(t, ts) + assert.Equal(t, milliseconds, TimestampToMilliseconds(ts)) + + // 测试微秒转换 + microseconds := int64(1672531200123456) + ts = MicrosecondsToTimestamp(µseconds) + assert.NotNil(t, ts) + assert.Equal(t, microseconds, TimestampToMicroseconds(ts)) + + // 测试纳秒转换 + nanoseconds := int64(1672531200123456789) + ts = NanosecondsToTimestamp(&nanoseconds) + assert.NotNil(t, ts) + assert.Equal(t, nanoseconds, TimestampToNanoseconds(ts)) +}