48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package ditime
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// Time is a DI-Net timestamp in milliseconds (UNIX in milliseconds)
|
|
type Time uint64
|
|
|
|
const milliToNano = int64(time.Millisecond) / int64(time.Nanosecond)
|
|
|
|
// Now gets the current local DI-Net timestamp (based on the UNIX timestamp)
|
|
func Now() Time {
|
|
return FromTime(time.Now())
|
|
}
|
|
|
|
// ToTime converts the DI-Net time to golang time.Time
|
|
func (t Time) ToTime() time.Time {
|
|
return ToTime(t)
|
|
}
|
|
|
|
// String gets the string presentation of time
|
|
func (t Time) String() string {
|
|
return t.ToTime().String()
|
|
}
|
|
|
|
// Sub returns the duration t-u. If the result exceeds the maximum (or minimum)
|
|
// value that can be stored in a Duration, the maximum (or minimum) duration will be returned.
|
|
func (t Time) Sub(u Time) time.Duration {
|
|
return t.ToTime().Sub(u.ToTime())
|
|
}
|
|
|
|
// Format Format returns a textual representation of the time value formatted according to layout
|
|
// which is compatible with stdlib time.Time.Format(layout)
|
|
func (t Time) Format(layout string) string {
|
|
return t.ToTime().Format(layout)
|
|
}
|
|
|
|
// FromTime converts golang time.Time to DI-Net Time
|
|
func FromTime(tm time.Time) Time {
|
|
return Time(tm.UTC().UnixNano() / milliToNano)
|
|
}
|
|
|
|
// ToTime converts DI-Net Time to golang time.Time
|
|
func ToTime(ms Time) time.Time {
|
|
return time.Unix(int64(ms/1000), int64(ms%1000)*milliToNano)
|
|
}
|