summaryrefslogtreecommitdiff
path: root/lualibs-os.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <phg42.2a@gmail.com>2013-09-21 13:31:56 +0200
committerPhilipp Gesang <phg42.2a@gmail.com>2013-09-21 13:31:56 +0200
commitd96d015887e6fcbe1ca8defcec44cb794421785d (patch)
tree7e636bed27130e0b4846a7d41dfcf50e12a83492 /lualibs-os.lua
parent08563510e0f4328d9e893f17fc1fd37bac00dbc7 (diff)
downloadlualibs-d96d015887e6fcbe1ca8defcec44cb794421785d.tar.gz
sync with Context as of 2013-09-21
Diffstat (limited to 'lualibs-os.lua')
-rw-r--r--lualibs-os.lua57
1 files changed, 57 insertions, 0 deletions
diff --git a/lualibs-os.lua b/lualibs-os.lua
index 8bfcf78..7f3fd7c 100644
--- a/lualibs-os.lua
+++ b/lualibs-os.lua
@@ -492,3 +492,60 @@ end
-- print(os.which("inkscape"))
-- print(os.which("gs.exe"))
-- print(os.which("ps2pdf"))
+
+-- These are moved from core-con.lua (as I needed them elsewhere).
+
+local function isleapyear(year)
+ return (year % 400 == 0) or ((year % 100 ~= 0) and (year % 4 == 0))
+end
+
+os.isleapyear = isleapyear
+
+-- nicer:
+--
+-- local days = {
+-- [false] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
+-- [true] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+-- }
+--
+-- local function nofdays(year,month)
+-- return days[isleapyear(year)][month]
+-- return month == 2 and isleapyear(year) and 29 or days[month]
+-- end
+--
+-- more efficient:
+
+local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
+
+local function nofdays(year,month)
+ if not month then
+ return isleapyear(year) and 365 or 364
+ else
+ return month == 2 and isleapyear(year) and 29 or days[month]
+ end
+end
+
+os.nofdays = nofdays
+
+function os.weekday(day,month,year)
+ return date("%w",time { year = year, month = month, day = day }) + 1
+end
+
+function os.validdate(year,month,day)
+ -- we assume that all three values are set
+ -- year is always ok, even if lua has a 1970 time limit
+ if month < 1 then
+ month = 1
+ elseif month > 12 then
+ month = 12
+ end
+ if day < 1 then
+ day = 1
+ else
+ local max = nofdays(year,month)
+ if day > max then
+ day = max
+ end
+ end
+ return year, month, day
+end