• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Tieske / date / 6100809670

06 Sep 2023 06:09PM UTC coverage: 83.296%. Remained the same
6100809670

push

github

Tieske
release 2.2.1

1 of 1 new or added line in 1 file covered. (100.0%)

374 of 449 relevant lines covered (83.3%)

229.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.3
/src/date.lua
1
---------------------------------------------------------------------------------------
2
-- Module for date and time calculations
3
--
4
-- Version 2.2.1
5
-- Copyright (C) 2005-2006, by Jas Latrix (jastejada@yahoo.com)
6
-- Copyright (C) 2013-2021, by Thijs Schreijer
7
-- Licensed under MIT, http://opensource.org/licenses/MIT
8

9
--[[ CONSTANTS ]]--
10
  local HOURPERDAY  = 24
6✔
11
  local MINPERHOUR  = 60
6✔
12
  local MINPERDAY    = 1440  -- 24*60
6✔
13
  local SECPERMIN   = 60
6✔
14
  local SECPERHOUR  = 3600  -- 60*60
6✔
15
  local SECPERDAY   = 86400 -- 24*60*60
6✔
16
  local TICKSPERSEC = 1000000
6✔
17
  local TICKSPERDAY = 86400000000
6✔
18
  local TICKSPERHOUR = 3600000000
6✔
19
  local TICKSPERMIN = 60000000
6✔
20
  local DAYNUM_MAX =  365242500 -- Sat Jan 01 1000000 00:00:00
6✔
21
  local DAYNUM_MIN = -365242500 -- Mon Jan 01 1000000 BCE 00:00:00
6✔
22
  local DAYNUM_DEF =  0 -- Mon Jan 01 0001 00:00:00
6✔
23
  local _;
6✔
24
--[[ GLOBAL SETTINGS ]]--
25
  local centuryflip = 0 -- year >= centuryflip == 1900, < centuryflip == 2000
6✔
26
--[[ LOCAL ARE FASTER ]]--
27
  local type     = type
6✔
28
  local pairs    = pairs
6✔
29
  local error    = error
6✔
30
  local assert   = assert
6✔
31
  local tonumber = tonumber
6✔
32
  local tostring = tostring
6✔
33
  local string   = string
6✔
34
  local math     = math
6✔
35
  local os       = os
6✔
36
  local unpack   = unpack or table.unpack
6✔
37
  local setmetatable = setmetatable
6✔
38
  local getmetatable = getmetatable
6✔
39
--[[ EXTRA FUNCTIONS ]]--
40
  local fmt  = string.format
6✔
41
  local lwr  = string.lower
6✔
42
  local rep  = string.rep
6✔
43
  local len  = string.len  -- luacheck: ignore
6✔
44
  local sub  = string.sub
6✔
45
  local gsub = string.gsub
6✔
46
  local gmatch = string.gmatch or string.gfind
6✔
47
  local find = string.find
6✔
48
  local ostime = os.time
6✔
49
  local osdate = os.date
6✔
50
  local floor = math.floor
6✔
51
  local ceil  = math.ceil
6✔
52
  local abs   = math.abs
6✔
53
  -- removes the decimal part of a number
54
  local function fix(n) n = tonumber(n) return n and ((n > 0 and floor or ceil)(n)) end
3,522✔
55
  -- returns the modulo n % d;
56
  local function mod(n,d) return n - d*floor(n/d) end
5,382✔
57
  -- is `str` in string list `tbl`, `ml` is the minimun len
58
  local function inlist(str, tbl, ml, tn)
59
    local sl = len(str)
636✔
60
    if sl < (ml or 0) then return nil end
636✔
61
    str = lwr(str)
848✔
62
    for k, v in pairs(tbl) do
7,603✔
63
      if str == lwr(sub(v, 1, sl)) then
11,915✔
64
        if tn then tn[0] = k end
396✔
65
        return k
396✔
66
      end
67
    end
68
  end
69
  local function fnil() end
6✔
70
--[[ DATE FUNCTIONS ]]--
71
  local DATE_EPOCH -- to be set later
72
  local sl_weekdays = {
6✔
73
    [0]="Sunday",[1]="Monday",[2]="Tuesday",[3]="Wednesday",[4]="Thursday",[5]="Friday",[6]="Saturday",
4✔
74
    [7]="Sun",[8]="Mon",[9]="Tue",[10]="Wed",[11]="Thu",[12]="Fri",[13]="Sat",
4✔
75
  }
76
  local sl_meridian = {[-1]="AM", [1]="PM"}
6✔
77
  local sl_months = {
6✔
78
    [00]="January", [01]="February", [02]="March",
4✔
79
    [03]="April",   [04]="May",      [05]="June",
4✔
80
    [06]="July",    [07]="August",   [08]="September",
4✔
81
    [09]="October", [10]="November", [11]="December",
4✔
82
    [12]="Jan", [13]="Feb", [14]="Mar",
4✔
83
    [15]="Apr", [16]="May", [17]="Jun",
4✔
84
    [18]="Jul", [19]="Aug", [20]="Sep",
4✔
85
    [21]="Oct", [22]="Nov", [23]="Dec",
4✔
86
  }
87
  -- added the '.2'  to avoid collision, use `fix` to remove
88
  local sl_timezone = {
6✔
89
    [000]="utc",    [0.2]="gmt",
4✔
90
    [300]="est",    [240]="edt",
4✔
91
    [360]="cst",  [300.2]="cdt",
4✔
92
    [420]="mst",  [360.2]="mdt",
4✔
93
    [480]="pst",  [420.2]="pdt",
4✔
94
  }
95
  -- set the day fraction resolution
96
  local function setticks(t)
97
    TICKSPERSEC = t;
98
    TICKSPERDAY = SECPERDAY*TICKSPERSEC
×
99
    TICKSPERHOUR= SECPERHOUR*TICKSPERSEC
×
100
    TICKSPERMIN = SECPERMIN*TICKSPERSEC
×
101
  end
102
  -- is year y leap year?
103
  local function isleapyear(y) -- y must be int!
