ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/bin/bash
DAYSRENTED="10"
RENTALDATE="20040319" # Format: yyyymmdd
RETURNDATE=$(date --date "$(date --date $RENTALDATE +%F) +$DAYSRENTED days" +%F)
echo "The car should be returned on: $RETURNDATE."
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;
....
sub calcFutureDate {
my $startDate = shift(@_);
my $timeSpent = shift(@_);
my @format = ("%Q");
my $futureDate = UnixDate(DateCalc($startDate, "+$timeSpent days"), @format);
return $futureDate;
}
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
# Written by JP
# adday [days] [yyyymmdd] [hh:mm]
format="%Y%m%d %T"
if [ -z $1 ];then
DATE=`date +"%Y%m%d %T"`
ADD=1
else
ADD="$1"
if [ -z "$2" ];then
DATE=`date +"%Y%m%d %T"`
else
shift 1
DATE="$*"
[ "$1" == "$*" ] && format="%Y%m%d"
fi
fi
DAYSECONDS=$(( $ADD * 86400 ))
DATESECONDS=`date +%s -d "${DATE}"`
let DATESECONDS+=$DAYSECONDS
date -d "1970-01-01 00:00 UTC $DATESECONDS sec" +"${format}"
Code:
# adday 10 20090525
20090604
# adday -10 20090525
20090515
# date
Sun May 10 12:58:32 CEST 2009
# adday 2
20090512 12:58:35
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.