date with perl localtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date with perl localtime
# 1  
Old 11-04-2010
date with perl localtime

Hi Experts,

I know how to handle normal date changes in perl. Most of my requirement are full filled with following:

Code:
$date1 = strftime "%Y%m%d",localtime;
$date2 = strftime "%Y%m%d",localtime(time -24 * 60 * 60);
$date3 = strftime "%Y%m%d",localtime(time +24 * 60 * 60);
$date4 = strftime "%Y%m%d",localtime(time + 4 * 24 * 60 * 60);

etc etc.

Recently, i have this requirement say start date is(YYYYMMDD format) 20101028 and end date is 20101105 :
I need to print date in this format until it reaches end date
20101028
20101029
20101030
'
'
20101105

My idea is if i can somehow calculate variable for "time" as per my start date.
then i can do something like this
Code:
$date_ran = strftime "%Y%m%d",localtime(20101028 + 24 * 60 * 60);

I am not sure if this is correct approach. Any help / hint ?

Thanks in advance.

Last edited by pludi; 11-04-2010 at 10:57 AM.. Reason: code tags, please...
# 2  
Old 11-04-2010
Quote:
Originally Posted by mtomar
... say start date is(YYYYMMDD format) 20101028 and end date is 20101105 :
I need to print date in this format until it reaches end date
20101028
20101029
20101030
'
'
20101105

...
The DateTime core module makes this very simple and elegant -

Code:
$
$
$ cat -n iterate.pl
     1  #!perl -w
     2  use DateTime;
     3  ($y1, $m1, $d1) = unpack("A4A2A2",$ARGV[0]);
     4  ($y2, $m2, $d2) = unpack("A4A2A2",$ARGV[1]);
     5  $start_dt = DateTime->new(year => $y1, month => $m1, day => $d1);
     6  $end_dt   = DateTime->new(year => $y2, month => $m2, day => $d2);
     7  for ($dt = $start_dt->clone(); $dt <= $end_dt; $dt->add(days => 1)) {
     8    print $dt->ymd(""),"\n";
     9  }
$
$
$ perl iterate.pl 20101028 20101105
20101028
20101029
20101030
20101031
20101101
20101102
20101103
20101104
20101105
$
$
$ perl iterate.pl 20100225 20100303
20100225
20100226
20100227
20100228
20100301
20100302
20100303
$
$
$ perl iterate.pl 20080226 20080304
20080226
20080227
20080228
20080229
20080301
20080302
20080303
20080304
$
$
$ perl iterate.pl 20101227 20110103
20101227
20101228
20101229
20101230
20101231
20110101
20110102
20110103
$
$

HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 11-04-2010
Thanks a lot.
It works fine on my test system. I am afraid i will not be allowed to install any new module on production. I hope there is a stupid way to do it with basic perl package.
# 4  
Old 11-04-2010
Quote:
Originally Posted by mtomar
...I am afraid i will not be allowed to install any new module on production. I hope there is a stupid way to do it with basic perl package.
Use the POSIX module then. Unless your application is from the Dark Ages, you should be fine -

Code:
$
$
$ cat -n iterate1.pl
     1  #!perl -w
     2  use POSIX;
     3  ($y1, $m1, $d1) = unpack("A4A2A2",$ARGV[0]);
     4  ($y2, $m2, $d2) = unpack("A4A2A2",$ARGV[1]);
     5  $start = mktime(0,0,0,$d1,($m1-1),($y1-1900));
     6  $end   = mktime(0,0,0,$d2,($m2-1),($y2-1900));
     7  while ($start <= $end) {
     8    print strftime("%Y%m%d",localtime($start)),"\n";
     9    $start += 24*60*60;
    10  }
$
$
$ perl iterate1.pl 20101028 20101105
20101028
20101029
20101030
20101031
20101101
20101102
20101103
20101104
20101105
$
$
$ perl iterate1.pl 20100225 20100303
20100225
20100226
20100227
20100228
20100301
20100302
20100303
$
$
$ perl iterate1.pl 20080226 20080304
20080226
20080227
20080228
20080229
20080301
20080302
20080303
20080304
$
$
$ perl iterate1.pl 20101227 20110103
20101227
20101228
20101229
20101230
20101231
20110101
20110102
20110103
$
$
$

tyler_durden
These 2 Users Gave Thanks to durden_tyler For This Post:
# 5  
Old 11-06-2010
Thanks a lot !!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use a variable in perl localtime()?

Hi all, a=$1 ## b=`echo "86400 * $a"|bc` `perl -e 'use POSIX qw(strftime);$now_string = strftime "%d/%m/%Y", localtime(time-$b); print $now_string,"\n";' > date_file` but im always getting current date; can any one suggest me any the improvement the above works fine if i use some thing... (2 Replies)
Discussion started by: zozoo
2 Replies

2. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

3. Shell Programming and Scripting

Extract week start,end date from given date in PERL

Hi All, what i want to do in perl is i should give the date at run time .Suppose date given is 23/12/2011(mm/dd/yyyy) the perl script shold find week start date, week end date, previous week start date,end date,next week start date, end date. In this case week start date will be-:12/19/2011... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

4. Shell Programming and Scripting

Perl help LocalTime in New Format

Hi, I'm new to perl scripting and am trying it out. I have a file written in the following format: myfile-MMDDYY where MM is the number of the Month; DD the Day and YY the last two of the year... (Apologies for dumbing this down; I'm trying to be clear). There is a new file put onto my... (2 Replies)
Discussion started by: Astrocloud
2 Replies

5. Shell Programming and Scripting

Need to capture dates between start date and end date Using perl.

Hi All, Want to get all dates and Julian week number for that date between the start date and end date. How can I achive this using perl? (To achive above functionality, I was connecting to the database from DB server. Need to execute the same script in application server, since databse... (6 Replies)
Discussion started by: Nagaraja Akkiva
6 Replies

6. Shell Programming and Scripting

Pack and unpack localtime in perl script

Hi I have a code like this: sub WriteEbcdicHeader { my $Htimestamp=localtime();#i need to pack and unpack this my $eheaderline = $Htimestamp; #packing has to be done here #unpacking has to be done after packing print $EOUTFILE return $eheaderline; } sub WriteEbcdicTrailer { ... (5 Replies)
Discussion started by: rbathena
5 Replies

7. Shell Programming and Scripting

what is the default return type of localtime() in perl?

Hi, I have given like this to get the time of the sub routine. my $start = localtime(); print "\n start time: $start \n"; Output start time: Fri Apr 29 01:01:31 2011 I want to know what is the format of the time. I am not able to follow is is HH:MM:SS or MM:HH:SS os... (2 Replies)
Discussion started by: vanitham
2 Replies

8. Shell Programming and Scripting

PERL localtime Warning

Hello All, I am facing a warning "Argument "" isn't numeric in localtime at" what i m using is below my $timestamp = Timestamp(time); go_log("###############$timestamp###############"); can some one please suggest the way to avoid this message :confused: (6 Replies)
Discussion started by: NIMISH AGARWAL
6 Replies

9. Shell Programming and Scripting

Perl + localtime()

ok here is a perl date question not asked befor. i know i am feeling small for not knowing. BUT!!!! $ENV{TZ}="US/Central"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(); how can i do the addition to year so i can get the current year w/o going $ntime=$year+1900;... (3 Replies)
Discussion started by: Optimus_P
3 Replies
Login or Register to Ask a Question