104
    return (mod(y, 4) == 0 and (mod(y, 100) ~= 0 or mod(y, 400) == 0))
42✔
105
  end
106
  -- day since year 0
107
  local function dayfromyear(y) -- y must be int!
108
    return 365*y + floor(y/4) - floor(y/100) + floor(y/400)
1,626✔
109
  end
110
  -- day number from date, month is zero base
111
  local function makedaynum(y, m, d)
112
    local mm = mod(mod(m,12) + 10, 12)
1,640✔
113
    return dayfromyear(y + floor(m/12) - floor(mm/10)) + floor((mm*306 + 5)/10) + d - 307
1,640✔
114
    --local yy = y + floor(m/12) - floor(mm/10)
115
    --return dayfromyear(yy) + floor((mm*306 + 5)/10) + (d - 1)
116
  end
117
  -- date from day number, month is zero base
118
  local function breakdaynum(g)
119
    local g = g + 306
378✔
120
    local y = floor((10000*g + 14780)/3652425)
378✔
121
    local d = g - dayfromyear(y)
504✔
122
    if d < 0 then y = y - 1; d = g - dayfromyear(y) end
378✔
123
    local mi = floor((100*d + 52)/3060)
378✔
124
    return (floor((mi + 2)/12) + y), mod(mi + 2,12), (d - floor((mi*306 + 5)/10) + 1)
504✔
125
  end
126
  --[[ for floats or int32 Lua Number data type
127
  local function breakdaynum2(g)
128
    local g, n = g + 306;
129
    local n400 = floor(g/DI400Y);n = mod(g,DI400Y);
130
    local n100 = floor(n/DI100Y);n = mod(n,DI100Y);
131
    local n004 = floor(n/DI4Y);   n = mod(n,DI4Y);
132
    local n001 = floor(n/365);   n = mod(n,365);
133
    local y = (n400*400) + (n100*100) + (n004*4) + n001  - ((n001 == 4 or n100 == 4) and 1 or 0)
134
    local d = g - dayfromyear(y)
135
    local mi = floor((100*d + 52)/3060)
136
    return (floor((mi + 2)/12) + y), mod(mi + 2,12), (d - floor((mi*306 + 5)/10) + 1)
137
  end
138
  ]]
139
  -- day fraction from time
140
  local function makedayfrc(h,r,s,t)
141
    return ((h*60 + r)*60 + s)*TICKSPERSEC + t
1,104✔
142
  end
143
  -- time from day fraction
144
  local function breakdayfrc(df)
145
    return
×
146
      mod(floor(df/TICKSPERHOUR),HOURPERDAY),
30✔
147
      mod(floor(df/TICKSPERMIN ),MINPERHOUR),
30✔
148
      mod(floor(df/TICKSPERSEC ),SECPERMIN),
30✔
149
      mod(df,TICKSPERSEC)
40✔
150
  end
151
  -- weekday sunday = 0, monday = 1 ...
152
  local function weekday(dn) return mod(dn + 1, 7) end
240✔
153
  -- yearday 0 based ...
154
  local function yearday(dn)
155
     return dn - dayfromyear((breakdaynum(dn))-1)
30✔
156
  end
157
  -- parse v as a month
158
  local function getmontharg(v)
159
    local m = tonumber(v);
612✔
160
    return (m and fix(m - 1)) or inlist(tostring(v) or "", sl_months, 2)
816✔
161
  end
162
  -- get daynum of isoweek one of year y
163
  local function isow1(y)
164
    local f = makedaynum(y, 0, 4) -- get the date for the 4-Jan of year `y`
138✔
165
    local d = weekday(f)
138✔
166
    d = d == 0 and 7 or d -- get the ISO day number, 1 == Monday, 7 == Sunday
138✔
167
    return f + (1 - d)
138✔
168
  end
169
  local function isowy(dn)
170
    local w1;
25✔
171
    local y = (breakdaynum(dn))
30✔
172
    if dn >= makedaynum(y, 11, 29) then
40✔
173
      w1 = isow1(y + 1);
8✔
174
      if dn < w1 then
6✔
175
        w1 = isow1(y);
176
      else
177
          y = y + 1;
6✔
178
      end
179
    else
180
      w1 = isow1(y);
32✔
181
      if dn < w1 then
24✔
182
        w1 = isow1(y-1)
8✔
183
        y = y - 1
6✔
184
      end
185
    end
186
    return floor((dn-w1)/7)+1, y
30✔
187
  end
188
  local function isoy(dn)
189
    local y = (breakdaynum(dn))
24✔
190
    return y + (((dn >= makedaynum(y, 11, 29)) and (dn >= isow1(y + 1))) and 1 or (dn < isow1(y) and -1 or 0))
40✔
191
  end
192
  local function makedaynum_isoywd(y,w,d)
193
    return isow1(y) + 7*w + d - 8 -- simplified: isow1(y) + ((w-1)*7) + (d-1)
104✔
194
  end
195
--[[ THE DATE MODULE ]]--
196
  local fmtstr  = "%x %X";
6✔
197
--#if not DATE_OBJECT_AFX then
198
  local date = {}
6✔
199
  setmetatable(date, date)
6✔
200
-- Version:  VMMMRRRR; V-Major, M-Minor, R-Revision;  e.g. 5.45.321 == 50450321
201
  do
202
    local major = 2
6✔
203
    local minor = 2
6✔
204
    local revision = 1
6✔
205
    date.version = major * 10000000 + minor * 10000 + revision
6✔
206
  end
207
--#end -- not DATE_OBJECT_AFX
208
--[[ THE DATE OBJECT ]]--
209
  local dobj = {}
6✔
210
  dobj.__index = dobj
6✔
211
  dobj.__metatable = dobj
6✔
212
  -- shout invalid arg
213
  local function date_error_arg() return error("invalid argument(s)",0) end
6✔
214
  -- create new date object
215
  local function date_new(dn, df)
216
    return setmetatable({daynum=dn, dayfrc=df}, dobj)
1,272✔
217
  end
