【源码阅读】 protobuf 中的 timestamp 包

Timestamp

path: google.golang.org/protobuf/types/known/timestamppb
在 timestamppb 中 Timestamp 包含两个字段
seconds 表示秒
nanos 表示纳秒

message Timestamp {
	int64 seconds = 1;
	int32 nanos = 2;
}

timestamp.go

path: github.com/golang/protobuf/ptypes/timestamp.go

package ptypes

import (
	"errors"
	"fmt"
	"time"

	timestamppb "github.com/golang/protobuf/ptypes/timestamp"
)

const (
	// 定义了当前包内方法所支持的最大时间戳和最小时间戳
	// 最小时间开始于 1 年 1 月 1 日 0:0:0 0
	minValidSeconds = -62135596800
	// 最大时间结束于 10000 年 1 月 1 日 0:0:0 0 
	maxValidSeconds = 253402300800
)

// Timestamp 方法将 *timestamppb.Timestamp 类型转换为 time.Time 类型
// 如果参数值为 nil,则返回 time.Unix(0, 0).UTC() 
func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) {
	var t time.Time
	if ts == nil {
		t = time.Unix(0, 0).UTC() 
	} else {
		t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
	}
	return t, validateTimestamp(ts)
}

// TimestampNow 方法返回一个当前时间的 timestamppb.Timestamp
func TimestampNow() *timestamppb.Timestamp {
	ts, err := TimestampProto(time.Now())
	if err != nil {
		panic("ptypes: time.Now() out of Timestamp range")
	}
	return ts
}

// TimestampProto 方法将 time.Time 类型转换为 *timestamppb.Timestamp 类型
func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) {
	ts := &timestamppb.Timestamp{
		Seconds: t.Unix(),
		Nanos:   int32(t.Nanosecond()),
	}
	if err := validateTimestamp(ts); err != nil {
		return nil, err
	}
	return ts, nil
}

// TimestampString 方法将 *timestamppb.Timestamp 转换为时间字符串
func TimestampString(ts *timestamppb.Timestamp) string {
	t, err := Timestamp(ts)
	if err != nil {
		return fmt.Sprintf("(%v)", err)
	}
	return t.Format(time.RFC3339Nano)
}

// validateTimestamp 方法,判断当前参数是否有效,以下几种场景
// 场景一:为 nil 报错
// 场景二:小于允许最小值,即表示 0001-01-01 以前的时间
// 场景三:大于允许最大值,即表示 10000-01-01 以后的时间
// 场景四:纳秒范围在允许值范围内
func validateTimestamp(ts *timestamppb.Timestamp) error {
	if ts == nil {
		return errors.New("timestamp: nil Timestamp")
	}
	if ts.Seconds < minValidSeconds {
		return fmt.Errorf("timestamp: %v before 0001-01-01", ts)
	}
	if ts.Seconds >= maxValidSeconds {
		return fmt.Errorf("timestamp: %v after 10000-01-01", ts)
	}
	if ts.Nanos < 0 || ts.Nanos >= 1e9 {
		return fmt.Errorf("timestamp: %v: nanos not in range [0, 1e9)", ts)
	}
	return nil
}
原创文章 44 获赞 15 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_32828933/article/details/105773544
今日推荐