5

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
MRCLE
  • 75
  • 4

2 Answers2

3

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
  • Sorry, I wasn't fully clear in my initial post. This was very helpful and along the lines of what I was trying to accomplish. The goal in this context would be to move date1 and date2 forward 14 days at a time, and loop until the target date is reached. The rebuild new dates portion is what I did not understand how to approach. Thanks for the help, I am new to lua and was not sure what functions I could use, the os.time/date in particular were what I was looking for. This also answers my question regarding date arithmetic, I was not sure if a lua table could do that – MRCLE Nov 04 '13 at 20:43
  • @user2943428 Welcome on Stack Overflow, then! Remember to read the [about] and [help] pages if you haven't already done it. Moreover the [ask] page will help you learn how to ask good questions. And keep in mind to accept the answer that helped you the most (you may consider waiting for a little while before accepting one to see if a better answer is posted). Additionally, when you reach enough reputation you'll be able also to upvote a question or an answer if you deem them useful. This is the mechanism that help keep SO quality high. – LorenzoDonati4Ukraine-OnStrike Nov 04 '13 at 20:47
  • @user2943428 about your last comment: "I was not sure if a lua table could do that". Just for the record, it's not a mechanism related to Lua tables. It's `os.time` that handles it internally. Lua tables are just the data structure used to store the data upon which `os.time` operates. – LorenzoDonati4Ukraine-OnStrike Nov 04 '13 at 20:50
  • Thank you for the welcome, much appreciated. And to clarify on that last bit regarding os.time, does this os.time function understand the inputs of day, month and year for what they are? Or are these variables that are being defined and called again later? – MRCLE Nov 04 '13 at 20:59
  • It is not clear what you mean by "does this os.time function understand the inputs of day, month and year for what they are". Maybe it is better for you to read the documentation I linked to in my answer and see if it makes sense for you. In short: `os.time` takes the provided table and uses its fields to determine the corresponding time value. It doesn't store anything you pass to it. – LorenzoDonati4Ukraine-OnStrike Nov 04 '13 at 21:10
  • Yes thank you, the documentation cleared that up for me. My question was whether or not os.time understood what each field value means, ie days can be (1-31) months (1-12) and years are four digits, etc. I didn't know if these were fields specific to os.time or if these were variables we were defining – MRCLE Nov 04 '13 at 21:22
0

You will likely want to use string.match for the parsing and os.time and os.date to construct the correct new time string (os.time to handle converting your new added date into the correct month/year and os.date to turn the time returned by os.time into a human-friendly string/table again) to do your date check looping/stopping.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148