218

219
--#if not NO_LOCAL_TIME_SUPPORT then
220
  -- magic year table
221
  local date_epoch, yt;
6✔
222
  local function getequivyear(y)
223
    assert(not yt)
×
224
    yt = {}
×
225
    local de = date_epoch:copy()
×
226
    local dw, dy
227
    for _ = 0, 3000 do
×
228
      de:setyear(de:getyear() + 1, 1, 1)
×
229
      dy = de:getyear()
×
230
      dw = de:getweekday() * (isleapyear(dy) and  -1 or 1)
×
231
      if not yt[dw] then yt[dw] = dy end  --print(de)
×
232
      if yt[1] and yt[2] and yt[3] and yt[4] and yt[5] and yt[6] and yt[7] and yt[-1] and yt[-2] and yt[-3] and yt[-4] and yt[-5] and yt[-6] and yt[-7] then
×
233
        getequivyear = function(y)  return yt[ (weekday(makedaynum(y, 0, 1)) + 1) * (isleapyear(y) and  -1 or 1) ]  end
×
234
        return getequivyear(y)
×
235
      end
236
    end
237
  end
238
  -- TimeValue from date and time
239
  local function totv(y,m,d,h,r,s)
240
    return (makedaynum(y, m, d) - DATE_EPOCH) * SECPERDAY  + ((h*60 + r)*60 + s)
×
241
  end
242
  -- TimeValue from TimeTable
243
  local function tmtotv(tm)
244
    return tm and totv(tm.year, tm.month - 1, tm.day, tm.hour, tm.min, tm.sec)
×
245
  end
246
  -- Returns the bias in seconds of utc time daynum and dayfrc
247
  local function getbiasutc2(self)
248
    local y,m,d = breakdaynum(self.daynum)
×
249
    local h,r,s = breakdayfrc(self.dayfrc)
×
250
    local tvu = totv(y,m,d,h,r,s) -- get the utc TimeValue of date and time
×
251
    local tml = osdate("*t", tvu) -- get the local TimeTable of tvu
×
252
    if (not tml) or (tml.year > (y+1) or tml.year < (y-1)) then -- failed try the magic
×
253
      y = getequivyear(y)
×
254
      tvu = totv(y,m,d,h,r,s)
×
255
      tml = osdate("*t", tvu)
×
256
    end
257
    local tvl = tmtotv(tml)
×
258
    if tvu and tvl then
×
259
      return tvu - tvl, tvu, tvl
×
260
    else
261
      return error("failed to get bias from utc time")
×
262
    end
263
  end
264
  -- Returns the bias in seconds of local time daynum and dayfrc
265
  local function getbiasloc2(daynum, dayfrc)
266
    local tvu
267
    -- extract date and time
268
    local y,m,d = breakdaynum(daynum)
×
269
    local h,r,s = breakdayfrc(dayfrc)
×
270
    -- get equivalent TimeTable
271
    local tml = {year=y, month=m+1, day=d, hour=h, min=r, sec=s}
×
272
    -- get equivalent TimeValue
273
    local tvl = tmtotv(tml)
×
274

275
    local function chkutc()
276
      tml.isdst =  nil; local tvug = ostime(tml) if tvug and (tvl == tmtotv(osdate("*t", tvug))) then tvu = tvug return end
×
277
      tml.isdst = true; local tvud = ostime(tml) if tvud and (tvl == tmtotv(osdate("*t", tvud))) then tvu = tvud return end
×
278
      tvu = tvud or tvug
×
279
    end
280
    chkutc()
×
281
    if not tvu then
×
282
      tml.year = getequivyear(y)
×
283
      tvl = tmtotv(tml)
×
284
      chkutc()
×
285
    end
286
    return ((tvu and tvl) and (tvu - tvl)) or error("failed to get bias from local time"), tvu, tvl
×
287
  end
288
--#end -- not NO_LOCAL_TIME_SUPPORT
289

290
--#if not DATE_OBJECT_AFX then
291
  -- the date parser
292
  local strwalker = {} -- ^Lua regular expression is not as powerful as Perl$
6✔
293
  strwalker.__index = strwalker
6✔
294
  local function newstrwalker(s)return setmetatable({s=s, i=1, e=1, c=len(s)}, strwalker) end
630✔
295
  function strwalker:aimchr() return "\n" .. self.s .. "\n" .. rep(".",self.e-1) .. "^" end
6✔
296
  function strwalker:finish() return self.i > self.c  end
1,248✔
297
  function strwalker:back()  self.i = self.e return self  end
24✔
298
  function strwalker:restart() self.i, self.e = 1, 1 return self end
300✔
299
  function strwalker:match(s)  return (find(self.s, s, self.i)) end
6✔
300
  function strwalker:__call(s, f)-- print("strwalker:__call "..s..self:aimchr())
6✔
301
    local is, ie; is, ie, self[1], self[2], self[3], self[4], self[5] = find(self.s, s, self.i)
5,484✔
302
    if is then self.e, self.i = self.i, 1+ie; if f then f(unpack(self)) end return self end
5,872✔
303
  end
304
  local function date_parse(str)
305
    local y,m,d, h,r,s,  z,  w,u, j,  e,  x,c,  dn,df
306
    local sw = newstrwalker(gsub(gsub(str, "(%b())", ""),"^(%s*)","")) -- remove comment, trim leading space
468✔
307
    --local function error_out() print(y,m,d,h,r,s) end
308
    local function error_dup(q) --[[error_out()]] error("duplicate value: " .. (q or "") .. sw:aimchr()) end
468✔
309
    local function error_syn(q) --[[error_out()]] error("syntax error: " .. (q or "") .. sw:aimchr()) end
468✔
310
    local function error_inv(q) --[[error_out()]] error("invalid date: " .. (q or "") .. sw:aimchr()) end
468✔
311
    local function sety(q) y = y and error_dup() or tonumber(q); end
930✔
312
    local function setm(q) m = (m or w or j) and error_dup(m or w or j) or tonumber(q) end
