DFLayoutDateTimeFieldProperties.cs 1.4 KB

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