LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl/bash - calculate date in future (https://www.linuxquestions.org/questions/programming-9/perl-bash-calculate-date-in-future-338815/)

rose_bud4201 06-30-2005 02:33 PM

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!

Dark_Helmet 06-30-2005 02:48 PM

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.

Hko 06-30-2005 03:04 PM

Code:

#!/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."


rose_bud4201 06-30-2005 05:26 PM

Thanks, guys :) Both of those are great, and I should be able to do this now without too much trouble.
Much appreciated!

bigearsbilly 07-01-2005 08:57 AM

date manipulation is (relatively :) ) easy in perl.
for example:


Code:


#!/usr/bin/perl -w

$days = 60 * 60 * 24;  # seconds in a day

my  $now = time;
my  $then = time + (21 * $days);

print "\nfrom\t" . scalar localtime $now;
print "\nto:\t"  . scalar localtime $then;
print "\n";

Quote:

$ perl perltime

from Fri Jul 1 14:51:46 2005
to: Fri Jul 22 14:51:46 2005


rose_bud4201 07-01-2005 10:14 AM

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;
}


frater 05-10-2009 05:59 AM

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



All times are GMT -5. The time now is 06:29 AM.