Lua中的模式:统一不同的输入日期格式

时间:2013-07-25 16:45:31

标签: regex date datetime lua

我想将所有日期统一为这种格式:

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

我有这样的输入:

-- 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

我知道在Lua中我可以做类似的事情:

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

我打算做一个if-else anidation。但是,正如您所看到的,输入格式有很多可能性。你能告诉我一个最聪明的解决方案吗?

例如,我已经完成了这个功能:

    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

谢谢!

1 个答案:

答案 0 :(得分:1)

  

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

这个硬编码只允许/作为分隔符。您可以使用一组来允许/- 1(即[/-])。您可以使用'*'表示零或更多以接受可选参数。例如,此模式将处理您的所有示例:

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))

还有一些细节需要解决。只是给你一个想法。