package utils

import (
	"time"
)

// PhysicalShiftBits is the number of bits TSO uses for the logical part (default 18 in TiDB).
const PhysicalShiftBits = 18

// TimeToTSO converts a time.Time to a TiDB TSO (uint64).
func TimeToTSO(t time.Time) uint64 {
	return uint64(t.UnixMilli()) << PhysicalShiftBits
}

// TSOToTime extract physical part and convert to time.Time, it will not check the logical part.
func TSOToTime(tso uint64) time.Time {
	physical := tso >> PhysicalShiftBits
	return time.Unix(0, int64(physical)*int64(time.Millisecond))
}

// IsValidTSO checks if a TSO is valid.
func IsValidTSO(tso uint64) bool {
	return tso>>PhysicalShiftBits != 0
}
