HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Date arithmetic within shell
Operating System - HP-UX
1832855
Members
2935
Online
110047
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 05:17 PM
01-10-2001 05:17 PM
Can anyone help me?
How do I perform arithmetic on dates within a shell script? I need to set a variable equal to something like "Now - 6 days", and, as if that wasn't enough, the final format of this date variable needs to be "MM/DD/YY HH:MM:SS". This is because the variable gets passed as a parameter to an executable which expects it in that format.
TIA,
Jim
How do I perform arithmetic on dates within a shell script? I need to set a variable equal to something like "Now - 6 days", and, as if that wasn't enough, the final format of this date variable needs to be "MM/DD/YY HH:MM:SS". This is because the variable gets passed as a parameter to an executable which expects it in that format.
TIA,
Jim
Remember, wherever you go, there you are...
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 05:50 PM
01-10-2001 05:50 PM
Solution
Hi Jim:
A C-program. Perl, awk or a shell function -- any of which you develop is needed for date offsets greater than 24-hours.
See this thread for an awk program (Al Langen's contribution) that does what you want:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x57eff841489fd4118fef0090279cd0f9,00.html
For a very clever computation of a dates +- 24-hours using the TZ variable, follow Rich Garland's contributions through the discussion referenced in this thread:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0xf9fd5f260cafd4118fef0090279cd0f9,00.html
Regards!
...JRF...
A C-program. Perl, awk or a shell function -- any of which you develop is needed for date offsets greater than 24-hours.
See this thread for an awk program (Al Langen's contribution) that does what you want:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x57eff841489fd4118fef0090279cd0f9,00.html
For a very clever computation of a dates +- 24-hours using the TZ variable, follow Rich Garland's contributions through the discussion referenced in this thread:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0xf9fd5f260cafd4118fef0090279cd0f9,00.html
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2001 06:19 PM
01-10-2001 06:19 PM
Re: Date arithmetic within shell
Thanks James! I too found Al's code just after posting my plea for help.
I've tweaked it slightly so that it can handle subtracting any number of days from the date passed into it.
x=`date +%x`
z=6;export z
y=`echo $x|awk whatever.awk`
Returns a valid value for any value of z, where z does not result in a date from prior to the previous year. That is, if x="01/10/01", a z>=376 will return an invalid date.
BEGIN{ FS="/"
split( "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month," ");
split( "31 28 31 30 31 30 31 31 30 31 30 31", days, " ");
}
{
year = $3
leapyear = year % 4;
if( leapyear == 0 )
days[2] = 29
else
days[2] = 28;
doy = 0;
for ( i = 1; i< $1; i++ )
doy = doy + days[i]
doy = doy + $2;
if ( doy <= ENVIRON["z"] )
{
doy = doy + 365;
year = year - 1;
leapyear = year %4;
if( leapyear == 0 )
{
days[2] = 29;
doy = doy + 1;
}
}
doy = doy - ENVIRON["z"];
for ( i = 1; i <= 12; i++ )
{
doy = doy - days[i];
if ( doy <= 0 )
{
doy = doy + days[i];
break;
}
}
printf( "%d/%d/%d", i, doy, year )
}
I've tweaked it slightly so that it can handle subtracting any number of days from the date passed into it.
x=`date +%x`
z=6;export z
y=`echo $x|awk whatever.awk`
Returns a valid value for any value of z, where z does not result in a date from prior to the previous year. That is, if x="01/10/01", a z>=376 will return an invalid date.
BEGIN{ FS="/"
split( "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", month," ");
split( "31 28 31 30 31 30 31 31 30 31 30 31", days, " ");
}
{
year = $3
leapyear = year % 4;
if( leapyear == 0 )
days[2] = 29
else
days[2] = 28;
doy = 0;
for ( i = 1; i< $1; i++ )
doy = doy + days[i]
doy = doy + $2;
if ( doy <= ENVIRON["z"] )
{
doy = doy + 365;
year = year - 1;
leapyear = year %4;
if( leapyear == 0 )
{
days[2] = 29;
doy = doy + 1;
}
}
doy = doy - ENVIRON["z"];
for ( i = 1; i <= 12; i++ )
{
doy = doy - days[i];
if ( doy <= 0 )
{
doy = doy + days[i];
break;
}
}
printf( "%d/%d/%d", i, doy, year )
}
Remember, wherever you go, there you are...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2001 09:24 AM
01-11-2001 09:24 AM
Re: Date arithmetic within shell
There is a REAL simple way to do this.
Here is a PERL example:
use Time::Local;
$yy = `date +%y`;
$mm = `date +%m`;
$day = `date +%d`;
chomp($day);
chomp($yy);
chomp($mm);
$start = timelocal("00", "00", "00", $day, ($mm-1), $yy);
$pcurr = $start - (7*86400); # get day 7 days prior
$fcurr = $start + (7*86400); # get day 7 days in future
print "\nDate: ".localtime($fcurr); # or pcurr for past
# you can also grab the elements and apply them to some hashes for conversions if you like.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$dst)=localtime($fcurr); # or use pcurr if you want the past
Basically it is three steps.
1. Get the current date/time in seconds (easy on UNIX)
2. Add the number of days * 86400
3. Convert the seconds to human readable date/time.
Have fun!
Here is a PERL example:
use Time::Local;
$yy = `date +%y`;
$mm = `date +%m`;
$day = `date +%d`;
chomp($day);
chomp($yy);
chomp($mm);
$start = timelocal("00", "00", "00", $day, ($mm-1), $yy);
$pcurr = $start - (7*86400); # get day 7 days prior
$fcurr = $start + (7*86400); # get day 7 days in future
print "\nDate: ".localtime($fcurr); # or pcurr for past
# you can also grab the elements and apply them to some hashes for conversions if you like.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$dst)=localtime($fcurr); # or use pcurr if you want the past
Basically it is three steps.
1. Get the current date/time in seconds (easy on UNIX)
2. Add the number of days * 86400
3. Convert the seconds to human readable date/time.
Have fun!
Laziness is the father of invention.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP