Skip to content

Commit

Permalink
Merge pull request #54 from radekg/hybrid-time-utils
Browse files Browse the repository at this point in the history
Add utility functions to simplify working with hybrid time
  • Loading branch information
radekg authored Dec 3, 2021
2 parents befdd7a + 6f0c069 commit d654563
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .compose/yugabytedb-minimal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ services:
container_name: yb-master-2
networks:
- yb-client-minimal
ports:
- 7101:7100
command: [ "/home/${YB_USER}/bin/yb-master",
"--callhome_enabled=false",
"--fs_data_dirs=${YB_MOUNT_PREFIX}/master",
Expand All @@ -48,6 +50,8 @@ services:
container_name: yb-master-3
networks:
- yb-client-minimal
ports:
- 7102:7100
command: [ "/home/${YB_USER}/bin/yb-master",
"--callhome_enabled=false",
"--fs_data_dirs=${YB_MOUNT_PREFIX}/master",
Expand Down
15 changes: 15 additions & 0 deletions utils/hybridtime/hybrid_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ func AddDuration(ht uint64, d time.Duration) uint64 {
func SubstractDuration(ht uint64, d time.Duration) uint64 {
return ht - ClockTimestampToHTTimestamp(uint64(d.Microseconds()))
}

// UnixTimeFromHT takes a hybrid time and returns time.Time.
// Nanoseconds are lost in this conversion.
func UnixTimeFromHT(ht uint64) time.Time {
uints := HTTimestampToPhysicalAndLogical(ht)
sec := int64(uints[0] / 1000000)
return time.Unix(sec, 0)
}

// UnixTimeToHT takes a time.Time and returns a hybrid time.
// Nanoseconds are lost in this conversion.
func UnixTimeToHT(t time.Time) uint64 {
// drop nanoseconds:
return PhysicalAndLogicalToHTTimestamp(uint64(t.Unix()*1000000), 0)
}
36 changes: 36 additions & 0 deletions utils/hybridtime/hybrid_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package hybridtime

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestHybridTime(t *testing.T) {

t.Run("it=handles fixed point in time", func(tt *testing.T) {
// this is a fixed server clock pointing at exactly '2021-12-03 00:27:34 +0000 UTC':
fixedPointInTime := uint64(6711260180246810624)
resultTime := UnixTimeFromHT(fixedPointInTime)
assert.Equal(tt, "2021-12-03 00:27:34 +0000 UTC", resultTime.UTC().String())
})

t.Run("it=converts times correctly", func(tt *testing.T) {
now := time.Now().UTC()
resultHT := UnixTimeToHT(now)
resultTime := UnixTimeFromHT(resultHT)
assert.Equal(tt, now.Unix(), resultTime.Unix())
})

t.Run("it=handles add and substract", func(tt *testing.T) {
now := time.Now().UTC()
resultHT := UnixTimeToHT(now)
afterAddHT := AddDuration(resultHT, time.Duration(time.Hour*48))
afterSubstractHT := SubstractDuration(afterAddHT, time.Duration(time.Hour*48))
resultTime := UnixTimeFromHT(afterSubstractHT)
assert.Equal(tt, now.Unix(), resultTime.Unix())

})

}

0 comments on commit d654563

Please sign in to comment.