Perl/bash - calculate date in future
Hi all,
I've got a date (call it "rentalDate") and a number of days ("daysRented") for a car-rental auditing type of app. Given the rental date and the number of days the car was rented for, I need to calculate the return date. I thought of using `date`, but afaik you can't use 2 "--date" flags (i.e. `date --date "yyyymmdd" --date 'X days from now'`)....anyone else got a way to do this? Thanks! |
Is there a requirement that you do it in a single command?
My suggestion would be something like this: 1) Read the "rentalDate" 2) Get the current date with the date command 3) Calculate the number of days elapsed between the rental date and toay 4) Subtract that number of days from the total number of days rented 5) Run the date command with something like "date -d "+${DAYS_REMAINING} days" This makes some assumptions. Primarily: 1) The program will process the records before the car is actually due back. This could be worked around by including the '+' or '-' sign in the DAYS_REMAINING variable. 2) The program will not be in the middle of processing a record when the clock strikes midnight. That is to say, if you execute the date in step #2 above, step #5 needs to occur before 12:00 AM. This is a pretty miniscule requirement unless the program would be used for some as part of a cron job. In general, scheduling cron jobs to run at or straddle 12:00 AM is a bad thing. |
Code:
#!/bin/bash |
Thanks, guys :) Both of those are great, and I should be able to do this now without too much trouble.
Much appreciated! |
date manipulation is (relatively :) ) easy in perl.
for example: Code:
Quote:
|
The reason I couldn't do it like that is I'm not calculating a date in the current future, necessarily...I'm calculating a date in the future from some arbitrary starting date, which probably occurred several months ago.
I liked Hko's solution for the simplicity of it, but doing nested bash commands in perl was sort of messy, so the way I ended up doing it was: Code:
use Date::Manip; |
I think this should give you all the flexibility you need.
I know it's a bit late (3 years), but I was looking myself for this code and then decided to write it myself. # cat adday Code:
#!/bin/sh Code:
# adday 10 20090525 |
All times are GMT -5. The time now is 06:29 AM. |