-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcalendar.d.lua
More file actions
294 lines (280 loc) · 6.62 KB
/
Copy pathcalendar.d.lua
File metadata and controls
294 lines (280 loc) · 6.62 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---@meta _
---Weekday number (1-7) where 1 is Monday and 7 is Sunday.
---@alias mods.calendarWeekday
---| 1 Monday
---| 2 Tuesday
---| 3 Wednesday
---| 4 Thursday
---| 5 Friday
---| 6 Saturday
---| 7 Sunday
---Month number (1-12) where 1 is January and 12 is December.
---@alias mods.calendarMonth
---| 1 January
---| 2 February
---| 3 March
---| 4 April
---| 5 May
---| 6 June
---| 7 July
---| 8 August
---| 9 September
---| 10 October
---| 11 November
---| 12 December
---Day of the month (1-31).
---@alias mods.calendarMonthDay
---| 1 1st day of the month
---| 2 2nd day of the month
---| 3 3rd day of the month
---| 4 4th day of the month
---| 5 5th day of the month
---| 6 6th day of the month
---| 7 7th day of the month
---| 8 8th day of the month
---| 9 9th day of the month
---| 10 10th day of the month
---| 11 11th day of the month
---| 12 12th day of the month
---| 13 13th day of the month
---| 14 14th day of the month
---| 15 15th day of the month
---| 16 16th day of the month
---| 17 17th day of the month
---| 18 18th day of the month
---| 19 19th day of the month
---| 20 20th day of the month
---| 21 21st day of the month
---| 22 22nd day of the month
---| 23 23rd day of the month
---| 24 24th day of the month
---| 25 25th day of the month
---| 26 26th day of the month
---| 27 27th day of the month
---| 28 28th day of the month
---| 29 29th day of the month
---| 30 30th day of the month
---| 31 31st day of the month
---
---Calculate weekday alignments, leap years, and month lengths.
---
---> [!WARNING]
--->
---> This module is still under development and may not be stable.
---> The API is incomplete and may change in future versions.
---
---## Usage
---
---```lua
---local mods = require "mods"
---local cal = mods.calendar
---
---print(cal.weekday(2026, 3, 26)) --> 4
---```
---
---@class mods.calendar
---@field JANUARY 1
---@field FEBRUARY 2
---@field MARCH 3
---@field APRIL 4
---@field MAY 5
---@field JUNE 6
---@field JULY 7
---@field AUGUST 8
---@field SEPTEMBER 9
---@field OCTOBER 10
---@field NOVEMBER 11
---@field DECEMBER 12
---
---@field MONDAY 1
---@field TUESDAY 2
---@field WEDNESDAY 3
---@field THURSDAY 4
---@field FRIDAY 5
---@field SATURDAY 6
---@field SUNDAY 7
---
---Month names indexed from `1` to `12`.
---
---```lua
---print(cal.months[1]) --> January
---print(cal.months[12]) --> December
---```
---@field months mods.List<string>
---
---Weekday names indexed from `1` (Monday) to `7` (Sunday).
---
---```lua
---print(cal.days[1]) --> Monday
---print(cal.days[7]) --> Sunday
---```
---@field days mods.List<string>
---
---The default first weekday field.
---
---```lua
---print(cal.firstweekday) --> 1
---cal.firstweekday = cal.SUNDAY
---print(cal.firstweekday) --> 7
---```
---
---> [!NOTE]
--->
---> Reading or writing this property is equivalent to calling
---> `getfirstweekday()` or `setfirstweekday()`.
---
---@field firstweekday mods.calendarWeekday
local M = {}
---
---Return the default first weekday.
---
---```lua
---print(cal.getfirstweekday()) --> 1
---```
---
---> [!NOTE]
--->
---> This returns the same value as `cal.firstweekday`.
---
---@return mods.calendarWeekday firstWeekday
---@nodiscard
function M.getfirstweekday() end
---
---Set the default first weekday.
---
---```lua
---cal.setfirstweekday(cal.SUNDAY)
---```
---
---> [!NOTE]
--->
---> This updates the same value as
---> `cal.firstweekday = ...`.
---
---@param firstWeekday mods.calendarWeekday
function M.setfirstweekday(firstWeekday) end
---
---Iterate weekday numbers for one full week.
---
---```lua
---local weekdays = {}
---for day in cal.weekdays() do
--- weekdays[#weekdays + 1] = day
---end
---print(table.concat(weekdays, ", ")) --> "1, 2, 3, 4, 5, 6, 7"
---```
---
---@section Iterators
---@param firstWeekday? mods.calendarWeekday
---@return fun():mods.calendarWeekday iter
---@nodiscard
function M.weekdays(firstWeekday) end
---
---Return `true` for leap years.
---
---```lua
---print(cal.isleap(2024)) --> true
---```
---
---@section Calendar Calculations
---@param year integer
---@return boolean isLeap
---@nodiscard
function M.isleap(year) end
---
---Return the number of leap years from `y1` up to but not including `y2`.
---
---```lua
---print(cal.leapdays(2000, 2025)) --> 7
---```
---
---@section Calendar Calculations
---@param y1 integer
---@param y2 integer
---@return integer count
---@nodiscard
function M.leapdays(y1, y2) end
---
---Return weekday number where Monday is `1` and Sunday is `7`.
---
---```lua
---print(cal.weekday(2026, 3, 26)) --> 4
---```
---
---@section Calendar Calculations
---@param year integer
---@param month mods.calendarMonth
---@param day mods.calendarMonthDay
---@return mods.calendarWeekday weekday
---@nodiscard
function M.weekday(year, month, day) end
---
---Return the first weekday and number of days for a month.
---
---```lua
---local wday, ndays = cal.monthrange(2026, 2)
---print(wday, ndays) --> 7 28
---```
---
---@section Calendar Calculations
---@param year integer
---@param month integer
---@return mods.calendarWeekday weekday
---@return integer ndays
---@nodiscard
function M.monthrange(year, month) end
---Iterate `(year, month, day, weekday)` tuples for a full calendar grid.
---
---```lua
---local List = mods.list
---local cal = mods.calendar
---local str = mods.str
---
---local header = cal.weekheader(2)
---local lines = List({
--- str.center(("%s %d"):format(cal.months[cal.FEBRUARY], 2026), #header),
--- header,
---})
---
---local cells = List()
---for _, m, d, _ in cal.monthdays(2026, cal.FEBRUARY) do
--- cells:append(m == cal.FEBRUARY and ("%2d"):format(d) or " ")
--- if #cells == 7 then
--- lines:append(cells:join(" "))
--- cells = List()
--- end
---end
---
---print(lines:join("\n"))
----- February 2026
----- Mo Tu We Th Fr Sa Su
----- 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
---```
---
---@section Iterators
---@param year integer
---@param month mods.calendarMonth
---@param firstWeekday? mods.calendarWeekday
---@return fun():year:integer,month:mods.calendarMonth,day:mods.calendarMonthDay,weekday:mods.calendarWeekday iter
---@nodiscard
function M.monthdays(year, month, firstWeekday) end
---
---Return the formatted weekday header string.
---
---```lua
---print(cal.weekheader(1, cal.SUNDAY)) --> "S M T W T F S"
---print(cal.weekheader(2, cal.SUNDAY)) --> "Su Mo Tu We Th Fr Sa"
---print(cal.weekheader(3, cal.SUNDAY)) --> "Sun Mon Tue Wed Thu Fri Sat"
---```
---
---@section Formatting
---@param width? integer
---@param firstWeekday? mods.calendarWeekday
---@return string header
---@nodiscard
function M.weekheader(width, firstWeekday) end
return M