Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-30-2005, 02:33 PM   #1
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
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?
Old 06-30-2005, 02:48 PM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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.
Old 06-30-2005, 03:04 PM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110

RENTALDATE="20040319"   # Format: yyyymmdd

RETURNDATE=$(date --date "$(date --date $RENTALDATE +%F) +$DAYSRENTED days" +%F)
echo "The car should be returned on: $RETURNDATE."
Old 06-30-2005, 05:26 PM   #4
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
Thanks, guys Both of those are great, and I should be able to do this now without too much trouble.
Much appreciated!
Old 07-01-2005, 08:57 AM   #5
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

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

#!/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";
$ perl perltime

from Fri Jul 1 14:51:46 2005
to: Fri Jul 22 14:51:46 2005
Old 07-01-2005, 10:14 AM   #6
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
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:
use Date::Manip;
sub calcFutureDate {
    my $startDate = shift(@_);
    my $timeSpent = shift(@_);
    my @format = ("%Q");

    my $futureDate = UnixDate(DateCalc($startDate, "+$timeSpent days"), @format);
    return $futureDate;
Old 05-10-2009, 05:59 AM   #7
Registered: Jul 2008
Posts: 110

Rep: Reputation: 23
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
# Written by JP
# adday [days] [yyyymmdd] [hh:mm]

format="%Y%m%d %T"
if [ -z $1 ];then
 DATE=`date +"%Y%m%d %T"`
 if [ -z "$2" ];then
  DATE=`date +"%Y%m%d %T"`
  shift 1
  [ "$1" == "$*" ] && format="%Y%m%d"

DAYSECONDS=$(( $ADD * 86400 ))
DATESECONDS=`date +%s -d "${DATE}"`

date -d "1970-01-01 00:00 UTC $DATESECONDS sec" +"${format}"
# adday 10 20090525
# adday -10 20090525
# date
Sun May 10 12:58:32 CEST 2009
# adday 2
20090512 12:58:35

Last edited by frater; 05-10-2009 at 06:00 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl: get file date kenneho Programming 5 11-01-2005 11:16 AM
Date calculations in BASH script Crashman Programming 4 07-03-2004 10:15 AM
Date in a BASH script... soulsniper Linux - Software 7 11-22-2003 06:08 PM
parsing mail.log with perl and calculate mail traffic on domain base markus1982 Programming 1 03-18-2003 06:22 AM
perl and date manipulation Syncrm Programming 5 10-28-2002 02:05 AM > Forums > Non-*NIX Forums > Programming

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration