aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEtiene Dalcol <Etiene@users.noreply.github.com>2017-09-20 17:35:47 +0100
committerGitHub <noreply@github.com>2017-09-20 17:35:47 +0100
commit9ee17b7134b88c01e7efe33a982c4b5b8234b86a (patch)
tree7fe50fc765ce1cf62a0f5383dc440c7013cee2db
parent083ca1a87531f34e8ed7245a099e483c3706947d (diff)
parentc494bfb8c510d9a8341b1a3e4b38748b4f437f12 (diff)
downloadvalua-9ee17b7134b88c01e7efe33a982c4b5b8234b86a.tar.gz
valua-9ee17b7134b88c01e7efe33a982c4b5b8234b86a.tar.bz2
valua-9ee17b7134b88c01e7efe33a982c4b5b8234b86a.zip
Merge pull request #14 from amora-labs/b-enhancement-datetime-support
Enhancement datetime support
-rw-r--r--valua-test.lua6
-rw-r--r--valua.lua48
2 files changed, 52 insertions, 2 deletions
diff --git a/valua-test.lua b/valua-test.lua
index 4554382..5f75fdd 100644
--- a/valua-test.lua
+++ b/valua-test.lua
@@ -34,7 +34,8 @@ local test_values = {
{},
{3,46},
"<script>alert('boohoo@email.com XSS');</script>",
- "test-123_maria.2@newdomain.wow.movie"
+ "test-123_maria.2@newdomain.wow.movie",
+ "10/06/1980 10:32:10"
}
local tests = {
@@ -59,7 +60,8 @@ local tests = {
{v:new().string(),{14,false,1,true}},
{v:new().string().alnum(),{6,false}},
{v:new().contains(" "),{2,false,1,true}},
- {v:new().no_white(),{1,false,2,true}}
+ {v:new().no_white(),{1,false,2,true}},
+ {v:new().datetime(),{19,true,9,false}}
}
for n,t in ipairs(tests) do
diff --git a/valua.lua b/valua.lua
index d5eafa3..cec04fb 100644
--- a/valua.lua
+++ b/valua.lua
@@ -172,6 +172,54 @@ function valua._date(value,format)
end
--
+-- Datetime
+
+-- Check for a UK date pattern dd/mm/yyyy hh:mi:ss, dd-mm-yyyy, dd.mm.yyyy
+-- or US pattern mm/dd/yyyy, mm-dd-yyyy, mm.dd.yyyy
+-- or ISO pattern yyyy/mm/dd, yyyy-mm-dd, yyyy.mm.dd
+-- Default is UK
+function valua._datetime(value,format)
+ local valid = true
+ if (match(value, "^%d+%p%d+%p%d%d%d%d %d%d%p%d%d%p%d%d$")) then
+ local d, m, y, hh, mm, ss
+ if format and format:lower() == 'us' then
+ m, d, y, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)")
+ elseif format and format:lower() == 'iso' then
+ y, m, d, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)")
+ else
+ d, m, y, hh, mm, ss = match(value, "(%d+)%p(%d+)%p(%d+) (%d%d)%p(%d%d)%p(%d%d)")
+ end
+ d, m, y, hh, mm, ss = tonumber(d), tonumber(m), tonumber(y), tonumber(hh), tonumber(mm), tonumber(ss)
+
+ local dm2 = d*m*m
+ if d>31 or m>12 or dm2==116 or dm2==120 or dm2==124 or dm2==496 or dm2==1116 or dm2==2511 or dm2==3751 then
+ -- invalid unless leap year
+ if not (dm2==116 and (y%400 == 0 or (y%100 ~= 0 and y%4 == 0))) then
+ valid = false
+ end
+ end
+
+ -- time validation
+ if not (hh >= 0 and hh <= 24) then
+ valid = false
+ end
+
+ if not (mm >= 0 and mm <= 60) then
+ valid = false
+ end
+
+ if not (ss >= 0 and ss <= 60) then
+ valid = false
+ end
+
+ else
+ valid = false
+ end
+ if not valid then return false, "is not a valid datetime" end
+ return true
+end
+--
+
-- Abstract
function valua._empty(value)
if not empty(value) then return false,"must be empty" end