Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ac6e02a

Browse files
fix(calendar): Do not modify time when switching months
1 parent 08d763d commit ac6e02a

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

lua/orgmode/objects/calendar.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ end
335335

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

345345
function Calendar:backward()
346346
self:_ensure_day()
347-
self.date = self.date:start_of('month'):subtract({ month = vim.v.count1 }):end_of('month')
347+
self.date = self.date:set({ day = 1 }):subtract({ month = vim.v.count1 }):last_day_of_month()
348348
self:render()
349349
vim.fn.cursor(8, 0)
350350
vim.fn.search([[\d\d]], 'b')

lua/orgmode/objects/date.lua

+4
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ function Date:end_of(span)
498498
return self
499499
end
500500

501+
function Date:last_day_of_month()
502+
return self:set({ day = Date._days_of_month(os_date(self.timestamp)) })
503+
end
504+
501505
---@return number
502506
function Date:get_isoweekday()
503507
---@type table

tests/plenary/object/date_spec.lua

+10
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ describe('Date object', function()
303303
assert.are.same('2021-05-31 Mon 23:59', date:to_string())
304304
end)
305305

306+
it('should get last day of the month', function()
307+
local date = Date.from_string('2024-02-02 09:00')
308+
date = date:last_day_of_month()
309+
assert.are.same('2024-02-29 Thu 09:00', date:to_string())
310+
date = date:add({ month = 1 }):last_day_of_month()
311+
assert.are.same('2024-03-31 Sun 09:00', date:to_string())
312+
date = date:set({ month = 6, day = 2 }):last_day_of_month()
313+
assert.are.same('2024-06-30 Sun 09:00', date:to_string())
314+
end)
315+
306316
it('should properly handle end of month', function()
307317
local date = Date.from_string('2021-05-12')
308318
date = date:end_of('month')

0 commit comments

Comments
 (0)