-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Remove custom protobuf time conversion functions
- Loading branch information
Showing
8 changed files
with
51 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,9 @@ | ||
package types | ||
|
||
import ( | ||
"time" | ||
|
||
"google.golang.org/protobuf/types/known/durationpb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func ConvertTime(t *time.Time) *timestamppb.Timestamp { | ||
if t == nil { | ||
return nil | ||
func valOrZero[T any](p *T) T { | ||
var v T | ||
if p != nil { | ||
v = *p | ||
} | ||
return timestamppb.New(*t) | ||
} | ||
|
||
func ConvertUnixSeconds(ts int64) *timestamppb.Timestamp { | ||
return ×tamppb.Timestamp{ | ||
Seconds: ts, | ||
Nanos: 0, | ||
} | ||
} | ||
|
||
func ConvertUnixMilli(ts int64) *timestamppb.Timestamp { | ||
const msPerSecond int64 = 1_000 | ||
const nsPerMilli int64 = 1_000 | ||
return ×tamppb.Timestamp{ | ||
Seconds: ts / msPerSecond, | ||
Nanos: int32((ts % msPerSecond) * nsPerMilli), | ||
} | ||
} | ||
|
||
func ConvertUnixNano(ts int64) *timestamppb.Timestamp { | ||
const nsPerSecond int64 = 1_000_000_000 | ||
return ×tamppb.Timestamp{ | ||
Seconds: ts / nsPerSecond, | ||
Nanos: int32(ts % nsPerSecond), | ||
} | ||
} | ||
|
||
func ConvertDuration(d float64) *durationpb.Duration { | ||
return durationpb.New(time.Duration(d * float64(time.Second))) | ||
return v | ||
} |