762✔
313
    local function setd(q) d = d and error_dup() or tonumber(q) end
834✔
314
    local function seth(q) h = h and error_dup() or tonumber(q) end
678✔
315
    local function setr(q) r = r and error_dup() or tonumber(q) end
678✔
316
    local function sets(q) s = s and error_dup() or tonumber(q) end
660✔
317
    local function adds(q) s = s + tonumber("."..string.sub(q,2,-1)) end
628✔
318
    local function setj(q) j = (m or w or j) and error_dup() or tonumber(q); end
498✔
319
    local function setz(q) z = (z ~= 0 and z) and error_dup() or q end
588✔
320
    local function setzn(zs,zn) zn = tonumber(zn); setz( ((zn<24) and (zn*60) or (mod(zn,100) + floor(zn/100) * 60))*( zs=='+' and -1 or 1) ) end
512✔
321
    local function setzc(zs,zh,zm) setz( ((tonumber(zh)*60) + tonumber(zm))*( zs=='+' and -1 or 1) ) end
524✔
322

323
    if not (sw("^(%d%d%d%d)",sety) and (sw("^(%-?)(%d%d)%1(%d%d)",function(_,a,b) setm(tonumber(a)); setd(tonumber(b)) end) or sw("^(%-?)[Ww](%d%d)%1(%d?)",function(_,a,b) w, u = tonumber(a), tonumber(b or 1) end) or sw("^%-?(%d%d%d)",setj) or sw("^%-?(%d%d)",function(a) setm(a);setd(1) end))
938✔
324
    and ((sw("^%s*[Tt]?(%d%d):?",seth) and sw("^(%d%d):?",setr) and sw("^(%d%d)",sets) and sw("^([,%.]%d+)",adds) and sw("%s*([+-])(%d%d):?(%d%d)%s*$",setzc))
376✔
325
      or sw:finish() or (sw"^%s*$" or sw"^%s*[Zz]%s*$" or sw("^%s-([%+%-])(%d%d):?(%d%d)%s*$",setzc) or sw("^%s*([%+%-])(%d%d)%s*$",setzn))
266✔
326
      )  )
3✔
327
    then --print(y,m,d,h,r,s,z,w,u,j)
328
    sw:restart(); y,m,d,h,r,s,z,w,u,j = nil,nil,nil,nil,nil,nil,nil,nil,nil,nil
392✔
329
      repeat -- print(sw:aimchr())
330
        if sw("^[tT:]?%s*(%d%d?):",seth) then --print("$Time")
1,448✔
331
          _ = sw("^%s*(%d%d?)",setr) and sw("^%s*:%s*(%d%d?)",sets) and sw("^([,%.]%d+)",adds)
144✔
332
        elseif sw("^(%d+)[/\\%s,-]?%s*") then --print("$Digits")
1,352✔
333
          x, c = tonumber(sw[1]), len(sw[1])
840✔
334
          if (x >= 70) or (m and d and (not y)) or (c > 3) then
630✔
335
            sety( x + ((x >= 100 or c>3) and 0 or x<centuryflip and 2000 or 1900) )
368✔
336
          else
337
            if m then setd(x) else m = x end
444✔
338
          end
339
        elseif sw("^(%a+)[/\\%s,-]?%s*") then --print("$Words")
512✔
340
          x = sw[1]
354✔
341
          if inlist(x, sl_months,   2, sw) then
472✔
342
            if m and (not d) and (not y) then d, m = m, false end
198✔
343
            setm(mod(sw[0],12)+1)
330✔
344
          elseif inlist(x, sl_timezone, 2, sw) then
208✔
345
            c = fix(sw[0]) -- ignore gmt and utc
128✔
346
            if c ~= 0 then setz(c) end
112✔
347
          elseif not inlist(x, sl_weekdays, 2, sw) then
80✔
348
            sw:back()
18✔
349
            -- am pm bce ad ce bc
350
            if sw("^([bB])%s*(%.?)%s*[Cc]%s*(%2)%s*[Ee]%s*(%2)%s*") or sw("^([bB])%s*(%.?)%s*[Cc]%s*(%2)%s*") then
30✔
351
              e = e and error_dup() or -1
12✔
352
            elseif sw("^([aA])%s*(%.?)%s*[Dd]%s*(%2)%s*") or sw("^([cC])%s*(%.?)%s*[Ee]%s*(%2)%s*") then
10✔
353
              e = e and error_dup() or 1
×
354
            elseif sw("^([PApa])%s*(%.?)%s*[Mm]?%s*(%2)%s*") then
8✔
355
              x = lwr(sw[1]) -- there should be hour and it must be correct
8✔
356
              if (not h) or (h > 12) or (h < 0) then return error_inv() end
6✔
357
              if x == 'a' and h == 12 then h = 0 end -- am
6✔
358
              if x == 'p' and h ~= 12 then h = h + 12 end -- pm
6✔
359
            else error_syn() end
×
360
          end
361
        elseif not(sw("^([+-])(%d%d?):(%d%d)",setzc) or sw("^([+-])(%d+)",setzn) or sw("^[Zz]%s*$")) then -- sw{"([+-])",{"(%d%d?):(%d%d)","(%d+)"}}
48✔
362
          error_syn("?")
×
363
        end
364
      sw("^%s*")  until sw:finish()
1,810✔
365
    --else print("$Iso(Date|Time|Zone)")
366
    end
367
    -- if date is given, it must be complete year, month & day
368
    if (not y and not h) or ((m and not d) or (d and not m)) or ((m and w) or (m and j) or (j and w)) then return error_inv("!") end
468✔
369
    -- fix month
370
    if m then m = m - 1 end
468✔
371
    -- fix year if we are on BCE
372
    if e and e < 0 and y > 0 then y = 1 - y end
468✔
373
    --  create date object
374
    dn = (y and ((w and makedaynum_isoywd(y,w,u)) or (j and makedaynum(y, 0, j)) or makedaynum(y, m, d))) or DAYNUM_DEF
