Re: script to compare dates

2009-08-18 Thread Steve Bertrand
Mihir Kamdar wrote:
> i am sorry Steve...actually I miswrote DateTime instead of DateManip...
>  
> i saw your code using DateTime but I before I posted this, I was trying
> using DateManip...but while testing the code, I found out that DateManip
> is not installed...
>  
> i tried executing your code as well but even DateTime is not installed
> in my Linux box..
>  
> while trying to install both DateTime and DateManip in my machine, it is
> giving some network error..."Url not found..."
>  
> need to figure out a way to install them..

I'll be at the office for a little while longer. I'd be pleased to help
you out to get the modules installed, and help you on your way if I can.

Contact me off-list.

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: script to compare dates

2009-08-18 Thread Mihir Kamdar
i am sorry Steve...actually I miswrote DateTime instead of DateManip...

i saw your code using DateTime but I before I posted this, I was trying
using DateManip...but while testing the code, I found out that DateManip is
not installed...

i tried executing your code as well but even DateTime is not installed in my
Linux box..

while trying to install both DateTime and DateManip in my machine, it is
giving some network error..."Url not found..."

need to figure out a way to install them..

On Tue, Aug 18, 2009 at 11:22 PM, Steve Bertrand  wrote:

> Mihir Kamdar wrote:
> > thanks all.and how will be the code like for my requirement if i use
> > DateTime...
>
> I truly thought that you might have attempted to try the complete
> cut/paste-able program I wrote and made slight changes to fit your
> "requirement". Even if not, at least testing it would have been nice.
>
> The code I gave you earlier is put verbatim below, with additional
> functions.
>
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use DateTime::Format::Strptime;
>
> my $EXTRACTION_LAYOUT = '@16 A1 @40 A9';
>
> VOUCHER_RECORD:
>
> while ( my $record =  ) {
>
>my ( $important_num, $date_string )
>= unpack $EXTRACTION_LAYOUT, $record;
>
>next VOUCHER_RECORD if $important_num != 2;
>
>my $static_date = DateTime->new(
>year => 2009,
>month => 3,
>day => 31
>);
>
>
># turn the extracted date string into a DateTime object
>
>my $date_formatter
>= new DateTime::Format::Strptime( pattern => '%d-%b-%y' );
>
>my $voucher_date
>= $date_formatter->parse_datetime($date_string);
>
>
># compare the DateTime compiled dates
>
>if ( DateTime->compare( $static_date, $voucher_date )) {
>
># - put your record/store functions code in this space.
># - depending on what you want to do, these comments
>#   are within the code block that ensures that your 2nd
>#   field is equal to '2', and that the 5th field is less
>#   than Mar 31, 2009
>
># - depending on how you want to "store" the information,
>#   I might suggest http://perldoc.perl.org/Storable.html
>}
> }
>
> __END__
>
> Please at least copy/paste my code and try it if you've been able to
> install DateTime. Use the example in my earlier post, as it also
> contains the data which is properly formatted.
>
> To be bluntly honest, I'm quite proud of my tiny code snip. I had to
> research/try the patterns for DateTime to get them right, and counted
> with my finger to position the layout properly.
>
> ...I am completely flabbergasted with your response.
>
> See kids, the post I am responding to is not proper list etiquette!
>
> Steve
>


Re: script to compare dates

2009-08-18 Thread Steve Bertrand
Mihir Kamdar wrote:
> thanks all.and how will be the code like for my requirement if i use
> DateTime...

I truly thought that you might have attempted to try the complete
cut/paste-able program I wrote and made slight changes to fit your
"requirement". Even if not, at least testing it would have been nice.

The code I gave you earlier is put verbatim below, with additional
functions.



#!/usr/bin/perl

use strict;
use warnings;

use DateTime::Format::Strptime;

my $EXTRACTION_LAYOUT = '@16 A1 @40 A9';

VOUCHER_RECORD:

while ( my $record =  ) {

my ( $important_num, $date_string )
= unpack $EXTRACTION_LAYOUT, $record;

next VOUCHER_RECORD if $important_num != 2;

my $static_date = DateTime->new(
year => 2009,
month => 3,
day => 31
);


# turn the extracted date string into a DateTime object

my $date_formatter
= new DateTime::Format::Strptime( pattern => '%d-%b-%y' );

my $voucher_date
= $date_formatter->parse_datetime($date_string);


# compare the DateTime compiled dates

if ( DateTime->compare( $static_date, $voucher_date )) {

# - put your record/store functions code in this space.
# - depending on what you want to do, these comments
#   are within the code block that ensures that your 2nd
#   field is equal to '2', and that the 5th field is less
#   than Mar 31, 2009

# - depending on how you want to "store" the information,
#   I might suggest http://perldoc.perl.org/Storable.html
}
}

