-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
378 lines (306 loc) · 10.5 KB
/
init.lua
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
-- /////////////////////////////////////////////////////////////////////////////
-- 2018-07-22 Obsolete
-- /////////////////////////////////////////////////////////////////////////////
-- /////////////////////////////////////////////////////////////////////////////
-- // Purpose: Date class
-- // Author: Hernan Cano [jhernancanom@gmail.com]
-- // Created: 2016-03-21
-- // Copyright: (c) 2016 Hernan Cano
-- // License: MIT/X11
-- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
---
--- >> Class constructor:
---
--- object Date:new ( string sDateCons )
---
--- Arguments:
---
--- sDateCons This string is used as reference to create the object.
---
---
--- >> Class methods:
---
--- number setDay() Sets the day on this date
--- number getDay() Gets the day of this date
--- number getMonth() Gets the month of this date
--- number getYear() Gets the year of this date
--- number getLastMonthDay() Gets the last day of the month in this date
--- number getFirstMonthDay() Gets the first day of the month in this date
--- string toString() Convert Date to string
---
local DATE_TODAY = 0
local DATE_YESTERDAY = 1
local number_of_days = {
jan = 31, feb = 28, mar = 31, apr = 30,
may = 31, jun = 30, jul = 31, aug = 31,
sep = 30, oct = 31, nov = 30, dec = 31,
}
local months = {
'jan', 'feb', 'mar', 'apr',
'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec',
}
local Date = class 'Date'
local function ftzero (num)
local sCode = "00" .. tonumber( num )
sCode = sCode:sub(#sCode -1, #sCode )
return sCode
end
function Date:Date( fields )
fields = fields or DATE_TODAY
if ( fields == DATE_TODAY ) then
local t = os.date '*t'
self.Year = t.year
self.Month = ftzero(t.month)
self.Day = ftzero(t.day )
end
if type(fields) == 'table' then
self.Year = fields.Year
self.Month = fields.Month
self.Day = fields.Day
end
if type(fields) == 'string' then
local fields = fields:delim '-'
self.Year = fields[1]
self.Month = fields[2]
self.Day = fields[3]
end
end
function Date:__tostring( ... )
return ('%s-%s-%s'):format(self.Year, self:getMonth 'string', self:getDay 'string')
end
function Date:toString ( ... )
return self.__tostring( ... )
end
function Date:getYear ( ... )
return tonumber(self.Year)
end
function Date:getDay ( what )
if what == 'string' then
if tonumber(self.Day) < 10 then
return tostring ('0' .. tonumber(self.Day) )
else
return tostring (self.Day)
end
end
return tonumber(self.Day)
end
function Date:setDay ( nDay )
lide.core.base.isnumber(nDay)
self.Day = nDay
return self.Day == nDay
end
-- by default reutnrs a number
function Date:getMonth ( ... )
local fields = (...) or 'number'
-- fields debe ser un string...
if (fields == '%s') or (fields:lower() == 'name') then
return months[tonumber(self.Month)]
elseif (fields == '%d') or (fields:lower() == 'number') then
return tonumber(self.Month)
elseif fields == 'string' then
if tonumber(self.Month) < 10 then
return tostring('0' .. tonumber(self.Month))
else
return tostring(self.Month)
end
end
end
function Date:getFirstMonthDay()
return 1
end
function Date:getLastMonthDay()
--- Si la fecha es febrero y es año biciesto :)
if ( self:getYear() % 4 == 0 ) and (self:getMonth() == 2) then
return 29
else
return number_of_days[ months[self:getMonth()] ]
end
end
return Date
-- --birthday = Date { Year = '1996', Month = '02', Day = '29' }
-- today = Date ( DATE_TODAY )
-- --print( 'birthday', birthday )
-- print( 'today' , today )
--[[ Comento lo siguiente porque aun no esta implementado, al parecer necesita un modulo que no está presente para funcionar
No obstante es interesante que podamos poner estas funciones mas adelante
-- Returns the year, month, and day values from a dateObject.
function Date:getDate()
local y, m, d = self.getdate()
return y, m, d
end
-- Returns a string 1979-12-02 from a dateObject (ISO 8601, international)
function Date:getDateYMD() -- string
return self.fmt('%Y-%m-%d')
end
-- Returns the hours, minutes, seconds and ticks values from a dateObject.
function Date:getTime()
local h, m, s, t = self.gettime()
return h, m, s, t
end
-- Returns a string 11:59:59 AM (or PM) from a dateObject.
function Date:getTimeAMPM() -- string
return self.fmt('%I:%M:%S %p')
end
-- Returns the year, month, day, hours, minutes, seconds and ticks values from a dateObject.
function Date:getDateTime()
local y, M, d = self.getdate()
local h, m, s, t = self.gettime()
return y, M, d, h, m, s, t
end
-- Returns a string 1979-12-02 from a dateObject (ISO 8601, international)
function Date:getDateYMD() -- string
return self.fmt('%Y-%m-%dT%H:%M:%\f')
end
-- Returns the year value from a dateObject.
function Date:getYear()
return self.getyear()
end
-- Returns the month (01..12) value from a dateObject.
function Date:getMonth()
return self.getmonth()
end
-- Returns the day value (01..31) from a dateObject.
function Date:getDay()
return self.getday()
end
-- Returns the hours value (00..23) from a dateObject.
function Date:getHours()
return self.gethours()
end
-- Returns the minutes value (00..59) from a dateObject.
function Date:getMinutes()
return self.getminutes()
end
-- Returns the seconds value (00..59) from a dateObject.
function Date:getSeconds()
return self.getseconds()
end
-- Returns the ticks value from a dateObject.
function Date:getTicks()
return self.getticks()
end
-- Returns the name of the month from a dateObject; string.
function Date:getcMonth() -- string (c is character)
return self.fmt('%B') -- '%b'
end
-- Changes the year value in dateObject to the value tNum
function Date::setYear(tNum)
return self.:setyear(tNum)
end
-- Changes the month value (01..12) in dateObject to the value tNum
function Date::setMonth(tNum)
return self.:setmonth(tNum)
end
-- Changes the day value (01..31) in dateObject to the value tNum
function Date::setDay(tNum)
return self.:setday(tNum)
end
-- Changes the hour value (00..23) in dateObject to the value tNum
function Date::setHour(tNum)
return self.:sethour(tNum)
end
-- Changes the minute value (00..59) in dateObject to the value tNum
function Date::setMinute(tNum)
return self.:setminute(tNum)
end
-- Changes the seconds value (00..59) in dateObject to the value tNum
function Date::setSeconds(tNum)
return self.:setseconds(tNum)
end
-- Changes the ticks value in dateObject to the value tNum
function Date::setTicks(tNum)
return self.:setticks(tNum)
end
-- Returns the number day of the week from a dateObject (sunday=1, monday=2, ...saturday=7)
function Date:getDayOfWeek() -- number
return self.getweekday()
end
function Date:getDOW() -- number
return self.getweekday()
end
-- Returns the string full weekday name from a dateObject (sunday, monday, ...saturday)
function Date:getcDOW() -- string (c is character)
return self.fmt('%A') -- '%a'
end
function Date:getDayOfWeekString() -- string
return self.fmt('%A') -- '%a'
end
-- Returns the week (of the year) number value (01..53) in a dateObject.
function Date:getWeekOfYear()
return self.getweeknumber()
end
-- Returns the day of year (1-366) in a dateObject.
function Date:getDayOfYear()
return self.getyearday() -- number 1..366
-- return self.fmt('%j') -- string 001..366
end
-- Add years in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addYears(tNum)
return self.addyears(tNum)
end
-- Add months in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addMonths(tNum)
return self.addmonths(tNum)
end
function Date:goMonth(tNum)
return self.addmonths(tNum)
end
-- Add days in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addDays(tNum)
return self.adddays(tNum)
end
-- Add hours in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addHours(tNum)
return self.addhours(tNum)
end
-- Add minutes in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addMinutes(tNum)
return self.addminutes(tNum)
end
-- Add seconds in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addSeconds(tNum)
return self.addseconds(tNum)
end
-- Add ticks in dateObject (If the value tNum is negative, the returned dateObject will be earlier)
function Date:addTicks(tNum)
return self.addticks(tNum)
end
-- Check if the year of dateObject is a leapyear.
-- Returns true if the year of dateObject is a leapyear; false otherwise.
function Date:isLeapYear() -------------------- función de DATE y metamétodo
return date.isleapyear(self)
end
-- Returns how many days the dateObject has.
function Date:spanDays()
return self.spandays()
end
-- Returns how many hours the dateObject has.
function Date:spanHours()
return self.spanhours()
end
-- Returns how many minutes the dateObject has.
function Date:spanMinutes()
return self.spanminutes()
end
-- Returns how many seconds the dateObject has.
function Date:spanSeconds()
return self.spanseconds()
end
-- Returns a formatted version of dateObject (see full help of date.lua.chm)
function Date:format(tFmt) -- string
return self.fmt(tFmt)
end
function Date:fmt(tFmt) -- string
return self.fmt(tFmt)
end
-- Subtract the date and time value of two dateObject and returns the resulting dateObject
-- function Date:diff -------------------- función de DATE ???????????
-- a = date(2181, "aPr", 4, 6, 30, 30, 15000)
-- b = date(a):adddays(2)
-- c = b - a
-- assert(c:spandays() == (2))
-- get the days between two dates
-- d = date.diff("Jan 7 1563", date(1563, 1, 2))
-- assert(d:spandays()==5)
]]