LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to get date in future? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-date-in-future-642236/)

niner710 05-14-2008 06:58 PM

How to get date in future?
 
Hi,

I write data to a crontab and want to see what the date the day before would be. Here is my crontab file.

35 21 1 7 6 perl blah.pl

So this is...
Min = 35,
Hours = 9pm,
Days = 1,
Months = 7,
Weekday = 6.

So the current date for the crontab is July 1, 9:35PM, Saturday. I would like to get the date for the day before, hence June 30, 9:35PM, Friday.

How best could I do this?

chrism01 05-14-2008 07:10 PM

Seeing as you mention Perl, use the Date::Calc.pm module.
Are you saying you want the called perl prog to calculate this, or something else. Its not clear?

niner710 05-15-2008 12:48 AM

So basically I have an html form that takes user input on what time a meeting is scheduled. It will take the day, month, time of a meeting. I would like to send a meeting notice out with blah.pl exactly 1 day before the scheduled meeting. I would like perl to take the input from the html form and calculate what would be the exact month, day, weekday, time, one day prior to the original inputs.

niner710 05-15-2008 02:06 AM

Ok, yah the Date::Calc module was really great. Pretty easy to do what I wanted with this.

Used the Date_to_Days function and Add_Delta_Days function and it gave me what I wanted. Thanks!

frater 05-10-2009 03:40 AM

I wrote this bash script because I couldn't find anything on the net.
It turns the given date (if no parameters is given it will be today) to "Linux seconds" (seconds as of 1970 Jan 1 00:00) adds days * 86400 (a day has 86400 seconds) and then back again.

I also made an "addhour" script the same way.

Code:

# adday
20090511
# adday 2
20090512
# adday -2
20090508
# adday 365
20100510
# adday 1 20090228
20090301
adday -2 20040301 01:00
20040228 01:00:00

# 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}"


frater 05-10-2009 03:45 AM

Code:

# addhour 1 20090228
20090228 01:00
# addhour -1 20090301
20090228
# addhour 1 20090228 12:00
20090228 13:00
# addhour 30 20090228 12:00
20090301 18:00
# addhour -30 20090301 12:00
20090228 06:00
# date -d "`addhour -30 20090301 12:00`"
Sat Feb 28 06:00:00 CET 2009
# date -d "`addhour -30 20090301 12:05:03`"
Sat Feb 28 06:05:03 CET 2009

# cat /usr/bin/addhour
Code:

#!/bin/sh
# Written by JP
# addhour [hours] [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 * 3600 ))
DATESECONDS=`date  +%s -d "${DATE}"`
let DATESECONDS+=$DAYSECONDS

date -d "1970-01-01 00:00 UTC $DATESECONDS sec" +"${format}"



All times are GMT -5. The time now is 10:16 PM.