DFLayoutDateFieldProperties.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Globalization;
  4. namespace InABox.Core
  5. {
  6. public class DFLayoutDateFieldProperties : DFLayoutFieldProperties<DateTime, DateTime?>
  7. {
  8. public DFLayoutDateFieldProperties()
  9. {
  10. Format = "dd MMM yy";
  11. }
  12. public string Format { get; set; }
  13. public override string FormatValue(DateTime? value)
  14. {
  15. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format.Replace(":", "\\:")) + "}", value);
  16. }
  17. public override DateTime GetValue(DateTime? value)
  18. {
  19. return value ?? Default;
  20. }
  21. public override void SerializeValue(DFSaveStorageEntry entry, DateTime? value)
  22. {
  23. if(value != null)
  24. {
  25. entry.SetValue(value.Value.ToString("dd-MM-yyyy"));
  26. }
  27. }
  28. public override DateTime? DeserializeValue(DFLoadStorageEntry entry)
  29. {
  30. var value = entry.GetValue();
  31. if (value is DateTime date)
  32. return date;
  33. if (DateTime.TryParseExact(value as string, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
  34. return result;
  35. return null;
  36. }
  37. protected override void LoadProperties()
  38. {
  39. base.LoadProperties();
  40. Format = GetProperty("Format", Format);
  41. }
  42. protected override void SaveProperties()
  43. {
  44. base.SaveProperties();
  45. SetProperty("Format", Format);
  46. }
  47. }
  48. }