AlexMJ
AlexMJ

Reputation: 89

Patterns in Lua: unify different input date formats

I would like to unify all my Dates into this only format:

 "D+/M+/YYYY H+:N+"

I have this 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 that in Lua I can make something similar to this:

p= '%d+/%d+/%d%d%d%d %d%d:%d%d [AP]M'
if string.match('6/5/2013 12:06 PM',p) then print('ok') else print('not ok') end

I've planned to make an if-else anidation. But, as you can see, there are lot of possibilities in input formats. Could you please suggest to me an smartest solution?

For example, I have done this 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 ~= "" and inputDateShp1 ~= nil then
        trazas:write(os.date().." inputDateShp1 = "..inputDateShp1,"\n")

        --"D+/M+/YYYY"
        if string.match(inputDateShp1,esFormat0) then           
            trazas:write(os.date().." esFormat0 = "..esFormat0,"\n")
            InputFormatCSV=esInputFormatCSV0            


        --"D+/M+/YYYY H+:N+"
        elseif string.match(inputDateShp1,esFormat1) then           
            trazas:write(os.date().." esFormat1 = "..esFormat1,"\n")
            InputFormatCSV=esInputFormatCSV1            


        --"D+/M+/YYYY H+:N+:S+"
        elseif string.match(inputDateShp1,esFormat2) then           
            trazas:write(os.date().." esFormat2 = "..esFormat2,"\n")
            InputFormatCSV=esInputFormatCSV2            


        --"D+/M+/YYYY H+:N+ #PM"
        elseif string.match(inputDateShp1,esFormat3) then           
            trazas:write(os.date().." esFormat3 = "..esFormat3,"\n")
            InputFormatCSV=esInputFormatCSV3            


        --"D+/M+/YYYY H+:N+:S+ #PM"
        elseif string.match(inputDateShp1,esFormat4) then           
            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 ~= "" and fechaFinalAnio ~= nil then
        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!

Upvotes: 0

Views: 585

Answers (1)

Mud
Mud

Reputation: 29010

'%d+/%d+/%d%d%d%d %d%d:%d%d [AP]M'

This one is hardcoded to allow only / as a separator. You can use a set to allow / or - 1(i.e. [/-]). You can use '*' to indicate zero or more to accept optional parameters. For instance, this pattern will handle all your 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 are still details to work out. Just giving you the idea.

Upvotes: 1

Related Questions