2015-10-06 125 views
1

我有一个文件(termino.txt)这是所有填写的格式如下:比较字符串 - Lua的

  1. 埋单
  2. 2015-08-30T13:22:53.108Z
  3. 去看医生
  4. 2015-09-30T13:22:53.108Z
  5. ....

所有偶数行是的形成RFC 3339时间戳。我需要的是将今天的日期与这些日期的文件进行比较,看看它们是否相同。我试试这个:

local function verifica(evt) 
    local nome= '' 
    local dia = '' 
    local turn = 1 
    local data = os.date("%x") 
    local file = io.open("termino.txt", "r") 
    while true do 
     nome = dia 
     line = file:read() 
     dia = line 

     if (turn %2 == 0) then 

      > Here I need to compare "data" with "dia" that will receive string with RFC 3339 timestamp format. 


     end 
    turn ++ 
    end 

end 

我需要帮助做出这个比较!由于

+1

'本地数据= os.date( “%F”)',比较前10个字符'如果数据==行:子(1,10)then' –

+0

我在巴西。所以我想当我运行本地数据= os.date(“%F”)时,格式将是“DD/MM/YYYY”,但在文件中格式为“YYYY-MM-DD”!我如何解决它? –

+0

使用'os.date'%Y-%m-%d“' –

回答

0
local dia = '2015-10-6T13:22:53.108Z' 
-- parse date info from the RFC 3339 timestamp 
local year, month, day = dia:match('(%d+)-(%d+)-(%d+)') 
-- get today's date from Lua, in table format 
local today = os.date('*t') 
-- compare 
if tonumber(year) == today.year 
    and tonumber(month) == today.month 
    and tonumber(day) == today.day then 
    -- the dates match 
end 
+2

你还需要'tonumber' /'tostring'调用。 – hjpotter92

+0

@Mud - 在Lua字符串中,“123”不等于123. –

+0

Doh。我如何在Lua中编程这么久,我不知道,我不知道。干杯。 – Mud