tkcashgame_v4/pkg/utils/stime/time.go

55 lines
1.2 KiB
Go

package stime
import (
"time"
"google.golang.org/protobuf/types/known/timestamppb"
)
func FormatPBTime(pbtime *timestamppb.Timestamp) string {
if pbtime.AsTime().Unix() > 0 {
return pbtime.AsTime().Local().Format("2006-01-02 15:04:05")
}
return ""
}
func FormatPBTimeToTime(pbtime *timestamppb.Timestamp) time.Time {
return pbtime.AsTime().Local()
}
func FormatStringToTime(stime string) (time.Time, error) {
return time.ParseInLocation("2006-01-02 15:04:05", stime, time.Local)
}
func FormatStringToPBTime(stime string) (*timestamppb.Timestamp, error) {
t, err := time.ParseInLocation("2006-01-02 15:04:05", stime, time.Local)
if err != nil {
return nil, err
}
return FormatTimePB(t), nil
}
func FormatTimePB(ttime time.Time) *timestamppb.Timestamp {
return timestamppb.New(ttime)
}
func FormatTimeString(ttime time.Time) string {
if ttime.Unix() > 0 {
return ttime.Local().Format("2006-01-02 15:04:05")
}
return ""
}
func FormatInt64ToString(n int64) string {
time := time.Unix(n, 0)
return FormatTimeString(time)
}
func FormatStringToInt64(stime string) int64 {
time, err := FormatStringToTime(stime)
if err != nil {
return 0
}
return time.Unix()
}