618✔
375
    df = makedayfrc(h or 0, r or 0, s or 0, 0) + ((z or 0)*TICKSPERMIN)
624✔
376
    --print("Zone",h,r,s,z,m,d,y,df)
377
    return date_new(dn, df) -- no need to :normalize();
468✔
378
  end
379
  local function date_fromtable(v)
380
    local y, m, d = fix(v.year), getmontharg(v.month), fix(v.day)
30✔
381
    local h, r, s, t = tonumber(v.hour), tonumber(v.min), tonumber(v.sec), tonumber(v.ticks)
18✔
382
    -- atleast there is time or complete date
383
    if (y or m or d) and (not(y and m and d)) then return error("incomplete table")  end
18✔
384
    return (y or h or r or s or t) and date_new(y and makedaynum(y, m, d) or DAYNUM_DEF, makedayfrc(h or 0, r or 0, s or 0, t or 0))
34✔
385
  end
386
  local tmap = {
6✔
387
    ['number'] = function(v) return date_epoch:copy():addseconds(v) end,
22✔
388
    ['string'] = function(v) return date_parse(v) end,
474✔
389
    ['boolean']= function(v) return date_fromtable(osdate(v and "!*t" or "*t")) end,
12✔
390
    ['table']  = function(v) local ref = getmetatable(v) == dobj; return ref and v or date_fromtable(v), ref end
262✔
391
  }
392
  local function date_getdobj(v)
393
    local o, r = (tmap[type(v)] or fnil)(v);
738✔
394
    return (o and o:normalize() or error"invalid date time value"), r -- if r is true then o is a reference to a date obj
984✔
395
  end
