date in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date in perl
# 1  
Old 06-11-2009
date in perl

im having trouble with this... i can do it in awk but perl is giving me trouble.
i want to test for the previous 3 days, today, and the coming three days.
heres the code:

#!/usr/bin/perl
chomp($info=`date +%b%d`);
$info =~ tr/[A-Z]/[a-z]/;
if ($info =~ m/^([a-z]*)([0-9])$/) {
$info = "$1.0.$2";
} #above date is stored in "list" as mmmdd, "jun04" etc.
#followed by other info divided by spaces.

open(FILE, "list");
close(@file=<FILE>);

$count = 0;
while ($file[$count] ne "") {
if ($file[$count] =~ $info) {
print $file[$count];
}
$count++;
}


finished. any ideas on how to test for the week span, instead of just today? thanks in advance if you can help.
# 2  
Old 06-11-2009
Code:
C:\>
C:\>type dates.txt
06/06/2009
06/07/2009
06/08/2009
06/09/2009
06/10/2009
06/11/2009
06/12/2009
06/13/2009
06/14/2009
06/15/2009
C:\>
C:\>
C:\>type testscr.pl
#!perl -w
use Date::Calc;
($yr,$mon,$dy) = Date::Calc::Today();
($sy,$sm,$sd) = Date::Calc::Add_Delta_Days($yr,$mon,$dy,-3);
($ey,$em,$ed) = Date::Calc::Add_Delta_Days($yr,$mon,$dy,3);
print "Today            = $mon/$dy/$yr\n";
print "Start date range = $sm/$sd/$sy\n";
print "End date range   = $em/$ed/$ey\n";
print "="x30,"\n";
##
open (DATES,"dates.txt") or die "Can't open dates.txt: $!";
while (<DATES>) {
  chomp($line = $_);
  @x = split /\//, $line;
  print $line,"\n" if abs(Date::Calc::Delta_Days($yr,$mon,$dy,$x[2],$x[0],$x[1])) <= 3
}
close(DATES) or die "Can't close dates.txt: $!";
C:\>
C:\>
C:\>perl testscr.pl
Today            = 6/11/2009
Start date range = 6/8/2009
End date range   = 6/14/2009
==============================
06/08/2009
06/09/2009
06/10/2009
06/11/2009
06/12/2009
06/13/2009
06/14/2009
C:\>
C:\>

tyler_durden
# 3  
Old 06-11-2009
i was using:

for (i=-3;i<=3;i++)
cmd="date --date=\"" i "days\" \"+%b%d\""
cmd | getline
close(cmd)

in gawk. is there a way without using date::calc?
# 4  
Old 06-11-2009
Quote:
Originally Posted by nomkev
i was using:
...
in gawk. is there a way without using date::calc?
I'd assume that date is a datatype in gawk, which allows you to perform simple date arithmetic like "date + 3" or "date - 3". As far as I know, date is not a datatype in Perl.

Of course, there is a way without using date::calc. And that is - implementing that logic by yourself, say, in a module. So the module would take care of things like changes in months, years, leap years, non-leap years (also the ones divisible by 100 but not 400) etc. Personally, I think that is too much work; and if someone else has done that work and tested it, then I don't see why I cannot use the module (Date::Calc etc.). The very reason they write modules is so others don't reinvent the wheel.

Yet another thought is - if your underlying shell allows you to perform date arithmetic, then you could invoke those commands using the "system" function. But then that's a shell script and not a perl program in the strictest sense.

tyler_durden
# 5  
Old 06-11-2009
Well, if you are doing this:

chomp($info=`date +%b%d`);

why not just continue to use the operating systems date command to get the range of days you want and use them to compare to the file?
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Today's date using Perl?

Easiest way to get it in the form of MM/DD/YY for Perl 5.8.8? Thanks (4 Replies)
Discussion started by: stevensw
4 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

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

5. Shell Programming and Scripting

Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl

Hi , This script working for fine if pass script-name.sh Jul/10/2007 ,I want to pass 20070710(yyyymmdd) .Please any help it should be appereciated. use Time::Local; my $d = $ARGV; my $t = $ARGV; my $m = ""; @d = split /\//, $d; @t = split /:/, $t; if ( $d eq "Jan" ) { $m = 0 }... (7 Replies)
Discussion started by: akil
7 Replies

6. Shell Programming and Scripting

Date in Perl

Hi All, I have a Perl code which performs ftp-ing of files. However, it seems like the variable $date_today is not passed on to this statement grep { /^ME.*JMR.*$date_today.*sorts$/ } and i can't ftp the files that i wanted. Can any expert help me to debug this ? Is there any thing wrong... (3 Replies)
Discussion started by: Raynon
3 Replies

7. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question