regex - Patterns in Lua: unify different input date formats -


i unify dates format:

 "d+/m+/yyyy h+:n+" 

i have kind of inputs:

-- dd/mm/yyyy 24/09/1986 1/09/1986 01/9/1986  -- dd-mm-yyyy 24-09-1986 1-09-1986 01-9-1986   05/12/2012 0:00:00 09/24/2012 24/9/1986 00:01 24/9/1986 0:1 24/9/1986 03:01 p.m. 24/9/1986 3:01 p.m. 24/9/1986 0:1 a.m. 24/9/86 24/9/86 03:01 p.m. 24/9/86 3:1 a.m.  24/9/86 00:01 24/9/86 0:1 24-09-1986 12:12 24-09-1986 12:12 24-09-1986 12:12 24-09-1986 12:12 

i know in lua can make similar this:

p= '%d+/%d+/%d%d%d%d %d%d:%d%d [ap]m' if string.match('6/5/2013 12:06 pm',p) print('ok') else print('not ok') end 

i've planned make if-else anidation. but, can see, there lot of possibilities in input formats. please suggest me smartest solution?

for example, have done function:

    function opunifydateformat( config, section, document ) local trazas = assert(io.open("d:\\logsapp\\autonomy\\desarrollos\\scriptslua\\commonfunctions\\trazas_lua_opunifydateformat.log", "a"))     trazas:write(os.date().." **************************** opreferenciadrereference "..document:getfieldvalue("drereference").."\n")      --date origin--     --shrpnt2010--     local inputdateshp1 = document:getfieldvalue("sp_created")     local inputdateshp2 = document:getfieldvalue("sp_modified")     local inputdateshp3 = document:getfieldvalue("sp_published")     --dctm--     local inputdatedctm11 = document:getfieldvalue("fecaprob")     --dctm2--     local inputdatedctm21 = document:getfieldvalue("attr_fechadoc")     local inputdatedctm22 = document:getfieldvalue("r_modify_date")      local outputformatforanio="dd/mm/yyyy"     local outputformatforfechaconsulta="dd/mm/yyyy hh:mm"      --list of input formats--     --('(%d+)[/-](%d+)[/-](%d+)%s*(%d*):?(%d*):?(%d*)%s*([apap]?)')('%s/%s/%s %d:%d')     --spanish--     local esformat0='%d+/%d+/%d%d%d%d'     local esinputformatcsv0="d+/m+/yyyy"         local esformat1='%d+/%d+/%d%d%d%d %d+:%d+'     local esinputformatcsv1="d+/m+/yyyy h+:n+"       local esformat2='%d+/%d+/%d%d%d%d %d+:%d+:%d+'     local esinputformatcsv2="d+/m+/yyyy h+:n+:s+"     local esformat3='%d+/%d+/%d%d%d%d %d+:%d+ [ap]m'     local esinputformatcsv3="d+/m+/yyyy h+:n+ #pm"     local esformat4='%d+/%d+/%d%d%d%d %d+:%d+:%d+ [ap]m'     local esinputformatcsv4="d+/m+/yyyy h+:n+:s+ #pm"     --portuguese--     local ptformat0='%d+-%d+-%d%d%d%d'     local ptinputformatcsv0="d+-m+-yyyy"         local ptformat1='%d+-%d+-%d%d%d%d %d+:%d+'     local ptinputformatcsv1="d+-m+-yyyy h+:n+"       local ptformat2='%d+-%d+-%d%d%d%d %d+:%d+:%d+'     local ptinputformatcsv2="d+-m+-yyyy h+:n+:s+"     local ptformat3='%d+-%d+-%d%d%d%d %d+:%d+ [ap]m'     local ptinputformatcsv3="d+-m+-yyyy h+:n+ #pm"     local ptformat4='%d+-%d+-%d%d%d%d %d+:%d+:%d+ [ap]m'     local ptinputformatcsv4="d+-m+-yyyy h+:n+:s+ #pm"      --sp_created--       if inputdateshp1 ~= "" , inputdateshp1 ~= nil         trazas:write(os.date().." inputdateshp1 = "..inputdateshp1,"\n")          --"d+/m+/yyyy"         if string.match(inputdateshp1,esformat0)                        trazas:write(os.date().." esformat0 = "..esformat0,"\n")             inputformatcsv=esinputformatcsv0                       --"d+/m+/yyyy h+:n+"         elseif string.match(inputdateshp1,esformat1)                        trazas:write(os.date().." esformat1 = "..esformat1,"\n")             inputformatcsv=esinputformatcsv1                       --"d+/m+/yyyy h+:n+:s+"         elseif string.match(inputdateshp1,esformat2)                        trazas:write(os.date().." esformat2 = "..esformat2,"\n")             inputformatcsv=esinputformatcsv2                       --"d+/m+/yyyy h+:n+ #pm"         elseif string.match(inputdateshp1,esformat3)                        trazas:write(os.date().." esformat3 = "..esformat3,"\n")             inputformatcsv=esinputformatcsv3                       --"d+/m+/yyyy h+:n+:s+ #pm"         elseif string.match(inputdateshp1,esformat4)                        trazas:write(os.date().." esformat4 = "..esformat4,"\n")             inputformatcsv=esinputformatcsv4                                 end          end      trazas:write(os.date().." fin if sp_created \n")          trazas:write(os.date().." formato fecha es ok!!--> "..inputformatcsv,"\n")           fechafinalanio=convert_date_time(inputdateshp1,inputformatcsv,outputformatforanio,false)                 fechafinalfechaconsulta=convert_date_time(inputdateshp1,inputformatcsv,outputformatforfechaconsulta,false)           trazas:write(os.date().." fechafinalanio es --> "..fechafinalanio,"\n")          trazas:write(os.date().." fechafinalfechaconsulta es --> "..fechafinalfechaconsulta,"\n")         if fechafinalanio ~= "" , fechafinalanio ~= nil         cridolanio=startatchars(document, fechafinalanio, "/,3")         document:addfield("cridol_anio",cridolanio)          end     cridolfechaconsulta=convert_date_time(inputdateshp1,inputformatcsv,"epochseconds",false)     document:addfield("cridol_fecha_consulta",cridolfechaconsulta)                    trazas:write(os.date().." cridol_anio= "..cridolanio,"\n")     trazas:write(os.date().." cridol_fecha_consulta= "..cridolfechaconsulta,"\n")      trazas:write(os.date().." done \n")      -- flush y cerrar     trazas:flush()     trazas:close() end 

thanks!

'%d+/%d+/%d%d%d%d %d%d:%d%d [ap]m'

this 1 hardcoded allow / separator. can use set allow / or - 1(i.e. [/-]). can use '*' indicate zero or more accept optional parameters. instance, pattern handle examples:

local day, month, year, hour, minute, seconds, meridiem      = input:match('(%d+)[/-](%d+)[/-](%d+)%s*(%d*):?(%d*):?(%d*)%s*([apap]?)') ('%s/%s/%s %d:%d'):format(day, month, year, tonumber(hour) or 0, tonumber(minute) or 0)) 

there still details work out. giving idea.


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -