PremiereDateComparer.cs 1.7 KB

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