Increment a date variable in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment a date variable in perl script
# 8  
Old 01-06-2010
try this one..
script checks/updates self timestamp on every run.

Code:
 
#!/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.
# 9  
Old 01-06-2010
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
jyothi_wipro
# 10  
Old 01-06-2010
may not efficient but should work,

Code:
#!/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".

Last edited by clx; 01-06-2010 at 10:01 AM.. Reason: some typo
# 11  
Old 01-06-2010
Quote:
Originally Posted by jyothi_wipro
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
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.

Code:
 
touch -t [[CC]YY]MMDDhhmm[.ss] filename

# 12  
Old 01-07-2010
Code:
#!/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";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to find a date variable and increment it

Hi, I have parameter file wo_location.prm which has a date variable $last_upd_date= 02032016. I need to write a unix shell script to find that variable and increment it by 1 day. The path to the file is root/dir_lc/shared/param/wo_location.prm and the variable is $last_upd_date. Any... (2 Replies)
Discussion started by: isenhiem
2 Replies

2. Shell Programming and Scripting

Increment date variable

hey guys, I need to incerement the date variable for instance echo `date '+%F %H:%M:00'` this produces 2014-08-02 20:05:00 -I will grant this to : $Datehour and need to assign 1 hr from now to $Datelasthour -the script time will be used to talk to DB system information. however... (4 Replies)
Discussion started by: mo_VERTICASQL
4 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

5. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

6. Shell Programming and Scripting

Date increment

hi Friends, Today_Dt=`date "+%Y-%m-%d"` So the Today date is 2010-05-03 I have a file which has date values as below 2010-04-27 2010-04-02 2010-04-18 2010-04-28 2010-04-29 .. (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

7. Shell Programming and Scripting

Increment in date

Hi, I have a variable lets say DATA_DATE. I have to pass some value to this variable in YYYYMMDD format. lets say today I have passed this variable as : DATA_DATE=20100107 Then pls help me how to calculate another variable DATA_DATE1 (which is DATA_DATE+1). The code should work... (3 Replies)
Discussion started by: 46019
3 Replies

8. Shell Programming and Scripting

Increment date in script

Hi, Iam new to scripting language.:o can someone help me out solving this thread?hopingly ya....:) I want to write a script which connects to db and searches the count in a table which has date column and id column. If the count is not equal to 0 then it should increment the date with the one... (4 Replies)
Discussion started by: jyothi_wipro
4 Replies

9. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

10. Shell Programming and Scripting

Shell Script To increment Date*HElp***

Hi, I want to increment date, using Shell Script, a loop to add one day after 24hrs. :confused: Cheers Kunal (1 Reply)
Discussion started by: niceboykunal123
1 Replies
Login or Register to Ask a Question