|
| 1 | +--- |
| 2 | +description: "Calendar and date helpers." |
| 3 | +--- |
| 4 | + |
| 5 | +# `calendar` |
| 6 | + |
| 7 | +Calendar and date helpers. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```lua |
| 12 | +cal = require "mods.calendar" |
| 13 | + |
| 14 | +print(cal.weekday(2026, 3, 26)) --> 4 |
| 15 | +``` |
| 16 | + |
| 17 | +## Functions |
| 18 | + |
| 19 | +| Function | Description | |
| 20 | +| ------------------------------------------------------ | --------------------------------- | |
| 21 | +| [`getfirstweekday()`](#fn-getfirstweekday) | Return the default first weekday. | |
| 22 | +| [`setfirstweekday(firstweekday)`](#fn-setfirstweekday) | Set the default first weekday. | |
| 23 | + |
| 24 | +**Calendar Calculations**: |
| 25 | + |
| 26 | +| Function | Description | |
| 27 | +| ------------------------------------------- | ----------------------------------------------------------------------- | |
| 28 | +| [`isleap(year)`](#fn-isleap) | Return `true` for leap years. | |
| 29 | +| [`leapdays(y1, y2)`](#fn-leapdays) | Return the number of leap years from `y1` up to but not including `y2`. | |
| 30 | +| [`monthrange(year, month)`](#fn-monthrange) | Return the first weekday and number of days for a month. | |
| 31 | +| [`weekday(year, month, day)`](#fn-weekday) | Return weekday number where Monday is `1` and Sunday is `7`. | |
| 32 | + |
| 33 | +**Formatting**: |
| 34 | + |
| 35 | +| Function | Description | |
| 36 | +| ----------------------------------------------------- | ------------------------------------------- | |
| 37 | +| [`weekheader(width?, firstweekday?)`](#fn-weekheader) | Return the formatted weekday header string. | |
| 38 | + |
| 39 | +**Iterators**: |
| 40 | + |
| 41 | +| Function | Description | |
| 42 | +| -------------------------------------------------------- | ---------------------------------------------------------------------- | |
| 43 | +| [`monthdays(year, month, firstweekday?)`](#fn-monthdays) | Iterate `(year, month, day, weekday)` tuples for a full calendar grid. | |
| 44 | +| [`weekdays(firstweekday?)`](#fn-weekdays) | Iterate weekday numbers for one full week. | |
| 45 | + |
| 46 | +<a id="fn-getfirstweekday"></a> |
| 47 | + |
| 48 | +### `getfirstweekday()` |
| 49 | + |
| 50 | +Return the default first weekday. |
| 51 | + |
| 52 | +**Return**: |
| 53 | + |
| 54 | +- `firstweekday` (`1|2|3|4|5|6|7`) |
| 55 | + |
| 56 | +**Example**: |
| 57 | + |
| 58 | +```lua |
| 59 | +print(cal.getfirstweekday()) --> 1 |
| 60 | +``` |
| 61 | + |
| 62 | +> [!NOTE] |
| 63 | +> |
| 64 | +> This returns the same value as `cal.firstweekday`. |
| 65 | +
|
| 66 | +<a id="fn-setfirstweekday"></a> |
| 67 | + |
| 68 | +### `setfirstweekday(firstweekday)` |
| 69 | + |
| 70 | +Set the default first weekday. |
| 71 | + |
| 72 | +**Parameters**: |
| 73 | + |
| 74 | +- `firstweekday` (`1|2|3|4|5|6|7`) |
| 75 | + |
| 76 | +**Example**: |
| 77 | + |
| 78 | +```lua |
| 79 | +cal.setfirstweekday(cal.SUNDAY) |
| 80 | +``` |
| 81 | + |
| 82 | +> [!NOTE] |
| 83 | +> |
| 84 | +> This updates the same value as `cal.firstweekday = ...`. |
| 85 | +
|
| 86 | +### Calendar Calculations |
| 87 | + |
| 88 | +<a id="fn-isleap"></a> |
| 89 | + |
| 90 | +#### `isleap(year)` |
| 91 | + |
| 92 | +Return `true` for leap years. |
| 93 | + |
| 94 | +**Parameters**: |
| 95 | + |
| 96 | +- `year` (`integer`) |
| 97 | + |
| 98 | +**Return**: |
| 99 | + |
| 100 | +- `isLeap` (`boolean`) |
| 101 | + |
| 102 | +**Example**: |
| 103 | + |
| 104 | +```lua |
| 105 | +print(cal.isleap(2024)) --> true |
| 106 | +``` |
| 107 | + |
| 108 | +<a id="fn-leapdays"></a> |
| 109 | + |
| 110 | +#### `leapdays(y1, y2)` |
| 111 | + |
| 112 | +Return the number of leap years from `y1` up to but not including `y2`. |
| 113 | + |
| 114 | +**Parameters**: |
| 115 | + |
| 116 | +- `y1` (`integer`) |
| 117 | +- `y2` (`integer`) |
| 118 | + |
| 119 | +**Return**: |
| 120 | + |
| 121 | +- `count` (`integer`) |
| 122 | + |
| 123 | +**Example**: |
| 124 | + |
| 125 | +```lua |
| 126 | +print(cal.leapdays(2000, 2025)) --> 7 |
| 127 | +``` |
| 128 | + |
| 129 | +<a id="fn-monthrange"></a> |
| 130 | + |
| 131 | +#### `monthrange(year, month)` |
| 132 | + |
| 133 | +Return the first weekday and number of days for a month. |
| 134 | + |
| 135 | +**Parameters**: |
| 136 | + |
| 137 | +- `year` (`integer`) |
| 138 | +- `month` (`integer`) |
| 139 | + |
| 140 | +**Return**: |
| 141 | + |
| 142 | +- `weekday` (`1|2|3|4|5|6|7`) |
| 143 | +- `ndays` (`integer`) |
| 144 | + |
| 145 | +**Example**: |
| 146 | + |
| 147 | +```lua |
| 148 | +wday, ndays = cal.monthrange(2026, 2) |
| 149 | +print(wday, ndays) --> 7 28 |
| 150 | +``` |
| 151 | + |
| 152 | +<a id="fn-weekday"></a> |
| 153 | + |
| 154 | +#### `weekday(year, month, day)` |
| 155 | + |
| 156 | +Return weekday number where Monday is `1` and Sunday is `7`. |
| 157 | + |
| 158 | +**Parameters**: |
| 159 | + |
| 160 | +- `year` (`integer`) |
| 161 | +- `month` (`1|2|3|4|5|6|7|8|9|10|11|12`) |
| 162 | +- `day` |
| 163 | + (`1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31`) |
| 164 | + |
| 165 | +**Return**: |
| 166 | + |
| 167 | +- `weekday` (`1|2|3|4|5|6|7`) |
| 168 | + |
| 169 | +**Example**: |
| 170 | + |
| 171 | +```lua |
| 172 | +print(cal.weekday(2026, 3, 26)) --> 4 |
| 173 | +``` |
| 174 | + |
| 175 | +### Formatting |
| 176 | + |
| 177 | +<a id="fn-weekheader"></a> |
| 178 | + |
| 179 | +#### `weekheader(width?, firstweekday?)` |
| 180 | + |
| 181 | +Return the formatted weekday header string. |
| 182 | + |
| 183 | +**Parameters**: |
| 184 | + |
| 185 | +- `width?` (`integer`) |
| 186 | +- `firstweekday?` (`1|2|3|4|5|6|7`) |
| 187 | + |
| 188 | +**Return**: |
| 189 | + |
| 190 | +- `header` (`string`) |
| 191 | + |
| 192 | +**Example**: |
| 193 | + |
| 194 | +```lua |
| 195 | +print(cal.weekheader(1, cal.SUNDAY)) --> "S M T W T F S" |
| 196 | +print(cal.weekheader(2, cal.SUNDAY)) --> "Su Mo Tu We Th Fr Sa" |
| 197 | +print(cal.weekheader(3, cal.SUNDAY)) --> "Sun Mon Tue Wed Thu Fri Sat" |
| 198 | +``` |
| 199 | + |
| 200 | +### Iterators |
| 201 | + |
| 202 | +<a id="fn-monthdays"></a> |
| 203 | + |
| 204 | +#### `monthdays(year, month, firstweekday?)` |
| 205 | + |
| 206 | +Iterate `(year, month, day, weekday)` tuples for a full calendar grid. |
| 207 | + |
| 208 | +**Parameters**: |
| 209 | + |
| 210 | +- `year` (`integer`) |
| 211 | +- `month` (`1|2|3|4|5|6|7|8|9|10|11|12`) |
| 212 | +- `firstweekday?` (`1|2|3|4|5|6|7`) |
| 213 | + |
| 214 | +**Return**: |
| 215 | + |
| 216 | +- `iter` |
| 217 | + (`fun():year:integer,month:modsCalendarMonth,day:modsCalendarMonthday,weekday:modsCalendarWeekday`) |
| 218 | + |
| 219 | +**Example**: |
| 220 | + |
| 221 | +```lua |
| 222 | +local mods = require "mods" |
| 223 | + |
| 224 | +local List = mods.List |
| 225 | +local cal = mods.calendar |
| 226 | +local str = mods.str |
| 227 | + |
| 228 | +local header = cal.weekheader(2) |
| 229 | +local lines = List({ |
| 230 | + str.center(("%s %d"):format(cal.months[cal.FEBRUARY], 2026), #header), |
| 231 | + header, |
| 232 | +}) |
| 233 | + |
| 234 | +local cells = List() |
| 235 | +for _, m, d, _ in cal.monthdays(2026, cal.FEBRUARY) do |
| 236 | + cells:append(m == cal.FEBRUARY and ("%2d"):format(d) or " ") |
| 237 | + if #cells == 7 then |
| 238 | + lines:append(cells:join(" ")) |
| 239 | + cells = List() |
| 240 | + end |
| 241 | +end |
| 242 | + |
| 243 | +print(lines:join("\n")) |
| 244 | +-- February 2026 |
| 245 | +-- Mo Tu We Th Fr Sa Su |
| 246 | +-- 1 |
| 247 | +-- 2 3 4 5 6 7 8 |
| 248 | +-- 9 10 11 12 13 14 15 |
| 249 | +-- 16 17 18 19 20 21 22 |
| 250 | +-- 23 24 25 26 27 28 |
| 251 | +``` |
| 252 | + |
| 253 | +<a id="fn-weekdays"></a> |
| 254 | + |
| 255 | +#### `weekdays(firstweekday?)` |
| 256 | + |
| 257 | +Iterate weekday numbers for one full week. |
| 258 | + |
| 259 | +**Parameters**: |
| 260 | + |
| 261 | +- `firstweekday?` (`1|2|3|4|5|6|7`) |
| 262 | + |
| 263 | +**Return**: |
| 264 | + |
| 265 | +- `iter` (`fun():modsCalendarWeekday`) |
| 266 | + |
| 267 | +**Example**: |
| 268 | + |
| 269 | +```lua |
| 270 | +local weekdays = {} |
| 271 | +for day in cal.weekdays() do |
| 272 | + weekdays[#weekdays + 1] = day |
| 273 | +end |
| 274 | +print(table.concat(weekdays, ", ")) --> "1, 2, 3, 4, 5, 6, 7" |
| 275 | +``` |
| 276 | + |
| 277 | +## Fields |
| 278 | + |
| 279 | +<a id="days"></a> |
| 280 | + |
| 281 | +### `days` (`mods.List<string>`) |
| 282 | + |
| 283 | +Weekday names indexed from `1` (Monday) to `7` (Sunday). |
| 284 | + |
| 285 | +```lua |
| 286 | +print(cal.days[1]) --> Monday |
| 287 | +print(cal.days[7]) --> Sunday |
| 288 | +``` |
| 289 | + |
| 290 | +<a id="firstweekday"></a> |
| 291 | + |
| 292 | +### `firstweekday` (`1|2|3|4|5|6|7`) |
| 293 | + |
| 294 | +The default first weekday field. |
| 295 | + |
| 296 | +```lua |
| 297 | +print(cal.firstweekday) --> 1 |
| 298 | +cal.firstweekday = cal.SUNDAY |
| 299 | +print(cal.firstweekday) --> 7 |
| 300 | +``` |
| 301 | + |
| 302 | +> [!NOTE] |
| 303 | +> |
| 304 | +> Reading or writing this property is equivalent to calling `getfirstweekday()` |
| 305 | +> or `setfirstweekday()`. |
| 306 | +
|
| 307 | +<a id="months"></a> |
| 308 | + |
| 309 | +### `months` (`mods.List<string>`) |
| 310 | + |
| 311 | +Month names indexed from `1` to `12`. |
| 312 | + |
| 313 | +```lua |
| 314 | +print(cal.months[1]) --> January |
| 315 | +print(cal.months[12]) --> December |
| 316 | +``` |
0 commit comments