2015-10-06 132 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