PremiereDateComparer.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Sorting;
  4. using MediaBrowser.Model.Querying;
  5. namespace Emby.Server.Implementations.Sorting
  6. {
  7. /// <summary>
  8. /// Class PremiereDateComparer.
  9. /// </summary>
  10. public class PremiereDateComparer : IBaseItemComparer
  11. {
  12. /// <summary>
  13. /// Gets the name.
  14. /// </summary>
  15. /// <value>The name.</value>
  16. public string Name => ItemSortBy.PremiereDate;
  17. /// <summary>
  18. /// Compares the specified x.
  19. /// </summary>
  20. /// <param name="x">The x.</param>
  21. /// <param name="y">The y.</param>
  22. /// <returns>System.Int32.</returns>
  23. public int Compare(BaseItem? x, BaseItem? y)
  24. {
  25. return GetDate(x).CompareTo(GetDate(y));
  26. }
  27. /// <summary>
  28. /// Gets the date.
  29. /// </summary>
  30. /// <param name="x">The x.</param>
  31. /// <returns>DateTime.</returns>
  32. private static DateTime GetDate(BaseItem? x)
  33. {
  34. if (x == null)
  35. {
  36. return DateTime.MinValue;
  37. }
  38. if (x.PremiereDate.HasValue)
  39. {
  40. return x.PremiereDate.Value;
  41. }
  42. if (x.ProductionYear.HasValue)
  43. {
  44. try
  45. {
  46. return new DateTime(x.ProductionYear.Value, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  47. }
  48. catch (ArgumentOutOfRangeException)
  49. {
  50. // Don't blow up if the item has a bad ProductionYear, just return MinValue
  51. }
  52. }
  53. return DateTime.MinValue;
  54. }
  55. }
  56. }