Skip to content

Commit

Permalink
fix(calendar): Do not modify time when switching months
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Sep 24, 2024
1 parent 08d763d commit ac6e02a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lua/orgmode/objects/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ end

function Calendar:forward()
self:_ensure_day()
self.date = self.date:start_of('month'):add({ month = vim.v.count1 })
self.date = self.date:set({ day = 1 }):add({ month = vim.v.count1 })
self:render()
vim.fn.cursor(2, 1)
vim.fn.search('01')
Expand All @@ -344,7 +344,7 @@ end

function Calendar:backward()
self:_ensure_day()
self.date = self.date:start_of('month'):subtract({ month = vim.v.count1 }):end_of('month')
self.date = self.date:set({ day = 1 }):subtract({ month = vim.v.count1 }):last_day_of_month()
self:render()
vim.fn.cursor(8, 0)
vim.fn.search([[\d\d]], 'b')
Expand Down
4 changes: 4 additions & 0 deletions lua/orgmode/objects/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ function Date:end_of(span)
return self
end

function Date:last_day_of_month()
return self:set({ day = Date._days_of_month(os_date(self.timestamp)) })
end

---@return number
function Date:get_isoweekday()
---@type table
Expand Down
10 changes: 10 additions & 0 deletions tests/plenary/object/date_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@ describe('Date object', function()
assert.are.same('2021-05-31 Mon 23:59', date:to_string())
end)

it('should get last day of the month', function()
local date = Date.from_string('2024-02-02 09:00')
date = date:last_day_of_month()
assert.are.same('2024-02-29 Thu 09:00', date:to_string())
date = date:add({ month = 1 }):last_day_of_month()
assert.are.same('2024-03-31 Sun 09:00', date:to_string())
date = date:set({ month = 6, day = 2 }):last_day_of_month()
assert.are.same('2024-06-30 Sun 09:00', date:to_string())
end)

it('should properly handle end of month', function()
local date = Date.from_string('2021-05-12')
date = date:end_of('month')
Expand Down

0 comments on commit ac6e02a

Please sign in to comment.