Skip to content

Commit

Permalink
Fix the bits of Time in big-endian case
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Sep 22, 2023
1 parent 2981c6c commit 37fe5f0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Ulid.Unity/Assets/Scripts/Ulid/Ulid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public DateTimeOffset Time
{
if (BitConverter.IsLittleEndian)
{
// |A|B|C|D|E|F|G|H|... -> |F|E|D|C|B|A|0|0|
// |A|B|C|D|E|F|... -> |F|E|D|C|B|A|0|0|

// Lower |A|B|C|D| -> |D|C|B|A|
// Upper |E|F| -> |F|E|
Expand All @@ -105,14 +105,14 @@ public DateTimeOffset Time
}
else
{
// |A|B|C|D|E|F|G|H|... -> |A|B|C|D|E|F|0|0|
// |A|B|C|D|E|F|... -> |0|0|A|B|C|D|E|F|

// Upper |A|B|C|D|
// Lower |E|F|
// Time |A|B|C|D| + |E|F|0|0|
// Time |A|B|C|C|0|0| + |E|F|
var upper = Unsafe.As<byte, uint>(ref Unsafe.AsRef(this.timestamp0));
var lower = Unsafe.As<byte, ushort>(ref Unsafe.AsRef(this.timestamp4));
var time = ((long)upper << 32) + ((long)lower << 16);
var time = ((long)upper << 16) + (long)lower;
return DateTimeOffset.FromUnixTimeMilliseconds(time);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Ulid/Ulid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public DateTimeOffset Time
{
if (BitConverter.IsLittleEndian)
{
// |A|B|C|D|E|F|G|H|... -> |F|E|D|C|B|A|0|0|
// |A|B|C|D|E|F|... -> |F|E|D|C|B|A|0|0|

// Lower |A|B|C|D| -> |D|C|B|A|
// Upper |E|F| -> |F|E|
Expand All @@ -105,14 +105,14 @@ public DateTimeOffset Time
}
else
{
// |A|B|C|D|E|F|G|H|... -> |A|B|C|D|E|F|0|0|
// |A|B|C|D|E|F|... -> |0|0|A|B|C|D|E|F|

// Upper |A|B|C|D|
// Lower |E|F|
// Time |A|B|C|D| + |E|F|0|0|
// Time |A|B|C|C|0|0| + |E|F|
var upper = Unsafe.As<byte, uint>(ref Unsafe.AsRef(this.timestamp0));
var lower = Unsafe.As<byte, ushort>(ref Unsafe.AsRef(this.timestamp4));
var time = ((long)upper << 32) + ((long)lower << 16);
var time = ((long)upper << 16) + (long)lower;
return DateTimeOffset.FromUnixTimeMilliseconds(time);
}
}
Expand Down

0 comments on commit 37fe5f0

Please sign in to comment.