| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleTimestamp.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Chronicler.Timing; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// An exact nonnegative instant relative to a simulation origin, with 2^-32-second precision. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// The range is <c>(0, 0)</c> through <c>(long.MaxValue, uint.MaxValue)</c>. |
| | | 17 | | /// The owner matches origins and lifetimes before comparing or subtracting timestamps. |
| | | 18 | | /// </remarks> |
| | | 19 | | public readonly struct ChronicleTimestamp : IEquatable<ChronicleTimestamp>, IComparable<ChronicleTimestamp> |
| | | 20 | | { |
| | | 21 | | private readonly ChronicleDuration _value; |
| | | 22 | | |
| | | 23 | | /// <summary>Creates an instant from nonnegative whole seconds and an unsigned fraction.</summary> |
| | | 24 | | /// <param name="wholeSeconds">The mathematical floor of elapsed seconds since the origin.</param> |
| | | 25 | | /// <param name="fractionalSecond">The nonnegative remainder in units of 2^-32 seconds.</param> |
| | | 26 | | /// <exception cref="ArgumentOutOfRangeException">Whole seconds are negative.</exception> |
| | | 27 | | public ChronicleTimestamp(long wholeSeconds, uint fractionalSecond) |
| | | 28 | | { |
| | 5299 | 29 | | if (wholeSeconds < 0) |
| | 6 | 30 | | throw new ArgumentOutOfRangeException(nameof(wholeSeconds), "A timestamp cannot precede its origin."); |
| | 5293 | 31 | | _value = new ChronicleDuration(wholeSeconds, fractionalSecond); |
| | 5293 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary>Gets the simulation origin.</summary> |
| | | 35 | | /// <value>Both components are zero, which is also the default value.</value> |
| | 54 | 36 | | public static ChronicleTimestamp Zero => default; |
| | | 37 | | |
| | | 38 | | /// <summary>Gets floor seconds for this instant.</summary> |
| | | 39 | | /// <value>Nonnegative whole seconds since the simulation origin.</value> |
| | 1388 | 40 | | public long WholeSeconds => _value.WholeSeconds; |
| | | 41 | | |
| | | 42 | | /// <summary>Gets the remainder after floor seconds.</summary> |
| | | 43 | | /// <value>A nonnegative count of 2^-32-second units, from zero through 2^32 − 1.</value> |
| | 1388 | 44 | | public uint FractionalSecond => _value.FractionalSecond; |
| | | 45 | | |
| | | 46 | | /// <summary>Orders this instant relative to another.</summary> |
| | | 47 | | /// <param name="other">An instant from the same origin and lifetime.</param> |
| | | 48 | | /// <returns>A negative value for less, zero for equal, and a positive value for greater.</returns> |
| | 12241 | 49 | | public int CompareTo(ChronicleTimestamp other) => _value.CompareTo(other._value); |
| | | 50 | | |
| | | 51 | | /// <summary>Tests exact instant equality.</summary> |
| | | 52 | | /// <param name="other">An instant from the same origin and lifetime.</param> |
| | | 53 | | /// <returns>True when both components match.</returns> |
| | 5871 | 54 | | public bool Equals(ChronicleTimestamp other) => _value.Equals(other._value); |
| | | 55 | | |
| | | 56 | | /// <summary>Tests equality with a boxed instant.</summary> |
| | | 57 | | /// <param name="obj">The object to compare. Null or another type compares unequal.</param> |
| | | 58 | | /// <returns>True when the object has this value type and both components match.</returns> |
| | 4 | 59 | | public override bool Equals(object? obj) => obj is ChronicleTimestamp other && Equals(other); |
| | | 60 | | |
| | | 61 | | /// <summary>Hashes the instant for use in collections.</summary> |
| | | 62 | | /// <returns>A deterministic 32-bit mix of both components.</returns> |
| | 2051 | 63 | | public override int GetHashCode() => _value.GetHashCode(); |
| | | 64 | | |
| | | 65 | | /// <summary>Offsets an instant by a signed interval.</summary> |
| | | 66 | | /// <param name="time">The starting instant in the current timeline.</param> |
| | | 67 | | /// <param name="delta">The signed displacement in simulation seconds.</param> |
| | | 68 | | /// <returns>The resulting instant relative to the same origin.</returns> |
| | | 69 | | /// <exception cref="OverflowException">The result precedes the origin or exceeds the timestamp range.</exception> |
| | | 70 | | public static ChronicleTimestamp operator +(ChronicleTimestamp time, ChronicleDuration delta) |
| | | 71 | | { |
| | 4839 | 72 | | ChronicleDuration result = time._value + delta; |
| | 4811 | 73 | | if (result.WholeSeconds < 0) |
| | 27 | 74 | | throw new OverflowException("The timestamp would precede its origin."); |
| | 4784 | 75 | | return new ChronicleTimestamp(result.WholeSeconds, result.FractionalSecond); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary>Measures the signed interval between two instants.</summary> |
| | | 79 | | /// <param name="left">The ending instant.</param> |
| | | 80 | | /// <param name="right">The starting instant from the same origin and lifetime.</param> |
| | | 81 | | /// <returns>The exact difference, negative when the ending instant precedes the start.</returns> |
| | | 82 | | public static ChronicleDuration operator -(ChronicleTimestamp left, ChronicleTimestamp right) => |
| | 2462 | 83 | | left._value - right._value; |
| | | 84 | | |
| | | 85 | | /// <summary>Tests whether the left instant exactly equals the right instant.</summary> |
| | | 86 | | /// <param name="left">The first operand.</param> |
| | | 87 | | /// <param name="right">The second operand from the same origin and lifetime.</param> |
| | | 88 | | /// <returns>True when the left operand exactly equals the right operand.</returns> |
| | 426 | 89 | | public static bool operator ==(ChronicleTimestamp left, ChronicleTimestamp right) => left.Equals(right); |
| | | 90 | | /// <summary>Tests whether the left instant differs from the right instant.</summary> |
| | | 91 | | /// <param name="left">The first operand.</param> |
| | | 92 | | /// <param name="right">The second operand from the same origin and lifetime.</param> |
| | | 93 | | /// <returns>True when the left operand differs from the right operand.</returns> |
| | 2448 | 94 | | public static bool operator !=(ChronicleTimestamp left, ChronicleTimestamp right) => !left.Equals(right); |
| | | 95 | | /// <summary>Tests whether the left instant is less than the right instant.</summary> |
| | | 96 | | /// <param name="left">The first operand.</param> |
| | | 97 | | /// <param name="right">The second operand from the same origin and lifetime.</param> |
| | | 98 | | /// <returns>True when the left operand is less than the right operand.</returns> |
| | 2448 | 99 | | public static bool operator <(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) < 0; |
| | | 100 | | /// <summary>Tests whether the left instant is less than or equal to the right instant.</summary> |
| | | 101 | | /// <param name="left">The first operand.</param> |
| | | 102 | | /// <param name="right">The second operand from the same origin and lifetime.</param> |
| | | 103 | | /// <returns>True when the left operand is less than or equal to the right operand.</returns> |
| | 2448 | 104 | | public static bool operator <=(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) <= 0; |
| | | 105 | | /// <summary>Tests whether the left instant is greater than the right instant.</summary> |
| | | 106 | | /// <param name="left">The first operand.</param> |
| | | 107 | | /// <param name="right">The second operand from the same origin and lifetime.</param> |
| | | 108 | | /// <returns>True when the left operand is greater than the right operand.</returns> |
| | 4496 | 109 | | public static bool operator >(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) > 0; |
| | | 110 | | /// <summary>Tests whether the left instant is greater than or equal to the right instant.</summary> |
| | | 111 | | /// <param name="left">The first operand.</param> |
| | | 112 | | /// <param name="right">The second operand from the same origin and lifetime.</param> |
| | | 113 | | /// <returns>True when the left operand is greater than or equal to the right operand.</returns> |
| | 2448 | 114 | | public static bool operator >=(ChronicleTimestamp left, ChronicleTimestamp right) => left.CompareTo(right) >= 0; |
| | | 115 | | } |