github.com/hedzr/evendeep@v0.4.8/internal/grpctool/tool.go (about)

     1  package grpctool
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // // toTime converts an 8-byte Windows Filetime to time.Time.
     8  // func toTime(t [8]byte) time.Time {
     9  //	ft := &windows.Filetime{
    10  //		LowDateTime:  binary.LittleEndian.Uint32(t[:4]),
    11  //		HighDateTime: binary.LittleEndian.Uint32(t[4:]),
    12  //	}
    13  //	return time.Unix(0, ft.Nanoseconds())
    14  // }
    15  
    16  // Int64ToTime converts utcTimeNanoSeconds (int64) to time.Time.
    17  // utcTimeNanoSeconds is a value in nanoseconds.
    18  func Int64ToTime(utcTimeNanoSeconds int64) (tm time.Time) {
    19  	return time.Unix(0, utcTimeNanoSeconds)
    20  }
    21  
    22  // Int64SecondsToTime converts utcTimeSeconds (int64) to time.Time.
    23  // utcTimeSeconds is a value in seconds.
    24  //
    25  // utcTimeSeconds is a standard unix timestamp.
    26  //
    27  // Unix returns the local Time corresponding to the given Unix
    28  // time, sec seconds and nsec nanoseconds since January 1, 1970
    29  // UTC. It is valid to pass nsec outside the range [0, 999999999].
    30  // Not all sec values have a corresponding time value. One such
    31  // value is 1<<63-1 (the largest int64 value).
    32  func Int64SecondsToTime(utcTimeSeconds int64) (tm time.Time) {
    33  	return time.Unix(utcTimeSeconds, 0)
    34  }
    35  
    36  // func TimestampToTime(ts *timestamp.Timestamp) (tm time.Time) {
    37  //	var err error
    38  //	tm, err = ptypes.Timestamp(ts)
    39  //	if err != nil {
    40  //		logrus.Warnf("CAN'T extract pb ptypes.timestamp to time: %v", err)
    41  //	}
    42  //	return
    43  // }
    44  //
    45  // func TimeToTimestamp(tm time.Time) (ts *timestamp.Timestamp) {
    46  //	var err error
    47  //	ts, err = ptypes.TimestampProto(tm)
    48  //	if err != nil {
    49  //		logrus.Warnf("CAN'T convert time to pb ptypes.timestamp: %v", err)
    50  //	}
    51  //	return
    52  // }
    53  //
    54  // func Int64ToTimestamp(utcTime int64) (ts *timestamp.Timestamp) {
    55  //	tm := Int64ToTime(utcTime)
    56  //	ts = &timestamp.Timestamp{Seconds: int64(tm.Unix()), Nanos: int32(tm.Nanosecond())}
    57  //	return
    58  // }
    59  
    60  // DecodeZigZagInt decodes a protobuffer variable integer (zigzag format).
    61  func DecodeZigZagInt(b []byte) (r int64, ate int) {
    62  	var b1 byte
    63  	var sh uint
    64  	for i := 0; i < len(b); i++ {
    65  		b1 = b[i]
    66  		if b1&0x80 == 0 {
    67  			r += int64(uint64(b1)) << sh
    68  			break
    69  		} else {
    70  			r += int64(b1&0x7f) << sh //nolint:gomnd //bitwise calculating here
    71  			sh += 7
    72  			ate++
    73  		}
    74  	}
    75  	ate++
    76  	return
    77  }
    78  
    79  // DecodeZigZagUint decodes a protobuffer variable integer (zigzag format) and return it as an uint64 umber.
    80  func DecodeZigZagUint(b []byte) (r uint64, ate int) {
    81  	var b1 byte
    82  	var sh uint
    83  	for i := 0; i < len(b); i++ {
    84  		b1 = b[i]
    85  		if b1&0x80 == 0 {
    86  			r += uint64(b1) << sh
    87  			break
    88  		} else {
    89  			r += uint64(b1&0x7f) << sh //nolint:gomnd //bitwise calculating here
    90  			sh += 7
    91  			ate++
    92  		}
    93  	}
    94  	ate++
    95  	return
    96  }