396
--#end -- not DATE_OBJECT_AFX
397
  local function date_from(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
398
    local y, m, d = fix(arg1), getmontharg(arg2), fix(arg3)
980✔
399
    local h, r, s, t = tonumber(arg4 or 0), tonumber(arg5 or 0), tonumber(arg6 or 0), tonumber(arg7 or 0)
588✔
400
    if y and m and d and h and r and s and t then
588✔
401
      return date_new(makedaynum(y, m, d), makedayfrc(h, r, s, t)):normalize()
1,176✔
402
    else
403
      return date_error_arg()
×
404
    end
405
  end
406

407
 --[[ THE DATE OBJECT METHODS ]]--
408
  function dobj:normalize()
6✔
409
    local dn, df = fix(self.daynum), self.dayfrc
2,096✔
410
    self.daynum, self.dayfrc = dn + floor(df/TICKSPERDAY), mod(df, TICKSPERDAY)
2,096✔
411
    return (dn >= DAYNUM_MIN and dn <= DAYNUM_MAX) and self or error("date beyond imposed limits:"..self)
1,572✔
412
  end
413

414
  function dobj:getdate()  local y, m, d = breakdaynum(self.daynum) return y, m+1, d end
14✔
415
  function dobj:gettime()  return breakdayfrc(self.dayfrc) end
12✔
416

417
  function dobj:getclockhour() local h = self:gethours() return h>12 and mod(h,12) or (h==0 and 12 or h) end
32✔
418

419
  function dobj:getyearday() return yearday(self.daynum) + 1 end
14✔
420
  function dobj:getweekday() return weekday(self.daynum) + 1 end   -- in lua weekday is sunday = 1, monday = 2 ...
14✔
421

422
  function dobj:getyear()   local r,_,_ = breakdaynum(self.daynum)  return r end
126✔
423
  function dobj:getmonth() local _,r,_ = breakdaynum(self.daynum)  return r+1 end-- in lua month is 1 base
134✔
424
  function dobj:getday()   local _,_,r = breakdaynum(self.daynum)  return r end
118✔
425
  function dobj:gethours()  return mod(floor(self.dayfrc/TICKSPERHOUR),HOURPERDAY) end
90✔
426
  function dobj:getminutes()  return mod(floor(self.dayfrc/TICKSPERMIN), MINPERHOUR) end
72✔
427
  function dobj:getseconds()  return mod(floor(self.dayfrc/TICKSPERSEC ),SECPERMIN)  end
42✔
428
  function dobj:getfracsec()  return mod(floor(self.dayfrc/TICKSPERSEC ),SECPERMIN)+(mod(self.dayfrc,TICKSPERSEC)/TICKSPERSEC) end
76✔
429
  function dobj:getticks(u)  local x = mod(self.dayfrc,TICKSPERSEC) return u and ((x*u)/TICKSPERSEC) or x  end
14✔
430

431
  function dobj:getweeknumber(wdb)
6✔
432
    local wd, yd = weekday(self.daynum), yearday(self.daynum)
16✔
433
    if wdb then
12✔
434
      wdb = tonumber(wdb)
6✔
435
      if wdb then
6✔
436
        wd = mod(wd-(wdb-1),7)-- shift the week day base
8✔
437
      else
438
        return date_error_arg()
×
439
      end
440
    end
441
    return (yd < wd and 0) or (floor(yd/7) + ((mod(yd, 7)>=wd) and 1 or 0))
16✔
442
  end
443

444
  function dobj:getisoweekday() return mod(weekday(self.daynum)-1,7)+1 end   -- sunday = 7, monday = 1 ...
66✔
445
  function dobj:getisoweeknumber() return (isowy(self.daynum)) end
22✔
446
  function dobj:getisoyear() return isoy(self.daynum)  end
30✔
447
  function dobj:getisodate()
6✔
448
    local w, y = isowy(self.daynum)
18✔
449
    return y, w, self:getisoweekday()
24✔
450
  end
451
  function dobj:setisoyear(y, w, d)
6✔
452
    local cy, cw, cd = self:getisodate()
18✔
453
    if y then cy = fix(tonumber(y))end
20✔
454
    if w then cw = fix(tonumber(w))end
22✔
455
    if d then cd = fix(tonumber(d))end
22✔
456
    if cy and cw and cd then
18✔
457
      self.daynum = makedaynum_isoywd(cy, cw, cd)
24✔
458
      return self:normalize()
18✔
459
    else
460
      return date_error_arg()
×
461
    end
462
  end
463

464
  function dobj:setisoweekday(d)    return self:setisoyear(nil, nil, d) end
12✔
465
  function dobj:setisoweeknumber(w,d)  return self:setisoyear(nil, w, d)  end
12✔
466

467
  function dobj:setyear(y, m, d)
6✔
468
    local cy, cm, cd = breakdaynum(self.daynum)
18✔
469
    if y then cy = fix(tonumber(y))end
20✔
470
    if m then cm = getmontharg(m)  end
20✔
471
    if d then cd = fix(tonumber(d))end
20✔
472
    if cy and cm and cd then
18✔
473
      self.daynum  = makedaynum(cy, cm, cd)
24✔
474
      return self:normalize()
18✔
475
    else
476
      return date_error_arg()
×
477
    end
478
  end
479

480
  function dobj:setmonth(m, d)return self:setyear(nil, m, d) end
12✔
481
  function dobj:setday(d)    return self:setyear(nil, nil, d) end
12✔
482

483
  function dobj:sethours(h, m, s, t)
6✔
484
    local ch,cm,cs,ck = breakdayfrc(self.dayfrc)
24✔
485
    ch, cm, cs, ck = tonumber(h or ch), tonumber(m or cm), tonumber(s or cs), tonumber(t or ck)
24✔
486
    if ch and cm and cs and ck then
24✔
487
      self.dayfrc = makedayfrc(ch, cm, cs, ck)
32✔
488
      return self:normalize()
24✔
489
    else
490
      return date_error_arg()
×
491
    end
492
  end
493

494
  function dobj:setminutes(m,s,t)  return self:sethours(nil,   m,   s, t) end
12✔
495
  function dobj:setseconds(s, t)  return self:sethours(nil, nil,   s, t) end
12✔
496
  function dobj:setticks(t)    return self:sethours(nil, nil, nil, t) end
12✔
497

498
  function dobj:spanticks()  return (self.daynum*TICKSPERDAY + self.dayfrc) end
18✔
499
  function dobj:spanseconds()  return (self.daynum*TICKSPERDAY + self.dayfrc)/TICKSPERSEC  end
24✔
500
  function dobj:spanminutes()  return (self.daynum*TICKSPERDAY + self.dayfrc)/TICKSPERMIN  end
18✔
501
  function dobj:spanhours()  return (self.daynum*TICKSPERDAY + self.dayfrc)/TICKSPERHOUR end
18✔
502
  function dobj:spandays()  return (self.daynum*TICKSPERDAY + self.dayfrc)/TICKSPERDAY  end
24✔
503

504
  function dobj:addyears(y, m, d)
6✔
505
    local cy, cm, cd = breakdaynum(self.daynum)
12✔
506
    if y then y = fix(tonumber(y))else y = 0 end
14✔
507
    if m then m = fix(tonumber(m))else m = 0 end
14✔
508
    if d then d = fix(tonumber(d))else d = 0 end
12✔
509
    if y and m and d then
12✔
510
      self.daynum  = makedaynum(cy+y, cm+m, cd+d)
16✔
511
      return self:normalize()
12✔
512
    else
513
      return date_error_arg()
×
514
    end
515
  end
516

517
  function dobj:addmonths(m, d)
6✔
518
    return self:addyears(nil, m, d)
6✔
519
  end
520

521
  local function dobj_adddayfrc(self,n,pt,pd)
522
    n = tonumber(n)
84✔
523
    if n then
84✔
524
      local x = floor(n/pd);
84✔
525
      self.daynum = self.daynum + x;
84✔
526
      self.dayfrc = self.dayfrc + (n-x*pd)*pt;
84✔
527
      return self:normalize()
84✔
528
    else
529
      return date_error_arg()
×
530
    end
531
  end
532
  function dobj:adddays(n)  return dobj_adddayfrc(self,n,TICKSPERDAY,1) end
42✔
533
  function dobj:addhours(n)  return dobj_adddayfrc(self,n,TICKSPERHOUR,HOURPERDAY) end
12✔
534
  function dobj:addminutes(n)  return dobj_adddayfrc(self,n,TICKSPERMIN,MINPERDAY)  end
12✔
535
  function dobj:addseconds(n)  return dobj_adddayfrc(self,n,TICKSPERSEC,SECPERDAY)  end
36✔
536
  function dobj:addticks(n)  return dobj_adddayfrc(self,n,1,TICKSPERDAY) end
12✔
537
  local tvspec = {
6✔
538
    -- Abbreviated weekday name (Sun)
539
    ['%a']=function(self) return sl_weekdays[weekday(self.daynum) + 7] end,
54✔
540
    -- Full weekday name (Sunday)
541
    ['%A']=function(self) return sl_weekdays[weekday(self.daynum)] end,
14✔
542
    -- Abbreviated month name (Dec)
543
    ['%b']=function(self) return sl_months[self:getmonth() - 1 + 12] end,
70✔
544
    -- Full month name (December)
545
    ['%B']=function(self) return sl_months[self:getmonth() - 1] end,
14✔
546
    -- Year/100 (19, 20, 30)
547
    ['%C']=function(self) return fmt("%.2d", fix(self:getyear()/100)) end,
26✔
548
    -- The day of the month as a number (range 1 - 31)
549
    ['%d']=function(self) return fmt("%.2d", self:getday())  end,
94✔
550
    -- year for ISO 8601 week, from 00 (79)
551
    ['%g']=function(self) return fmt("%.2d", mod(self:getisoyear() ,100)) end,
6✔
552
    -- year for ISO 8601 week, from 0000 (1979)
553
    ['%G']=function(self) return fmt("%.4d", self:getisoyear()) end,
6✔
554
    -- same as %b
555
    ['%h']=function(self) return self:fmt0("%b") end,
12✔
556
    -- hour of the 24-hour day, from 00 (06)
557
    ['%H']=function(self) return fmt("%.2d", self:gethours()) end,
62✔
558
    -- The  hour as a number using a 12-hour clock (01 - 12)
559
    ['%I']=function(self) return fmt("%.2d", self:getclockhour()) end,
22✔
560
    -- The day of the year as a number (001 - 366)
561
    ['%j']=function(self) return fmt("%.3d", self:getyearday())  end,
6✔
562
    -- Month of the year, from 01 to 12
563
    ['%m']=function(self) return fmt("%.2d", self:getmonth())  end,
38✔
564
    -- Minutes after the hour 55
565
    ['%M']=function(self) return fmt("%.2d", self:getminutes())end,
78✔
566
    -- AM/PM indicator (AM)
567
    ['%p']=function(self) return sl_meridian[self:gethours() > 11 and 1 or -1] end, --AM/PM indicator (AM)
22✔
568
    -- The second as a number (59, 20 , 01)
569
    ['%S']=function(self) return fmt("%.2d", self:getseconds())  end,
38✔
570
    -- ISO 8601 day of the week, to 7 for Sunday (7, 1)
571
    ['%u']=function(self) return self:getisoweekday() end,
6✔
572
    -- Sunday week of the year, from 00 (48)
573
    ['%U']=function(self) return fmt("%.2d", self:getweeknumber()) end,
6✔
574
    -- ISO 8601 week of the year, from 01 (48)
575
    ['%V']=function(self) return fmt("%.2d", self:getisoweeknumber()) end,
6✔
576
    -- The day of the week as a decimal, Sunday being 0
577
    ['%w']=function(self) return self:getweekday() - 1 end,
6✔
578
    -- Monday week of the year, from 00 (48)
579
    ['%W']=function(self) return fmt("%.2d", self:getweeknumber(2)) end,
6✔
580
    -- The year as a number without a century (range 00 to 99)
581
    ['%y']=function(self) return fmt("%.2d", mod(self:getyear() ,100)) end,
26✔
582
    -- Year with century (2000, 1914, 0325, 0001)
583
    ['%Y']=function(self) return fmt("%.4d", self:getyear()) end,
22✔
584
    -- Time zone offset, the date object is assumed local time (+1000, -0230)
585
    ['%z']=function(self) local b = -self:getbias(); local x = abs(b); return fmt("%s%.4d", b < 0 and "-" or "+", fix(x/60)*100 + floor(mod(x,60))) end,
6✔
586
    -- Time zone name, the date object is assumed local time
587
    ['%Z']=function(self) return self:gettzname() end,
6✔
588
    -- Misc --
589
    -- Year, if year is in BCE, prints the BCE Year representation, otherwise result is similar to "%Y" (1 BCE, 40 BCE)
590
    ['%\b']=function(self) local x = self:getyear() return fmt("%.4d%s", x>0 and x or (-x+1), x>0 and "" or " BCE") end,
46✔
591
    -- Seconds including fraction (59.998, 01.123)
592
    ['%\f']=function(self) local x = self:getfracsec() return fmt("%s%.9f",x >= 10 and "" or "0", x) end,
54✔
593
    -- percent character %
594
    ['%%']=function(self) return "%" end,
6✔
595
    -- Group Spec --
596
    -- 12-hour time, from 01:00:00 AM (06:55:15 AM); same as "%I:%M:%S %p"
597
    ['%r']=function(self) return self:fmt0("%I:%M:%S %p") end,
12✔
598
    -- hour:minute, from 01:00 (06:55); same as "%I:%M"
599
    ['%R']=function(self) return self:fmt0("%I:%M")  end,
6✔
600
    -- 24-hour time, from 00:00:00 (06:55:15); same as "%H:%M:%S"
601
    ['%T']=function(self) return self:fmt0("%H:%M:%S") end,
12✔
602
    -- month/day/year from 01/01/00 (12/02/79); same as "%m/%d/%y"
603
    ['%D']=function(self) return self:fmt0("%m/%d/%y") end,
12✔
604
    -- year-month-day (1979-12-02); same as "%Y-%m-%d"
605
    ['%F']=function(self) return self:fmt0("%Y-%m-%d") end,
12✔
606
    -- The preferred date and time representation;  same as "%x %X"
607
    ['%c']=function(self) return self:fmt0("%x %X") end,
6✔
608
    -- The preferred date representation, same as "%a %b %d %\b"
609
    ['%x']=function(self) return self:fmt0("%a %b %d %\b") end,
36✔
610
    -- The preferred time representation, same as "%H:%M:%\f"
611
    ['%X']=function(self) return self:fmt0("%H:%M:%\f") end,
36✔
612
    -- GroupSpec --
613
    -- Iso format, same as "%Y-%m-%dT%T"
614
    ['${iso}'] = function(self) return self:fmt0("%Y-%m-%dT%T") end,
6✔
615
    -- http format, same as "%a, %d %b %Y %T GMT"
616
    ['${http}'] = function(self) return self:fmt0("%a, %d %b %Y %T GMT") end,
6✔
617
    -- ctime format, same as "%a %b %d %T GMT %Y"
618
    ['${ctime}'] = function(self) return self:fmt0("%a %b %d %T GMT %Y") end,
6✔
619
    -- RFC850 format, same as "%A, %d-%b-%y %T GMT"
620
    ['${rfc850}'] = function(self) return self:fmt0("%A, %d-%b-%y %T GMT") end,
6✔
621
    -- RFC1123 format, same as "%a, %d %b %Y %T GMT"
622
    ['${rfc1123}'] = function(self) return self:fmt0("%a, %d %b %Y %T GMT") end,
6✔
623
    -- asctime format, same as "%a %b %d %T %Y"
624
    ['${asctime}'] = function(self) return self:fmt0("%a %b %d %T %Y") end,
6✔
625
  }
626
  function dobj:fmt0(str) return (gsub(str, "%%[%a%%\b\f]", function(x) local f = tvspec[x];return (f and f(self)) or x end)) end
906✔
627
  function dobj:fmt(str)
6✔
628
    str = str or self.fmtstr or fmtstr
114✔
629
    return self:fmt0((gmatch(str, "${%w+}")) and (gsub(str, "${%w+}", function(x)local f=tvspec[x];return (f and f(self)) or x end)) or str)
114✔
630
  end
631

632
  function dobj.__lt(a, b) if (a.daynum == b.daynum) then return (a.dayfrc < b.dayfrc) else return (a.daynum < b.daynum) end end
36✔
633
  function dobj.__le(a, b) if (a.daynum == b.daynum) then return (a.dayfrc <= b.dayfrc) else return (a.daynum <= b.daynum) end end
54✔
634
  function dobj.__eq(a, b)return (a.daynum == b.daynum) and (a.dayfrc == b.dayfrc) end
486✔
635
  function dobj.__sub(a,b)
8✔
636
    local d1, d2 = date_getdobj(a), date_getdobj(b)
96✔
637
    local d0 = d1 and d2 and date_new(d1.daynum - d2.daynum, d1.dayfrc - d2.dayfrc)
72✔
638
    return d0 and d0:normalize()
96✔
639
  end
640
  function dobj.__add(a,b)
8✔
641
    local d1, d2 = date_getdobj(a), date_getdobj(b)
24✔
642
    local d0 = d1 and d2 and date_new(d1.daynum + d2.daynum, d1.dayfrc + d2.dayfrc)
18✔
643
    return d0 and d0:normalize()
24✔
644
  end
645
  function dobj.__concat(a, b) return tostring(a) .. tostring(b) end
38✔
646
  function dobj:__tostring() return self:fmt() end
36✔
647

648
  function dobj:copy() return date_new(self.daynum, self.dayfrc) end
96✔
649

650
--[[ THE LOCAL DATE OBJECT METHODS ]]--
651
  function dobj:tolocal()
8✔
652
    local dn,df = self.daynum, self.dayfrc
×
653
    local bias  = getbiasutc2(self)
×
654
    if bias then
×
655
      -- utc = local + bias; local = utc - bias
656
      self.daynum = dn
×
657
      self.dayfrc = df - bias*TICKSPERSEC
×
658
      return self:normalize()
×
659
    else
660
      return nil
×
661
    end
662
  end
663

664
  function dobj:toutc()
8✔
665
    local dn,df = self.daynum, self.dayfrc
×
666
    local bias  = getbiasloc2(dn, df)
×
667
    if bias then
×
668
      -- utc = local + bias;
669
      self.daynum = dn
×
670
      self.dayfrc = df + bias*TICKSPERSEC
×
671
      return self:normalize()
×
672
    else
673
      return nil
×
674
    end
675
  end
676

677
  function dobj:getbias()  return (getbiasloc2(self.daynum, self.dayfrc))/SECPERMIN end
6✔
678

679
  function dobj:gettzname()
8✔
680
    local _, tvu, _ = getbiasloc2(self.daynum, self.dayfrc)
×
681
    return tvu and osdate("%Z",tvu) or ""
×
682
  end
683

684
--#if not DATE_OBJECT_AFX then
685
  function date.time(h, r, s, t)
6✔
686
    h, r, s, t = tonumber(h or 0), tonumber(r or 0), tonumber(s or 0), tonumber(t or 0)
×
687
    if h and r and s and t then
×
688
       return date_new(DAYNUM_DEF, makedayfrc(h, r, s, t))
×
689
    else
690
      return date_error_arg()
×
691
    end
692
  end
693

694
  function date:__call(arg1, ...)
6✔
695
    local arg_count = select("#", ...) + (arg1 == nil and 0 or 1)
1,146✔
696
    if arg_count  > 1 then return (date_from(arg1, ...))
1,342✔
697
    elseif arg_count == 0 then return (date_getdobj(false))
560✔
698
    else local o, r = date_getdobj(arg1);  return r and o:copy() or o end
756✔
699
  end
700

701
  date.diff = dobj.__sub
6✔
702

703
  function date.isleapyear(v)
8✔
704
    local y = fix(v);
24✔
705
    if not y then
24✔
706
      y = date_getdobj(v)
×
707
      y = y and y:getyear()
×
708
    end
709
    return isleapyear(y+0)
24✔
710
  end
711

712
  function date.epoch() return date_epoch:copy()  end
6✔
713

714
  function date.isodate(y,w,d) return date_new(makedaynum_isoywd(y + 0, w and (w+0) or 1, d and (d+0) or 1), 0)  end
22✔
715
  function date.setcenturyflip(y)
8✔
716
    if y ~= floor(y) or y < 0 or y > 100 then date_error_arg() end
18✔
717
    centuryflip = y
18✔
718
  end
719
  function date.getcenturyflip() return centuryflip end
12✔
720

721
-- Internal functions
722
  function date.fmt(str) if str then fmtstr = str end; return fmtstr end
6✔
723
  function date.daynummin(n)  DAYNUM_MIN = (n and n < DAYNUM_MAX) and n or DAYNUM_MIN  return n and DAYNUM_MIN or date_new(DAYNUM_MIN, 0):normalize()end
6✔
724
  function date.daynummax(n)  DAYNUM_MAX = (n and n > DAYNUM_MIN) and n or DAYNUM_MAX return n and DAYNUM_MAX or date_new(DAYNUM_MAX, 0):normalize()end
6✔
725
  function date.ticks(t) if t then setticks(t) end return TICKSPERSEC  end
12✔
726
--#end -- not DATE_OBJECT_AFX
727

728
  local tm = osdate("!*t", 0);
6✔
729
  if tm then
6✔
730
    date_epoch = date_new(makedaynum(tm.year, tm.month - 1, tm.day), makedayfrc(tm.hour, tm.min, tm.sec, 0))
12✔
731
    -- the distance from our epoch to os epoch in daynum
732
    DATE_EPOCH = date_epoch and date_epoch:spandays()
8✔
733
  else -- error will be raise only if called!
734
    date_epoch = setmetatable({},{__index = function() error("failed to get the epoch date") end})
×
735
  end
736

737
--#if not DATE_OBJECT_AFX then
738
return date
6✔
739
--#else
740
--$return date_from
741
--#end
742

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc