github.com/blend/go-sdk@v1.20220411.3/protoutil/timestamp.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package protoutil
     9  
    10  import (
    11  	"time"
    12  
    13  	"github.com/golang/protobuf/ptypes/timestamp"
    14  )
    15  
    16  // Timestamp returns a proto timestamp.
    17  //
    18  // NOTE: protobuf timestamps are always transmitted as UTC.
    19  func Timestamp(t time.Time) *timestamp.Timestamp {
    20  	if t.IsZero() {
    21  		return nil
    22  	}
    23  	return &timestamp.Timestamp{
    24  		Seconds: int64(t.UTC().Unix()),
    25  		Nanos:   int32(t.UTC().Nanosecond()),
    26  	}
    27  }
    28  
    29  // FromTimestamp returns a time.Time.
    30  //
    31  // NOTE: protobuf timestamps are always transmitted as UTC.
    32  func FromTimestamp(t *timestamp.Timestamp) time.Time {
    33  	if t == nil {
    34  		return time.Time{}
    35  	}
    36  	return time.Unix(t.Seconds, int64(t.Nanos)).UTC()
    37  }