Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua Date Manipulation Loop

I'm attempting to run a batch script with two date values that I would like to adjust and then have the script run again. I would like to reference a lua script that will adjust those date values for me. I'm using lua simply because most of the project is coded in this language.

If my variables were start=01/01/2012 and end=02/01/2012 how could I go about moving both of these dates forward 14 days? I would then like to have it run, then loop again and move both forward ANOTHER 14 days. This would continue until the loop breaks with a date that I decide. Is it possible to accomplish this in this manner or am I approaching this wrong?

I imagine I could do something involving string.gsub with a function that would pull the "dd" from my dates and move that up 14. However, is it possible to do this kind of date arithmetic or would this break as soon as I reached the end of the month and it would try to read a date like 01/45/2012?

like image 590
MRCLE Avatar asked Oct 20 '22 21:10

MRCLE


1 Answers

Your specification is not completely clear. I guess the following script may be helpful. The trick is to convert your dates to time values, as returned by os.time, which can be compared as simple numbers.

To do this you first have to parse the string form of the dates and then convert them in a table form suitable to be fed to os.time. Note that incrementing the fields of those tables (which represent the dates with their components split) already handles the time arithmetic, i.e. specifying a day field with a value of, for example, 32 will wrap correctly to the next month when converted to time values.

When you reach the target date you convert the time value back to a date value in the format you need using os.date.

local TARGET_DATE = "03/05/2012"

-- get the two dates from the command line of this lua script

local date1 = arg[1]
local date2 = arg[2]

date1 = "01/01/2012"    -- test data - remove in actual script
date2 = "02/01/2012"    -- test data - remove in actual script

-- parse the dates (assuming the format day/month/year)

local d1, m1, y1 = string.match( date1,  '^(%d%d)/(%d%d)/(%d%d%d%d)$' )
local d2, m2, y2 = string.match( date2,  '^(%d%d)/(%d%d)/(%d%d%d%d)$' )
local td, tm, ty = string.match( TARGET_DATE,  '^(%d%d)/(%d%d)/(%d%d%d%d)$' )

print( d1, m1, y1 ) -- debug
print( d2, m2, y2 ) -- debug
print( td, tm, ty ) -- debug

date1_tbl = { day = d1, month = m1, year = y1 }
date2_tbl = { day = d2, month = m2, year = y2 }

local time1 = os.time( date1_tbl )
local time2 = os.time( date2_tbl )
local target_time = os.time { day = td, month = tm, year = ty }

-- loop until both dates exceed target date
while time1 < target_time or time2 < target_time  do
    date1_tbl.day = date1_tbl.day + 14
    date2_tbl.day = date2_tbl.day + 14
    time1 = os.time( date1_tbl )
    time2 = os.time( date2_tbl )
end

-- rebuild new dates
local newdate1 = os.date( "%d/%m/%Y", time1 )
local newdate2 = os.date( "%d/%m/%Y", time2 )

print( newdate1 ) -- debug
print( newdate2 ) -- debug
like image 113
Lorenzo Donati -- Codidact.com Avatar answered Oct 23 '22 23:10

Lorenzo Donati -- Codidact.com