aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandre alves garzia <andre@andregarzia.com>2017-02-11 15:16:22 -0200
committerandre alves garzia <andre@andregarzia.com>2017-02-11 15:16:22 -0200
commit96858816371f16ee914145e7814488405845aca8 (patch)
tree71b8c3aab77339ff36ecbd0b4a331b9183d092be
parent083ca1a87531f34e8ed7245a099e483c3706947d (diff)
downloadvalua-96858816371f16ee914145e7814488405845aca8.tar.gz
valua-96858816371f16ee914145e7814488405845aca8.tar.bz2
valua-96858816371f16ee914145e7814488405845aca8.zip
enhancement: datetime support
-rw-r--r--valua.lua48
1 files changed, 48 insertions, 0 deletions
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