PDA

View Full Version : LUA Date, adding days!?



danward79
September 6th, 2004, 04:09 PM
Hi,

Does anyone know how to add to the date function in lua.

i.e. day1 = date(%d %b %Y) + 1

so if the date was the 6th september, it would result in the 7th september?

and if the date was the 31st, it would result in the 1st?

any help would be most appreciated.

Dan

:wink:

Promixis
September 6th, 2004, 05:11 PM
I don't know of any built in lua function to do this.

If you google for it, I am sure you will find a predone code for doing this (in another language). Perfect way for you to brush up your lua skills :D :D

VaioUserChris
September 6th, 2004, 10:11 PM
Hi Dan,

Here's a function I wrote to increase a date by any number of days you specify.


-- newdate function allows you to increase a date by a number of days you define
-- and still retain correct calendar formatting
-- Requires you to define the variables: curday, curmonth, curyear before function
-- Pass the dayfix within the parens when calling the function - i.e. newdate(2)
-- Function returns the variables: newday, newmonth, newyear
function newdate(dayfix)
newday = tonumber(curday)+dayfix
newmonth = tonumber(curmonth)
newyear = tonumber(curyear)
if newday > 31 then
if newmonth == 1 or newmonth == 3 or newmonth == 5 or newmonth == 7
or newmonth == 8 or newmonth == 10 or newmonth == 12 then
newday = newday - 31
newmonth = newmonth + 1
end
if newmonth > 12 then
newmonth = newmonth - 12
newyear = newyear + 1
end
end
if newday > 30 then
if newmonth == 4 or newmonth == 6 or newmonth == 9 or newmonth == 11 then
newday = newday - 30
newmonth = newmonth + 1
end
end
if newday > 28 and newmonth == 2
and newyear/4 ~= tonumber(format('%d',newyear/4)) then
newday = newday - 28
newmonth = newmonth + 1
end
if newday > 29 and newmonth == 2 then
newday = newday - 29
newmonth = newmonth + 1
end
newday = format('%02d',newday)
newmonth = format('%02d',newmonth)
newyear = format('%02d',newyear)
end

curmonth = "03"
curday = "28"
curyear = "08"
print ("Original date: "..curmonth.."/"..curday.."/"..curyear)
newdate(2)
print ("New date: "..newmonth.."/"..newday.."/"..newyear)


Chris

danward79
September 6th, 2004, 10:51 PM
Thanks Chris,

Will try that when I get back from work, it looks like the exact thing I need.

Welcome Back Mike! :wink:

Dan

danward79
September 7th, 2004, 12:46 PM
Thanks Chris, seems to work ok.

Until I change the line "newdate(2)" to "newdate(256)"!!!!

:wink:

I accidentally found that I only need to add five days at a time!

Dan

VaioUserChris
September 7th, 2004, 01:05 PM
Hi Dan,

I thought about that flaw after I posted it. Basically the script will work with date changes up to 28 days, after that it get's iffy depending on the month (definitely doesn't work past 31 days). I only needed to change it by 2 days so that wasn't a problem for me.

It could easily be modified for an infinite number of days by using a second function that calls newdate in 28 day increments. I'm not at my development computer now so I can't guarantee this script works but in concept it should:
-- for more than 28 day changes use this wrapper function
function 28daychange(bigdayfix)
cycle = tonumber(format('%d',bigdayfix/28))
for i = 1 to cycle do
newdate(28)
curday = newday
curmonth = newmonth
curyear = newyear
end
finalfix = bidgayfix - (cycle*28)
newdate(finalfix)
end This will still call newdate, but will do so in pieces it can logically handle.
Chris

danward79
September 7th, 2004, 01:23 PM
Umm, I may try that later.

I am actually only needing a few days at a time.

Thanks

Dan

danward79
September 7th, 2004, 03:07 PM
Hi

I have been working on a script to extract tidal info, from a csv file, which I will assemble anually, from a tidal prediction program. Appart from tidal info for my local beach, it also includes moon phase info.

The reason I need the tidal info is that the beach that I sail off in the UK works best at low tide. Having this info to hand will enable me to plan my day better. I intend to send it to Netremote from Girder, once girder has extracted it for me.

So there are two possibly three phases to this.

1. Extract tidal data, set up girder to send data to Netremote.
2. Set up CCF to display this data. (may add it to my weather pages)
maybe 3. Extract the moon phase info and have that as well, just for the hell of it.... :wink:

I have been working on the script and thought I would just post it here for interests or no interests sake! If you have any ideas, I would like to hear them, especially, if it makes the script more efficent.

It is by no means complete


UnloadLuaCom ()
LoadLuaCom ()

function open_files (which_files)

fso = luacom.CreateObject ("Scripting.FileSystemObject")
tf = fso:GetFile(which_files)
ts = tf:OpenAsTextStream ()

fso = nil
collectgarbage ()

end

----------------------------------------
function getnumberoflines ()
ts = tf:OpenAsTextStream ()
--get number of lines
numoflines = 0
while ts.AtEndOfStream ~= 1 do
numoflines = numoflines + 1
ts.SkipLine (1)
end

print ("Number of lines in the file ".. numoflines)

err = ts.Close
if not err then
print (err)
end
end

