Skip to content

Commit 86b8ade

Browse files
authored
Merge pull request #26 from natekspencer/fix-4-week-calendar
Fix calendar generation for month/year combinations with exactly 4 full weeks (28 days)
2 parents 1ee8ef9 + 9908ade commit 86b8ade

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

php/tests/CalendarTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Vestaboard\Vbml\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Vestaboard\Vbml\Calendar;
7+
8+
class CalendarTest extends TestCase
9+
{
10+
public function testShouldCreateCalendarWithOnly4Weeks(): void
11+
{
12+
$result = Calendar::makeCalendar(
13+
'2',
14+
'2026',
15+
[]
16+
);
17+
$this->assertEquals([
18+
[28, 59, 28, 32, 0, 19, 13, 20, 23, 20, 6, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
19+
[0, 27, 44, 33, 0, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
20+
[0, 34, 44, 27, 30, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
21+
[27, 31, 44, 28, 27, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
22+
[28, 28, 44, 28, 34, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
23+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
24+
], $result);
25+
}
26+
}

python/pyvbml/calendar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def make_calendar(
5959
fifth_row_days = (
6060
[str(fifth_start), str(fifth_end)] if fifth_start <= num_days else None
6161
)
62-
num_days_last_row = fifth_end - (7 - offset + 22) + 1
62+
num_days_last_row = fifth_end - fifth_start + 1
6363

6464
def dc(digit: str) -> int:
6565
"""Digit → character code, or 0 when dates are hidden."""

src/__tests__/calendar.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { makeCalendar } from "../calendar";
2+
3+
describe("Calendar", () => {
4+
it("Should create calendar with only 4 weeks", () => {
5+
const result = makeCalendar(
6+
"2",
7+
"2026",
8+
{},
9+
);
10+
expect(result).toEqual([
11+
[28, 59, 28, 32, 0, 19, 13, 20, 23, 20, 6, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
12+
[0, 27, 44, 33, 0, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
13+
[0, 34, 44, 27, 30, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
14+
[27, 31, 44, 28, 27, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
15+
[28, 28, 44, 28, 34, 65, 65, 65, 65, 65, 65, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
16+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
17+
]);
18+
});
19+
});

src/calendar.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ export const makeCalendar = (
4646
const secondRowDays = [`${7 - offset + 1}`, `${7 - offset + 7}`];
4747
const thirdRowDays = [`${7 - offset + 8}`, `${7 - offset + 14}`];
4848
const fourthRowDays = [`${7 - offset + 15}`, `${7 - offset + 21}`];
49-
const fifthRowDays = [
50-
`${7 - offset + 22}`,
51-
`${Math.min(7 - offset + numberOfDaysInMonth, numberOfDaysInMonth)}`,
52-
];
53-
const numberOfDaysInLastRow =
54-
Math.min(7 - offset + numberOfDaysInMonth, numberOfDaysInMonth) -
55-
(7 - offset + 22) +
56-
1;
49+
const fifthStart = 7 - offset + 22;
50+
const fifthEnd = Math.min(7 - offset + numberOfDaysInMonth, numberOfDaysInMonth);
51+
const fifthRowDays = fifthStart <= numberOfDaysInMonth
52+
? [`${fifthStart}`, `${fifthEnd}`]
53+
: [];
54+
const numberOfDaysInLastRow = fifthEnd - fifthStart + 1;
5755
const calendarDayColor = defaultDayColor || 65;
5856
const firstRow =
5957
firstRowDays[0] === firstRowDays[1]

0 commit comments

Comments
 (0)