__END__

Please at least copy/paste my code and try it if you've been able to
install DateTime. Use the example in my earlier post, as it also
contains the data which is properly formatted.

To be bluntly honest, I'm quite proud of my tiny code snip. I had to
research/try the patterns for DateTime to get them right, and counted
with my finger to position the layout properly.

...I am completely flabbergasted with your response.

See kids, the post I am responding to is not proper list etiquette!

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: script to compare dates

2009-08-18 Thread Mihir Kamdar
thanks all.and how will be the code like for my requirement if i use
DateTime...

On Tue, Aug 18, 2009 at 10:09 AM, Shawn H. Corey wrote:

>  Steve Bertrand wrote:
>
>> Mihir Kamdar wrote:
>>
>>> I dont have Datetime module installed. Is there a way without using
>>> DateTime??
>>>
>>
>> As always, TIMTOWTDI, but I have to maintain a lot of legacy code, where
>> the original author (including myself) would rewrite date routines
>> (differently) in each subroutine that needed to parse/manipulate dates.
>>
>> I would highly advise that you get DateTime installed at any cost. It is
>> consistent, extremely flexible, and very well maintained and documented.
>>  There is also a spectacular number of different formatting modules as
>> well that make all tasks involving dates easy and enjoyable.
>>
>
> Or even better, get the date format changed to Système International (SI),
> which is, 4-digit year, 2-digit month, 2-digit day.  I thought everyone
> fixed this problem in Y2K.
>
>
> --
> Just my 0.0002 million dollars worth,
>  Shawn
>
> Programming is as much about organization and communication
> as it is about coding.
>
> I like Perl; it's the only language where you can bless your
> thingy.
>


RE: script to compare dates

2009-08-18 Thread Wagner, David --- Senior Programmer Analyst --- CFS
> -Original Message-
> From: Mihir Kamdar [mailto:kamdarmihi...@gmail.com] 
> Sent: Tuesday, August 18, 2009 08:55
> To: Steve Bertrand
> Cc: beginners
> Subject: Re: script to compare dates
> 
> I dont have Datetime module installed. Is there a way without using
> DateTime??
> 
You don't need it. You can with a little work generate what you
need. I don't have access to an open setup, so here is a shot at using
what you already have in a normal load. You will need to modify and
possibly add some checks, but all doable using std libs:

#!/usr/bin/perl

use strict;
use warnings;

use Time::Local 'timelocal';

my %MonthNameToNbr = qw( JAN 0 FEB 1 MAR 2 APR 3 MAY 4 JUN 5 JUL 6
AUG 7 SEP 8 OCT 9 NOV 10 DEC 11);

my $checkdateinsecs = timelocal(0, 0, 0, 31,
$MonthNameToNbr{q[mar]}, 109);   # generate check date once

printf "checkdateinsecs: %d\n",
$checkdateinsecs;
my $day;
my $month;
my $year;
my @cdr = ();

while (  ) {
chomp;
next if ( /^\s*$/ );
@cdr = split(/\s*,\s*/, $_);

if ( $cdr[0] !~ /^.(\d+)\S(\S{3})\S(\d+)/ ) {
 #print some type of error and either continue or die
 printf "(%5d)in error:\n<%s>\n",
$.,
$_;
 next;
 }
$day   = $1;
$month = uc($2);
$year  = $3;
my $dateinsecs = timelocal(0, 0, 0, $day,
$MonthNameToNbr{$month}, $year+100);

next if ( ! ( ($dateinsecs > $checkdateinsecs ) and
$cdr[1] =~ /2.*/
)
 );
# do your other processing
printf "dateinsecs: %d was greater (%d)\n",
$dateinsecs,
$.;
 }
__DATA__
*04-NOV-08*,2
*04-apr-09*,3
*xx-dec-08*,2
*04-apr-09*,2

Output:
[C:/CurrWrka/00Commonprlprod/src] aapl013s
checkdateinsecs: 1233385200
(3)in error:
<*xx-dec-08*,2>
dateinsecs: 1238824800 was greater (4)


