2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
F K ff784b31d1
[Framework] Add a DateTime property for CarrierJumpRequest DepartureTime (#119)
Refactored out the logic backing the JournalBase TimestampDateTime property so it can be used for any DateTime type property, providing a standardized json String -> DateTime conversion for any date-time property. Implemented as an `internal static` method on JournalBase so journal objects which inherit from JournalBase or don't inherit from it can use it.

Used this to provide a DepatureTimeDateTime on CarrierJumpRequest (this property was added in Update 14) and to implement the existing ExpiryDateTime on CurrentGoal.

From a quick search in the journal documentation, I don't see any other applications for this.
2023-08-14 09:35:00 -02:30

57 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace Observatory.Framework.Files.Journal
{
public class JournalBase
{
[JsonPropertyName("timestamp")]
public string Timestamp { get; init; }
[JsonIgnore]
public DateTime TimestampDateTime
{
get => ParseDateTime(Timestamp);
}
[JsonPropertyName("event")]
public string Event { get; init; }
[JsonExtensionData]
public Dictionary<string, object> AdditionalProperties { get; init; }
[JsonIgnore]
public string Json
{
get => json;
set
{
if (json == null || string.IsNullOrWhiteSpace(json))
{
json = value;
}
else
{
throw new Exception("Journal property \"Json\" can only be set while empty.");
}
}
}
private string json;
// For use by Journal object classes for .*DateTime properties, like TimestampeDateTime, above.
internal static DateTime ParseDateTime(string value)
{
if (DateTime.TryParseExact(value, "yyyy-MM-ddTHH:mm:ssZ", null, System.Globalization.DateTimeStyles.AssumeUniversal, out DateTime dateTimeValue))
{
return dateTimeValue;
}
else
{
return new DateTime();
}
}
}
}