using Newtonsoft.Json.Linq; using System; using System.Globalization; namespace InABox.Core { public class DFLayoutTimeFieldProperties : DFLayoutFieldProperties { [EditorSequence(-995)] [TimeOfDayEditor] public override TimeSpan Default { get; set; } public DFLayoutTimeFieldProperties() { Format = "hh:mm"; } public string Format { get; set; } public override string FormatValue(TimeSpan? value) { var time = value ?? default; return string.IsNullOrWhiteSpace(Format) || string.Equals(Format, "hh:mm") ? Math.Truncate(time.TotalHours).ToString("#00") + ":" + time.Minutes.ToString("D2") : string.Format("{0:" + Format.Replace(":", "\\:") + "}", time); } public override void SerializeValue(DFSaveStorageEntry entry, TimeSpan? value) { if (value.HasValue) { entry.SetValue(value.Value.ToString("c")); } } public override TimeSpan? DeserializeValue(DFLoadStorageEntry entry) { var value = entry.GetValue(); if (value is TimeSpan time) return time; if (TimeSpan.TryParseExact(value as string, "c", CultureInfo.InvariantCulture, TimeSpanStyles.None, out var result)) return result; return null; } public override TimeSpan GetValue(TimeSpan? value) { return value ?? Default; } protected override void LoadProperties() { base.LoadProperties(); Format = GetProperty("Format", "hh:mm"); } protected override void SaveProperties() { base.SaveProperties(); SetProperty("Format", Format); } } }