| | | 1 | | //======================================================================= |
| | | 2 | | // RecordChronicleTime.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 | | using Chronicler.Timing; |
| | | 10 | | |
| | | 11 | | namespace Chronicler; |
| | | 12 | | |
| | | 13 | | /// <summary>Records exact time values through a versioned, transport-neutral component schema.</summary> |
| | | 14 | | public static class RecordChronicleTime |
| | | 15 | | { |
| | | 16 | | /// <summary>Records a signed interval at 2^-32-second precision.</summary> |
| | | 17 | | /// <param name="chronicler">The active pass supplied to <see cref="IRecordable.RecordData"/>.</param> |
| | | 18 | | /// <param name="value">The duration to record or update. A failed load leaves it unchanged.</param> |
| | | 19 | | /// <param name="name">The stable name of the nested time record.</param> |
| | | 20 | | /// <remarks>Version 1 records the schema version, floor seconds, and fractional units in that order. Missing compon |
| | | 21 | | /// <exception cref="InvalidOperationException">The nested record or its version is missing, or the schema version i |
| | | 22 | | public static void Look(IChronicler chronicler, ref ChronicleDuration value, string name) |
| | | 23 | | { |
| | 369 | 24 | | var record = new TimeRecord(value.WholeSeconds, value.FractionalSecond); |
| | 369 | 25 | | RecordDeepStruct.Look(chronicler, ref record, name); |
| | 369 | 26 | | record.ValidateSchema(); |
| | 343 | 27 | | value = new ChronicleDuration(record.WholeSeconds, record.FractionalSecond); |
| | 343 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary>Records a nonnegative instant at 2^-32-second precision.</summary> |
| | | 31 | | /// <param name="chronicler">The active pass supplied to <see cref="IRecordable.RecordData"/>.</param> |
| | | 32 | | /// <param name="value">The timestamp to record or update. A failed load leaves it unchanged.</param> |
| | | 33 | | /// <param name="name">The stable name of the nested time record.</param> |
| | | 34 | | /// <remarks>Version 1 records the schema version, floor seconds, and fractional units in that order. Missing compon |
| | | 35 | | /// <exception cref="InvalidOperationException">The nested record or its version is missing, or the schema version i |
| | | 36 | | /// <exception cref="ArgumentOutOfRangeException">The recorded timestamp precedes its origin.</exception> |
| | | 37 | | public static void Look(IChronicler chronicler, ref ChronicleTimestamp value, string name) |
| | | 38 | | { |
| | 357 | 39 | | var record = new TimeRecord(value.WholeSeconds, value.FractionalSecond); |
| | 357 | 40 | | RecordDeepStruct.Look(chronicler, ref record, name); |
| | 357 | 41 | | record.ValidateSchema(); |
| | 333 | 42 | | value = new ChronicleTimestamp(record.WholeSeconds, record.FractionalSecond); |
| | 329 | 43 | | } |
| | | 44 | | |
| | | 45 | | // Validate after the deep read so missing records are rejected as well. |
| | | 46 | | private struct TimeRecord : IRecordable |
| | | 47 | | { |
| | | 48 | | private int _schemaVersion; |
| | | 49 | | public long WholeSeconds; |
| | | 50 | | public uint FractionalSecond; |
| | | 51 | | |
| | | 52 | | public TimeRecord(long wholeSeconds, uint fractionalSecond) |
| | | 53 | | { |
| | 726 | 54 | | _schemaVersion = 1; |
| | 726 | 55 | | WholeSeconds = wholeSeconds; |
| | 726 | 56 | | FractionalSecond = fractionalSecond; |
| | 726 | 57 | | } |
| | | 58 | | |
| | | 59 | | public void RecordData(IChronicler chronicler) |
| | | 60 | | { |
| | 726 | 61 | | RecordValues.Look(chronicler, ref _schemaVersion, "SchemaVersion", 0); |
| | 726 | 62 | | RecordValues.Look(chronicler, ref WholeSeconds, nameof(WholeSeconds), 0L); |
| | 726 | 63 | | RecordValues.Look(chronicler, ref FractionalSecond, nameof(FractionalSecond), 0u); |
| | 726 | 64 | | } |
| | | 65 | | |
| | | 66 | | public readonly void ValidateSchema() |
| | | 67 | | { |
| | 726 | 68 | | if (_schemaVersion != 1) |
| | 50 | 69 | | throw new InvalidOperationException("The time value schema is missing or unsupported."); |
| | 676 | 70 | | } |
| | | 71 | | } |
| | | 72 | | } |