LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2008, 07:58 PM   #1
niner710
Member
 
Registered: Apr 2008
Posts: 37

Rep: Reputation: 15
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?
 
Old 05-14-2008, 08:10 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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?
 
Old 05-15-2008, 01:48 AM   #3
niner710
Member
 
Registered: Apr 2008
Posts: 37

Original Poster
Rep: Reputation: 15
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.
 
Old 05-15-2008, 03:06 AM   #4
niner710
Member
 
Registered: Apr 2008
Posts: 37

Original Poster
Rep: Reputation: 15
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!
 
Old 05-10-2009, 04:40 AM   #5
frater
Member
 
Registered: Jul 2008
Posts: 110

Rep: Reputation: 23
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}"

Last edited by frater; 05-10-2009 at 06:38 AM. Reason: negative days clashed with getopt
 
Old 05-10-2009, 04:45 AM   #6
frater
Member
 
Registered: Jul 2008
Posts: 110

Rep: Reputation: 23
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}"

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


Reply


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
Future of Linux, Future of BSD ? tarballed Linux - General 28 01-01-2012 05:04 PM
Perl/bash - calculate date in future rose_bud4201 Programming 6 05-10-2009 06:59 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 10:59 AM
Setting system date and time affecting the clock and date on BIOS satimis Ubuntu 7 09-21-2007 09:02 AM
what is the correct syntax order for tar with --after-date DATE, --newer DAT farhan Linux - General 1 03-16-2007 09:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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