feat: Timestamp Conversions.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user