Skip to content

Commit

Permalink
Change how Time is constructed in uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Feoramund committed Jun 22, 2024
1 parent 4481f9c commit 8a4a3ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
8 changes: 3 additions & 5 deletions core/encoding/uuid/reading.odin
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ Returns:
- timestamp: The timestamp of the UUID.
*/
time_v1 :: proc "contextless" (id: Identifier) -> (timestamp: time.Time) {
delta := cast(time.Duration)(raw_time_v1(id) - HNS_INTERVALS_BETWEEN_GREG_AND_UNIX) * 100
return time.time_add({}, delta)
return { _nsec = cast(i64)(raw_time_v1(id) - HNS_INTERVALS_BETWEEN_GREG_AND_UNIX) * 100 }
}

/*
Expand Down Expand Up @@ -195,8 +194,7 @@ Returns:
- timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.
*/
time_v6 :: proc "contextless" (id: Identifier) -> (timestamp: time.Time) {
delta := cast(time.Duration)(raw_time_v6(id) - HNS_INTERVALS_BETWEEN_GREG_AND_UNIX) * 100
return time.time_add({}, delta)
return { _nsec = cast(i64)(raw_time_v6(id) - HNS_INTERVALS_BETWEEN_GREG_AND_UNIX) * 100 }
}

/*
Expand All @@ -223,7 +221,7 @@ Returns:
- timestamp: The timestamp, in milliseconds since the UNIX epoch.
*/
time_v7 :: proc "contextless" (id: Identifier) -> (timestamp: time.Time) {
return time.time_add({}, cast(time.Duration)(raw_time_v7(id) * 1e6))
return { _nsec = cast(i64)raw_time_v7(id) * 1e6 }
}

/*
Expand Down
48 changes: 24 additions & 24 deletions tests/core/encoding/uuid/test_core_uuid.odin
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ test_legacy_namespaced_uuids :: proc(t: ^testing.T) {
test_v1 :: proc(t: ^testing.T) {
context.random_generator = crypto.random_generator()

point_a := time.time_add({}, 1 * time.Second)
point_b := time.time_add({}, 3 * time.Second)
point_c := time.time_add({}, 5 * time.Second)
point_a := time.unix(1, 0)
point_b := time.unix(3, 0)
point_c := time.unix(5, 0)

CLOCK :: 0x3A1A
mac := [6]u8{0xFF, 0x10, 0xAA, 0x55, 0x01, 0xFF}
Expand Down Expand Up @@ -179,9 +179,9 @@ test_v1 :: proc(t: ^testing.T) {
test_v6 :: proc(t: ^testing.T) {
context.random_generator = crypto.random_generator()

point_a := time.time_add({}, 1 * time.Second)
point_b := time.time_add({}, 3 * time.Second)
point_c := time.time_add({}, 5 * time.Second)
point_a := time.unix(1, 0)
point_b := time.unix(3, 0)
point_c := time.unix(5, 0)

CLOCK :: 0x3A1A
mac := [6]u8{0xFF, 0x10, 0xAA, 0x55, 0x01, 0xFF}
Expand Down Expand Up @@ -214,9 +214,9 @@ test_v6 :: proc(t: ^testing.T) {
test_v7 :: proc(t: ^testing.T) {
context.random_generator = crypto.random_generator()

point_a := time.time_add({}, 1 * time.Second)
point_b := time.time_add({}, 3 * time.Second)
point_c := time.time_add({}, 5 * time.Second)
point_a := time.unix(1, 0)
point_b := time.unix(3, 0)
point_c := time.unix(5, 0)

v7_a := uuid.generate_v7(point_a)
v7_b := uuid.generate_v7(point_b)
Expand Down Expand Up @@ -245,11 +245,11 @@ test_sorting_v1 :: proc(t: ^testing.T) {
// They are incapable of sorting properly by the nature their time bit ordering.
//
// Something is very strange if they do sort correctly.
point_a := time.time_add({}, 1 * time.Second)
point_b := time.time_add({}, 3 * time.Second)
point_c := time.time_add({}, 5 * time.Second)
point_d := time.time_add({}, 7 * time.Second)
point_e := time.time_add({}, 11 * time.Second)
point_a := time.unix(1, 0)
point_b := time.unix(3, 0)
point_c := time.unix(5, 0)
point_d := time.unix(7, 0)
point_e := time.unix(11, 0)

mac: [6]byte
v1_a := uuid.generate_v1(0, mac, point_a)
Expand Down Expand Up @@ -283,11 +283,11 @@ test_sorting_v1 :: proc(t: ^testing.T) {
test_sorting_v6 :: proc(t: ^testing.T) {
context.random_generator = crypto.random_generator()

point_a := time.time_add({}, 1 * time.Second)
point_b := time.time_add({}, 3 * time.Second)
point_c := time.time_add({}, 5 * time.Second)
point_d := time.time_add({}, 7 * time.Second)
point_e := time.time_add({}, 11 * time.Second)
point_a := time.unix(1, 0)
point_b := time.unix(3, 0)
point_c := time.unix(5, 0)
point_d := time.unix(7, 0)
point_e := time.unix(11, 0)

mac: [6]byte
v6_a := uuid.generate_v6(0, mac, point_a)
Expand Down Expand Up @@ -326,11 +326,11 @@ test_sorting_v6 :: proc(t: ^testing.T) {
test_sorting_v7 :: proc(t: ^testing.T) {
context.random_generator = crypto.random_generator()

point_a := time.time_add({}, 1 * time.Second)
point_b := time.time_add({}, 3 * time.Second)
point_c := time.time_add({}, 5 * time.Second)
point_d := time.time_add({}, 7 * time.Second)
point_e := time.time_add({}, 11 * time.Second)
point_a := time.unix(1, 0)
point_b := time.unix(3, 0)
point_c := time.unix(5, 0)
point_d := time.unix(7, 0)
point_e := time.unix(11, 0)

v7_a := uuid.generate_v7(point_a)
v7_b := uuid.generate_v7(point_b)
Expand Down

0 comments on commit 8a4a3ed

Please sign in to comment.