> On Tue, Aug 18, 2009 at 7:37 AM, Steve Bertrand 
>  wrote:
> 
> >  Mihir Kamdar wrote:
> > > Hi,
> > >
> > > I want to write a script whose input data would be a csv 
> file and records
> > > would be as follows:-
> > >
> > > 60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*
> > > ,1,INR,,VOUCHER_BATCH_20080919_00014,2C,,0
> > > 326495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*
> > > ,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> > > 327480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*
> > > ,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> > >
> > > Here I want to compare whether the 5th field, which is 
> the date field
> > > is *earlier
> > > than 31-Mar-09 and 2nd field value is 2.*
> > >
> > > If yes, then I will take that record and store it in another file.
> > >
> > > Please help me as to how do I compare dates, preferably 
> with some sample
> > > code.
> > >
> > > I started coding for this as below, but am stuck on how 
> to compare date
> > in
> > > my input with another date.
> > >
> > > #!/usr/bin/perl
> > > use strict;
> > > use warnings ;
> > >
> > > open (my $IN_FILE,"<","testdata.txt") or die $!." file 
> not found" ;
> > > while (my $line=readline($IN_FILE))
> > > {
> > > my @cdr=split (/,/, $line) ;
> > > if($cdr[5]
> > > .
> > > .
> > > }
> >
> > For fun, and to try out a few things I came across in 
> Damian's book. I
> > use DateTime to manage all aspects of dates and times. The 
> code below
> > assumes that all records will always be in the exact same format.
> > Criticism welcome:
> >
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > use DateTime::Format::Strptime;
> >
> > my $EXTRACTION_LAYOUT = '@16 A1 @40 A9';
> >
> > VOUCHER_RECORD:
> >
> > while ( my $record =  ) {
> >
> >my ( $important_num, $date_string )
> >= unpack $EXTRACTION_LAYOUT, $record;
> >
> >next VOUCHER_RECORD if $important_num != 2;
> >
> >my $static_date = DateTime->new(
> >year => 2009,
> >month => 3,
> >day => 31
> >);
> >
> >
> ># turn the extracted date str

Re: script to compare dates

2009-08-18 Thread Shawn H. Corey

Steve Bertrand wrote:

Mihir Kamdar wrote:

I dont have Datetime module installed. Is there a way without using
DateTime??


As always, TIMTOWTDI, but I have to maintain a lot of legacy code, where
the original author (including myself) would rewrite date routines
(differently) in each subroutine that needed to parse/manipulate dates.

I would highly advise that you get DateTime installed at any cost. It is
consistent, extremely flexible, and very well maintained and documented.
 There is also a spectacular number of different formatting modules as
well that make all tasks involving dates easy and enjoyable.


Or even better, get the date format changed to Système International 
(SI), which is, 4-digit year, 2-digit month, 2-digit day.  I thought 
everyone fixed this problem in Y2K.



--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: script to compare dates

2009-08-18 Thread Steve Bertrand
Mihir Kamdar wrote:
> I dont have Datetime module installed. Is there a way without using
> DateTime??

As always, TIMTOWTDI, but I have to maintain a lot of legacy code, where
the original author (including myself) would rewrite date routines
(differently) in each subroutine that needed to parse/manipulate dates.

I would highly advise that you get DateTime installed at any cost. It is
consistent, extremely flexible, and very well maintained and documented.
 There is also a spectacular number of different formatting modules as
well that make all tasks involving dates easy and enjoyable.

Steve


smime.p7s
Description: S/MIME Cryptographic Signature


Re: script to compare dates

2009-08-18 Thread Telemachus
On Tue Aug 18 2009 @  7:55, Mihir Kamdar wrote:
> I dont have Datetime module installed. Is there a way without using
> DateTime??
I don't want to be glib, but I can see at least two broad possible options:

(1) Install DateTime (it's very worth it).
(2) Rewrite all the relevant code from DateTime (it's very not worth it,
except perhaps as a learning exercise or masochism).

More seriously getting date arithmetic right can be very hard. Your
particular need doesn't sound too complicated now (compare two dates to a
fixed date), but I still wouldn't choose to recreate a solution for date
parsing and comparison when there are good solutions easily available at
CPAN.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: script to compare dates

2009-08-18 Thread Mihir Kamdar
I dont have Datetime module installed. Is there a way without using
DateTime??

On Tue, Aug 18, 2009 at 7:37 AM, Steve Bertrand  wrote:

>  Mihir Kamdar wrote:
> > Hi,
> >
> > I want to write a script whose input data would be a csv file and records
> > would be as follows:-
> >
> > 60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*
> > ,1,INR,,VOUCHER_BATCH_20080919_00014,2C,,0
> > 326495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*
> > ,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> > 327480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*
> > ,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> >
> > Here I want to compare whether the 5th field, which is the date field
> > is *earlier
> > than 31-Mar-09 and 2nd field value is 2.*
> >
> > If yes, then I will take that record and store it in another file.
> >
> > Please help me as to how do I compare dates, preferably with some sample
> > code.
> >
> > I started coding for this as below, but am stuck on how to compare date
> in
> > my input with another date.
> >
> > #!/usr/bin/perl
> > use strict;
> > use warnings ;
> >
> > open (my $IN_FILE,"<","testdata.txt") or die $!." file not found" ;
> > while (my $line=readline($IN_FILE))
> > {
> > my @cdr=split (/,/, $line) ;
> > if($cdr[5]
> > .
> > .
> > }
>
> For fun, and to try out a few things I came across in Damian's book. I
> use DateTime to manage all aspects of dates and times. The code below
> assumes that all records will always be in the exact same format.
> Criticism welcome:
>
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use DateTime::Format::Strptime;
>
> my $EXTRACTION_LAYOUT = '@16 A1 @40 A9';
>
> VOUCHER_RECORD:
>
> while ( my $record =  ) {
>
>my ( $important_num, $date_string )
>= unpack $EXTRACTION_LAYOUT, $record;
>
>next VOUCHER_RECORD if $important_num != 2;
>
>my $static_date = DateTime->new(
>year => 2009,
>month => 3,
>day => 31
>);
>
>
># turn the extracted date string into a DateTime object
>
>my $date_formatter
>= new DateTime::Format::Strptime( pattern => '%d-%b-%y' );
>
>my $voucher_date
>= $date_formatter->parse_datetime($date_string);
>
>
># compare the DateTime compiled dates
>
>if ( DateTime->compare( $static_date, $voucher_date )) {
>
>print "Voucher ${voucher_date} is prior to ${static_date}" .
>  " and the important number is 2\n";
>}
> }
>
> __DATA__
>
> 60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*,1,INR,,VOUCHER_BATCH_20080919_00014,2C,,0
>
> 326495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
>
> 327480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
>
> Steve
>


Re: script to compare dates

2009-08-18 Thread Steve Bertrand
Mihir Kamdar wrote:
> Hi,
> 
> I want to write a script whose input data would be a csv file and records
> would be as follows:-
> 
> 60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*
> ,1,INR,,VOUCHER_BATCH_20080919_00014,2C,,0
> 326495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*
> ,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> 327480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*
> ,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
> 
> Here I want to compare whether the 5th field, which is the date field
> is *earlier
> than 31-Mar-09 and 2nd field value is 2.*
> 
> If yes, then I will take that record and store it in another file.
> 
> Please help me as to how do I compare dates, preferably with some sample
> code.
> 
> I started coding for this as below, but am stuck on how to compare date in
> my input with another date.
> 
> #!/usr/bin/perl
> use strict;
> use warnings ;
> 
> open (my $IN_FILE,"<","testdata.txt") or die $!." file not found" ;
> while (my $line=readline($IN_FILE))
> {
> my @cdr=split (/,/, $line) ;
> if($cdr[5]
> .
> .
> }

For fun, and to try out a few things I came across in Damian's book. I
use DateTime to manage all aspects of dates and times. The code below
assumes that all records will always be in the exact same format.
Criticism welcome:


#!/usr/bin/perl

use strict;
use warnings;

use DateTime::Format::Strptime;

my $EXTRACTION_LAYOUT = '@16 A1 @40 A9';

VOUCHER_RECORD:

while ( my $record =  ) {

my ( $important_num, $date_string )
= unpack $EXTRACTION_LAYOUT, $record;

next VOUCHER_RECORD if $important_num != 2;

my $static_date = DateTime->new(
year => 2009,
month => 3,
day => 31
);


# turn the extracted date string into a DateTime object

my $date_formatter
= new DateTime::Format::Strptime( pattern => '%d-%b-%y' );

my $voucher_date
= $date_formatter->parse_datetime($date_string);


# compare the DateTime compiled dates

if ( DateTime->compare( $static_date, $voucher_date )) {

print "Voucher ${voucher_date} is prior to ${static_date}" .
  " and the important number is 2\n";
}
}

__DATA__
60020003076570*,2,*20-SEP-08.01:09:18,,*04-NOV-08*,1,INR,,VOUCHER_BATCH_20080919_00014,2C,,0
326495*,5,*20-SEP-08.01:09:57,,*31-DEC-09*,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0
327480,*2,*20-SEP-08.01:09:57,,*31-DEC-08*,10,INR,,VOUCHER_BATCH_20080919_00024,1K,,0

Steve


smime.p7s
Description: S/MIME Cryptographic Signature