---------------
-- newdate function allows you to increase a date by a number of days you define
-- and still retain correct calendar formatting
-- Requires you to define the variables: curday, curmonth, curyear before function
-- Pass the dayfix within the parens when calling the function - i.e. newdate(2)
-- Function returns the variables: newday, newmonth, newyear
function newdate(dayfix)
newday = tonumber(curday)+dayfix
newmonth = tonumber(curmonth)
newyear = tonumber(curyear)
if newday > 31 then
if newmonth == 1 or newmonth == 3 or newmonth == 5 or newmonth == 7
or newmonth == 8 or newmonth == 10 or newmonth == 12 then
newday = newday - 31
newmonth = newmonth + 1
end
if newmonth > 12 then
newmonth = newmonth - 12
newyear = newyear + 1
end
end
if newday > 30 then
if newmonth == 4 or newmonth == 6 or newmonth == 9 or newmonth == 11 then
newday = newday - 30
newmonth = newmonth + 1
end
end
if newday > 28 and newmonth == 2
and newyear/4 ~= tonumber(format('%d',newyear/4)) then
newday = newday - 28
newmonth = newmonth + 1
end
if newday > 29 and newmonth == 2 then
newday = newday - 29
newmonth = newmonth + 1
end
newday = format('%02d',newday)
newmonth = format('%02d',newmonth)
if newmonth == "01" then newmonth = "Jan"
elseif newmonth == "02" then newmonth = "Feb"
elseif newmonth == "03" then newmonth = "Mar"
elseif newmonth == "04" then newmonth = "Apr"
elseif newmonth == "05" then newmonth = "May"
elseif newmonth == "06" then newmonth = "Jun"
elseif newmonth == "07" then newmonth = "Jul"
elseif newmonth == "08" then newmonth = "Aug"
elseif newmonth == "09" then newmonth = "Sep"
elseif newmonth == "10" then newmonth = "Oct"
elseif newmonth == "11" then newmonth = "Nov"
elseif newmonth == "12" then newmonth = "Dec"
end
newyear = format('%02d',newyear)
return (newday.." "..newmonth.." "..newyear)
end
-----------------------------------
function searchfile(getdate)

ts = tf:OpenAsTextStream ()

for i = 1, numoflines do
line = i
line1 = ts.ReadLine(1)
searchdate1, searchdate2 = strfind (line1, "%d+%s%a+%s%d+,")
--print (searchdate1, searchdate2)
if (searchdate1 ~= nil) or (searchdate2 ~= nil) then
searchdate = strsub (line1, searchdate1, searchdate2 - 1)
--print (searchdate)

if searchdate == getdate then
line2 = ts.ReadLine(1)
line3 = ts.ReadLine(1)
line4 = ts.ReadLine(1)
line5 = ts.ReadLine(1)
break
end
end
end

err = ts.Close
if not err then
print (err)
end
return line
end
-------------------------------------------------
--[[
curmonth = date ("%m")
curday = date ("%d")
curyear = date ("%Y")
print ("Original date: "..curday.."/"..curmonth.."/"..curyear)
print ("New date: "..newday.."/"..newmonth.."/"..newyear)

currentdate1 = newdate(0)
currentdate2 = newdate(1)
currentdate3 = newdate(2)
currentdate4 = newdate(3)
currentdate5 = newdate(4)

print (currentdate1)
print (currentdate2)
print (currentdate3)
print (currentdate4)
print (currentdate5)
]]--
------------------------------

open_files ("C:\\Documents and Settings\\Dannyboy\\Desktop\\book1.csv")
getnumberoflines ()
print(searchfile (date("%d %b %Y")))
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)

Chris, This was what I needed the date stuff for.

Thanks

Dan

VaioUserChris
September 7th, 2004, 03:52 PM
Very nice Dan.

The only suggestion I have has to do with the file system object you call. Lua has a built-in ability to read files (as well as write and append to) so you don't need to call the file system object in this case.

If you use the built-in function, you can eliminate getnumberoflines() and open_files() functions from your script and replace the searchfile function with this:

function searchfile(getdate, filename)

tempf = readfrom(filename)
line = 0
while 1 do
line1 = read(tempf)
if not line1 then break end
line = line + 1

searchdate1, searchdate2 = strfind (line1, "%d+%s%a+%s%d+,")
--print (searchdate1, searchdate2)
if (searchdate1 ~= nil) or (searchdate2 ~= nil) then
searchdate = strsub (line1, searchdate1, searchdate2 - 1)
--print (searchdate)

if searchdate == getdate then
line2 = read(tempf)
line3 = read(tempf)
line4 = read(tempf)
line5 = read(tempf)
break
end
end
end
readfrom()
return line
end Then the last set of lines in the script would become:

filename = "C:\\Documents and Settings\\Dannyboy\\Desktop\\book1.csv"
print(searchfile (date("%d %b %Y"), filename))
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)Again, I'm not at my development computer so I haven't tested this script, but I think it will work. If not, let me know and I'll debug when I get home.

Chris

danward79
September 7th, 2004, 04:05 PM
Hi

Do you think that would be quicker?

I am not a programmin expert. I suppose the built in functions for lua, would be quicker than calling a FSO.

Perhaps I will try it and see. I wonder how I could time it!...

Any ideas?

Dan

danward79
September 7th, 2004, 04:08 PM
Man you are good! :)

That worked first time!

Cheers will try that, tomorrow whaen I get back from work.

Dan

VaioUserChris
September 7th, 2004, 04:38 PM
My hunch is that the built-in function would be faster than calling the FSO. But given the length of this script you're probably looking at a fraction of a second difference in time

I suppose you could test the timing by getting the tick count at the beginning and end of the script then subtracting to see how many cycles it took. I can't recall the function name for get tick count, though.

You might find this funny but until I saw your original post on this thread, I didn't know you could get a date by simply using date("%d") I thought it could only be called after defining a timer (since that's the only example I found here). That's the great thing about this forum. I'm learning all the time.

I think I'll finally break down and buy the Lua book.

Chris

danward79
September 7th, 2004, 10:52 PM
you should get the book, I did. I read it on the train to work, I helps send me to sleep on the way to work... I still have'nt finished it!

just remember it is lua version 5 thou

Dan