Increment a date variable in perl script

Hi,

I have a perl script which prints epoch value of date in milliseconds as the output.
My reuirement is that once the output is printed,the day variable shld increment by 1 and when i execute the script for the second time the output shld be for the new day value.

My script looks as below:

#!/usr/bin/perl
use Time::Local ;
$sec=00;
$min=01;
$hours=00;
$day=14;
$month=7;
$year=109;
#
$Beg_time = (timelocal($sec,$min,$hours,$day,$month,$year) * 1000);
print "Epoch Beg Value: $Beg_time\n";

======================================================

1st execution output will be for "14th of August"

When i execute second time the output shld be for 15th August.

Can someone pls suggest.

Store result in file .
Next time read the file and increment .
"File" can be file , DB , whatever persistent .

hi,

I would like to increment $day value to 15 after first execution of the script.If i understood correctly the solution u gave is to store the result ie output in a file.
How to increment $day=14 to $day=15 ?


my $somefile = "/tmp/whatever" ;

sub store  { 
  my $what = shift ; 
  open my $file , ">$somefile" or die "$!" ; 
  print $file "$what"  ;
  close $file;
}

sub read_it { 
  open my $file , "<$somefile" or die "$!" ; 
  my $result = <$file> ; 
  close $file ;
  return $result ;  
}

if  ( -f  $somefile ) { 
   my $result = read_it();
   $result += 24 * 3600 ; # increment 1 day , date should be in epoch format 
   store($result);
   # do whatever you want with new result
} else {
  # original code  
  # 
  store($starting_date);
}
  

Store data in file in epoch format .
There is almost no exception handling in this code and it will not work concurrent .

You can check the date/timestamp on the file and then just increment the date/day by 1.

Try:

#!/usr/bin/perl -w
use Time::Local ;
$sec=00;
$min=01;
$hours=00;
$day=14;
$month=7;
$year=109;
#
$Beg_time = (timelocal($sec,$min,$hours,$day,$month,$year) * 1000);
$i=0;
while (++$i<10) {
$val=$Beg_time*86400000;
printf "Epoch Beg Value %d:%f\n",$i,$val;
}

Thanks for the reply but this does not give me what is required.I hope my query was clear.
when i execute the code which i posted it prints me the epoch value for "Aug 14 00:01:00".
Once it has printed the output,i want that the day value is inremented by 1 ie "Aug 15 00:01:00" so that i can get the epoch value of Aug 15 when i execute the script the second time.

Hope I get some reply which suits my requirement.

try this one..
script checks/updates self timestamp on every run.

 
#!/usr/bin/perl
use Time::Local ;
 
@timeData = localtime((stat($0))[9]);
$sec=$timeData[0];
$min=$timeData[1];
$hours=$timeData[2];
$day=$timeData[3]+1;  
$month=$timeData[4];
$year=$timeData[5];
$Beg_time = (timelocal($sec,$min,$hours,$day,$month,$year) * 1000);
print "Epoch Beg Value: $Beg_time\n";
$year=$year+1900 ;
$month=$mount+1;
$TS=(sprintf "%4d%02d%02d%02d%02d.%02d",$year,$month,$day,$hours,$min,$sec);
system("touch -t $TS $0");

hope this one helps.

Hi Xoops,
This code works fine with the present date.
But can u tell me how can i give a date of my choice and get the output in similar way?

regards,
jyothi

may not efficient but should work,

#!/usr/bin/perl
use Time::Local ;
$sec=00;
$min=01;
$hours=00;
$day=&get_day;
$month=7;
$year=109;
 
$Beg_time = (timelocal($sec,$min,$hours,$day,$month,$year) * 1000);
print "Epoch Beg Value: $Beg_time\n";
 
add_day();
 
 
sub get_day {
 
 my $val = 0;
 open (F,"file") || die "unable to open";
  while ($line = <F>){ chomp($line);if ( $.==1) { $val=$line }};
  close F;
 return $val;
}
 
sub add_day {
 
 my $cday = 0;
 $cday = &get_day;
 $cday=$cday+1;
 open (F,">file") || die "unable to open";
  print F "$cday\n";
 close F;
}

this will take the day value from a file called "file".

You can set the timestamp of your script to the date you want and it should continue from there.
Script just picks the modifiction date of itself.

 
touch -t [[CC]YY]MMDDhhmm[.ss] filename
#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;
use constant ONE_DAY => 24 * 60 * 60;

my $file = 'date';

my @date = get_date();

print 'Epoch Beg Value: ',
      my $date = (timelocal(@date) * 1000),
      "\n";

$date /= 1000;
$date += ONE_DAY;

save_date((localtime $date)[0 .. 5]);



sub get_date {
  my @default = qw(0 1 0 14 7 109);

  unless (-f $file && -r _) {
    return @default;
  }

  my $d;
  unless ( open $d, '<', $file ) {
    return @default;
  }

  chomp(my $date = <$d>);

  return @default unless $date;

  my @date = split/:/, $date;

  if (@date == 6) {
    return @date;
  } else {
    return @default;
  }
}

sub save_date {
  my @date = @_;

  my $d;
  unless ( open $d, '>', $file ) {
    die "Can't write to $file\n";
  }

  print $d join ':', @date;
  print $d "\n";
}