By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,698 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,698 IT Pros & Developers. It's quick & easy.

Unix Date Help

P: 11
Hey guys.

I need to manipulate the system date to move forward a day. So, if the system date was 02/14/08, I need the 'new' date to be 02/15/08 so my script will look for the date of 02/15/08. The thing is the file I will be looking for always has the date of the next day. Any simple unix command solution?

Any ideas,

Thanks
Feb 14 '08 #1
Share this Question
Share on Google+
2 Replies

P: 58
Here's a Perl solution if you can't find anything simpler. You'll need to download the Time::Piece and Time::Seconds modules from CPAN.

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. use Time::Piece;
  4. use Time::Seconds;
  5.  
  6. &init;
  7.  
  8. sub init
  9. {
  10.    my $today      = localtime();
  11.    my $days_ahead = 1;
  12.    my $date       = &get_days_ahead ( $today, $days_ahead );
  13.  
  14.    print "$date\n";
  15. }
  16.  
  17. sub get_days_ahead
  18. {
  19.    my ( $date, $days ) = @_;
  20.  
  21.    for ( my $i = 0; $i < $days; $i++ )
  22.    {
  23.       $date += ONE_DAY;
  24.    }
  25.  
  26.    return ( &get_mmddyy ( $date ) );
  27. }
  28.  
  29. sub get_mmddyy
  30. {
  31.    my ( $date ) = @_;
  32.    my ( $weekday, $month, $day, $time, $year ) = split ( / +/, uc ( $date ) );
  33.  
  34.    if    ( $month eq "JAN" ) { $month = "01"; }
  35.    elsif ( $month eq "FEB" ) { $month = "02"; }
  36.    elsif ( $month eq "MAR" ) { $month = "03"; }
  37.    elsif ( $month eq "APR" ) { $month = "04"; }
  38.    elsif ( $month eq "MAY" ) { $month = "05"; }
  39.    elsif ( $month eq "JUN" ) { $month = "06"; }
  40.    elsif ( $month eq "JUL" ) { $month = "07"; }
  41.    elsif ( $month eq "AUG" ) { $month = "08"; }
  42.    elsif ( $month eq "SEP" ) { $month = "09"; }
  43.    elsif ( $month eq "OCT" ) { $month = "10"; }
  44.    elsif ( $month eq "NOV" ) { $month = "11"; }
  45.    elsif ( $month eq "DEC" ) { $month = "12"; }
  46.  
  47.    if ( length ( $day ) == 1 ) { $day = "0" . $day; }
  48.    $year = substr ( $year, 2 );
  49.  
  50.    return ( "$month/$day/$year" );
  51. }
Feb 14 '08 #2

Expert 100+
P: 511
Hey guys.

I need to manipulate the system date to move forward a day. So, if the system date was 02/14/08, I need the 'new' date to be 02/15/08 so my script will look for the date of 02/15/08. The thing is the file I will be looking for always has the date of the next day. Any simple unix command solution?

Any ideas,

Thanks
if you have GNU date
Expand|Select|Wrap|Line Numbers
  1. # date +%Y/%m/%d -d "1 day"
  2. 2008/02/16
  3.  
  4.  
Feb 15 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.