From fccccd432f8c9b8aadba51eea09a64200d718bc1 Mon Sep 17 00:00:00 2001
From: Kristijan Husak <husakkristijan@gmail.com>
Date: Sun, 12 Jan 2025 23:40:35 +0100
Subject: [PATCH] fix(dates): Check if date is without a time when doing
 comparison

When comparing two dates, and one does not have a specified time,
comparison should check only dates.
---
 lua/orgmode/objects/date.lua | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/lua/orgmode/objects/date.lua b/lua/orgmode/objects/date.lua
index feca8d0fa..2ced5ad2b 100644
--- a/lua/orgmode/objects/date.lua
+++ b/lua/orgmode/objects/date.lua
@@ -30,23 +30,38 @@ local time_format = '%H:%M'
 local Date = {
   ---@type fun(this: OrgDate, other: OrgDate): boolean
   __eq = function(this, other)
-    return this.timestamp == other.timestamp
+    if this.date_only ~= other.date_only then
+      return this:is_same(other, 'day')
+    end
+    return this:is_same(other)
   end,
   ---@type fun(this: OrgDate, other: OrgDate): boolean
   __lt = function(this, other)
-    return this.timestamp < other.timestamp
+    if this.date_only ~= other.date_only then
+      return this:is_before(other, 'day')
+    end
+    return this:is_before(other)
   end,
   ---@type fun(this: OrgDate, other: OrgDate): boolean
   __le = function(this, other)
-    return this.timestamp <= other.timestamp
+    if this.date_only ~= other.date_only then
+      return this:is_same_or_before(other, 'day')
+    end
+    return this:is_same_or_before(other)
   end,
   ---@type fun(this: OrgDate, other: OrgDate): boolean
   __gt = function(this, other)
-    return this.timestamp > other.timestamp
+    if this.date_only ~= other.date_only then
+      return this:is_after(other, 'day')
+    end
+    return this:is_after(other)
   end,
   ---@type fun(this: OrgDate, other: OrgDate): boolean
   __ge = function(this, other)
-    return this.timestamp >= other.timestamp
+    if this.date_only ~= other.date_only then
+      return this:is_same_or_after(other, 'day')
+    end
+    return this:is_same_or_after(other)
   end,
 }
 
@@ -85,7 +100,7 @@ function Date:new(data)
   opts.active = data.active or false
   opts.range = data.range
   opts.timestamp = os.time(opts)
-  opts.date_only = date_only
+  opts.date_only = date_only or false
   opts.dayname = os.date('%a', opts.timestamp) --[[@as string]]
   opts.is_dst = os_date(opts.timestamp).isdst
   opts.adjustments = data.adjustments or {}
@@ -628,7 +643,7 @@ function Date:is_before(date, span)
 end
 
 ---@param date OrgDate
----@param span string
+---@param span? string
 ---@return boolean
 function Date:is_same_or_before(date, span)
   local d = date
@@ -641,7 +656,7 @@ function Date:is_same_or_before(date, span)
 end
 
 ---@param date OrgDate
----@param span string
+---@param span? string
 ---@return boolean
 function Date:is_after(date, span)
   return not self:is_same_or_before(date, span)