Scripting Challenge Beginner 5 - working with dates.

This is a multi-part message in MIME format.
--===============1558578940==
Content-type: multipart/alternative;
boundary=------------080908000005060002060902

This is a multi-part message in MIME format.
--------------080908000005060002060902
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

%$# [at] !

Beginner Challenge 5 - at the contest!
my script b5.pl
solution script sb5.pl

D:\My Documents\perl scripts\Challenge>b5.pl February 21, 2009
Days: 367
Months: 12
Months/Days: 12 / 1

D:\My Documents\perl scripts\Challenge>sb5.pl February 21, 2009
Date::Calc::Delta_Days(): not a valid date at D:\My Documents\perl
scripts\Challenge\sb5.pl line 8.

D:\My Documents\perl scripts\Challenge>sb5.pl 2/21/2009
Days: 367
Months: 12
Months/Days: 12 / 1
D:\My Documents\perl scripts\Challenge>

Quote from the contest!"English (United States) regional and language
option settings, and we will always enter dates using a string value
similar to this: March 3, 2008"

But the solution fails if you enter a string date, like March 3, 2008.

Its likely my solution is wrong especially if today is like the 31st
cause I had to fudge not passing the target day / when I hit the first
before I hit the day of month. But the solution provided fails long
before that and we have no test data so I can not see a successful
solution and where I broke down. I would love to see the test data they
used! I understand they can't give back everyone feedback.

Sorry about my venting. Would anyone care to comment on my failed script?
I could not find the rules, so I have been using just the default
ActivePerl install, and all scripts so far have been doable, even the
read from a database, which I'm not to happy with the documentation that
keeps saying you must use the DATASOURCES ( ODBC) panel to associate
the database with the driver. This is totally bad, I did find the info
I wanted so I could create the DSN in the script thank GOODNESS. OOPS,
get of my soap box.

Sorry about the lack of comments. s variables are the starting date, ie
today that I advance to equal the e variables "entered date".
#!/perl
use warnings;
use strict;
use ActiveState::DateTime qw(is_leap_year days_in_month check_date
month_name_short month_name_long gmt_offset);
my [at] months = qw(January February March April May June July August
September October November December);
my ($ss,$sm,$sh,$smday,$smon,$syear,$swday,$syday,$sisdst) =
localtime(time);
$syear+=1900;
my $em = shift or badinput();
badinput() unless join(" ", [at] months) =~ /$em/i;
my $ed = shift or badinput();
$ed =~ tr/,//d;
my $ey = shift or badinput();
my $emc=0;
foreach ( [at] months ){
$emc++;
last if $months[$emc] eq $em;
}
my ($mm,$md,$cd,$cm);
$mm=$md=$cd=$cm=0;
my $loop=1;
my $max_days = days_in_month($syear, $smon+1);
my $mdtrigger=$smday;
while($emc!=$smon || $ed!=$smday || $ey!=$syear){
if ($smday+1 > $max_days) {
if ( $smon == 11 ) {
$syear++;
$smon=0;
} else {
$smon++;
}
$max_days = days_in_month($syear, $smon+1);
$smday=0;
$cm++;
}
$cd++;
$smday++;
if ($smday == $mdtrigger || $mdtrigger > days_in_month($syear,
$smon==0?12:$smon) && $smday == 1){
$mm++;
$md=-1;
}
$md++;
}

print "Days: $cd\n";
print "Months: $cm\n";
print "Months/Days: $mm / $md\n";

sub badinput{
die "Expected a date of the format $months[$smon] $smday, $syear, to
be entered on the command line.\n";
}


--------------080908000005060002060902
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
%$# [at] !<br>
<br>
Beginner Challenge 5 - at the contest!<br>
my script b5.pl<br>
solution script sb5.pl<br>
<br>
D:\My Documents\perl scripts\Challenge>b5.pl February 21, 2009<br>
Days: 367<br>
Months: 12<br>
Months/Days: 12 / 1<br>
<br>
D:\My Documents\perl scripts\Challenge>sb5.pl February 21, 2009<br>
Date::Calc::Delta_Days(): not a valid date at D:\My Documents\perl
scripts\Challenge\sb5.pl line 8.<br>
<br>
D:\My Documents\perl scripts\Challenge>sb5.pl 2/21/2009<br>
Days: 367<br>
Months: 12<br>
Months/Days: 12 / 1<br>
D:\My Documents\perl scripts\Challenge><br>
<br>
Quote from the contest!"<b>English (</b><b>United States</b><b>)</b>
regional and language option settings, and we will always enter dates
using a string value similar to this: March 3, 2008"<br>
<br>
But the solution fails if you enter a string date, like March 3, 2008.<br>
<br>
Its likely my solution is wrong especially if today is like the 31st
cause I had to fudge not passing the target day / when I hit the first
before I hit the day of month.  But the solution provided fails long
before that and we have no test data so I can not see a successful
solution and where I broke down.  I would love to see the test data
they used!  I understand they can't give back everyone feedback.<br>
<br>
Sorry about my venting.  Would anyone care to comment on my failed
script?<br>
I could not find the rules, so I have been using just the default
ActivePerl install, and all scripts so far have been doable, even the
read from a database, which I'm not to happy with the documentation
that keeps saying you must use the DATASOURCES ( ODBC)  panel to
associate the database with the driver.  This is totally bad, I did
find the info I wanted so I could create the DSN in the script thank
GOODNESS.  OOPS, get of my soap box.<br>
<br>
Sorry about the lack of comments.  s variables are the starting date,
ie today that I advance to equal the e variables "entered date".<br>
#!/perl<br>
use warnings;<br>
use strict;<br>
use ActiveState::DateTime qw(is_leap_year days_in_month check_date
month_name_short month_name_long gmt_offset);<br>
my [at] months = qw(January February March April May June July August
September October November December);<br>
my ($ss,$sm,$sh,$smday,$smon,$syear,$swday,$syday,$sisdst) =
localtime(time);<br>
$syear+=1900;<br>
my $em = shift or badinput();<br>
badinput() unless join(" ", [at] months) =~ /$em/i;<br>
my $ed = shift or badinput();<br>
$ed =~ tr/,//d;<br>
my $ey = shift or badinput();<br>
my $emc=0;<br>
foreach ( [at] months ){<br>
   $emc++;<br>
   last if $months[$emc] eq $em;<br>
}<br>
my ($mm,$md,$cd,$cm);<br>
$mm=$md=$cd=$cm=0;<br>
my $loop=1;<br>
my $max_days = days_in_month($syear, $smon+1);<br>
my $mdtrigger=$smday;<br>
while($emc!=$smon || $ed!=$smday || $ey!=$syear){<br>
   if ($smday+1 > $max_days) {<br>
      if ( $smon == 11 ) {<br>
         $syear++;<br>
         $smon=0;<br>
      } else {<br>
         $smon++;<br>
      }<br>
      $max_days = days_in_month($syear, $smon+1);<br>
      $smday=0; <br>
      $cm++;<br>
   }<br>
   $cd++;<br>
   $smday++;<br>
   if ($smday == $mdtrigger || $mdtrigger > days_in_month($syear,
$smon==0?12:$smon) && $smday == 1){<br>
      $mm++;<br>
      $md=-1;<br>
   }<br>
   $md++;<br>
}<br>
<br>
print "Days: $cd\n";<br>
print "Months: $cm\n";<br>
print "Months/Days: $mm / $md\n";<br>
<br>
sub badinput{<br>
   die "Expected a date of the format $months[$smon] $smday, $syear, to
be entered on the command line.\n";<br>
}<br>
<br>
</body>
</html>

--------------080908000005060002060902--


--===============1558578940==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1558578940==--
bdaoust [ Do, 28 Februar 2008 19:59 ] [ ID #1926731 ]

RE: Scripting Challenge Beginner 5 - working with dates.

Suggest you don't parse the dates yourself. See Regexp::Common::time,
Time::Normalize, Date::Parse, etc.

Eric

________________________________

From: activeperl-bounces [at] listserv.ActiveState.com
[mailto:activeperl-bounces [at] listserv.ActiveState.com] On Behalf Of Basil
A. Daoust
Sent: Thursday, February 28, 2008 1:59 PM
To: activeperl [at] listserv.ActiveState.com
Subject: Scripting Challenge Beginner 5 - working with dates.


%$# [at] !

Beginner Challenge 5 - at the contest!
my script b5.pl
solution script sb5.pl

D:\My Documents\perl scripts\Challenge>b5.pl February 21, 2009
Days: 367
Months: 12
Months/Days: 12 / 1

D:\My Documents\perl scripts\Challenge>sb5.pl February 21, 2009
Date::Calc::Delta_Days(): not a valid date at D:\My Documents\perl
scripts\Challenge\sb5.pl line 8.

D:\My Documents\perl scripts\Challenge>sb5.pl 2/21/2009
Days: 367
Months: 12
Months/Days: 12 / 1
D:\My Documents\perl scripts\Challenge>

Quote from the contest!"English (United States) regional and language
option settings, and we will always enter dates using a string value
similar to this: March 3, 2008"

But the solution fails if you enter a string date, like March 3, 2008.

Its likely my solution is wrong especially if today is like the 31st
cause I had to fudge not passing the target day / when I hit the first
before I hit the day of month. But the solution provided fails long
before that and we have no test data so I can not see a successful
solution and where I broke down. I would love to see the test data they
used! I understand they can't give back everyone feedback.

Sorry about my venting. Would anyone care to comment on my failed
script?
I could not find the rules, so I have been using just the default
ActivePerl install, and all scripts so far have been doable, even the
read from a database, which I'm not to happy with the documentation that
keeps saying you must use the DATASOURCES ( ODBC) panel to associate
the database with the driver. This is totally bad, I did find the info
I wanted so I could create the DSN in the script thank GOODNESS. OOPS,
get of my soap box.

Sorry about the lack of comments. s variables are the starting date, ie
today that I advance to equal the e variables "entered date".
#!/perl
use warnings;
use strict;
use ActiveState::DateTime qw(is_leap_year days_in_month check_date
month_name_short month_name_long gmt_offset);
my [at] months = qw(January February March April May June July August
September October November December);
my ($ss,$sm,$sh,$smday,$smon,$syear,$swday,$syday,$sisdst) =
localtime(time);
$syear+=1900;
my $em = shift or badinput();
badinput() unless join(" ", [at] months) =~ /$em/i;
my $ed = shift or badinput();
$ed =~ tr/,//d;
my $ey = shift or badinput();
my $emc=0;
foreach ( [at] months ){
$emc++;
last if $months[$emc] eq $em;
}
my ($mm,$md,$cd,$cm);
$mm=$md=$cd=$cm=0;
my $loop=1;
my $max_days = days_in_month($syear, $smon+1);
my $mdtrigger=$smday;
while($emc!=$smon || $ed!=$smday || $ey!=$syear){
if ($smday+1 > $max_days) {
if ( $smon == 11 ) {
$syear++;
$smon=0;
} else {
$smon++;
}
$max_days = days_in_month($syear, $smon+1);
$smday=0;
$cm++;
}
$cd++;
$smday++;
if ($smday == $mdtrigger || $mdtrigger > days_in_month($syear,
$smon==0?12:$smon) && $smday == 1){
$mm++;
$md=-1;
}
$md++;
}

print "Days: $cd\n";
print "Months: $cm\n";
print "Months/Days: $mm / $md\n";

sub badinput{
die "Expected a date of the format $months[$smon] $smday, $syear, to
be entered on the command line.\n";
}


_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
eroode [ Do, 28 Februar 2008 20:03 ] [ ID #1926732 ]

Re: Scripting Challenge Beginner 5 - working with dates.

Roode, Eric wrote:

>Suggest you don't parse the dates yourself. See Regexp::Common::time,
>Time::Normalize, Date::Parse, etc.
>
>Eric
>
>
Thanks Eric, but are these default modules? If not they break my first
rule.
All scripts should be build with a default ActivePerl install.

Basil

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
bdaoust [ Do, 28 Februar 2008 21:59 ] [ ID #1926736 ]
Perl » gmane.comp.lang.perl.active-perl » Scripting Challenge Beginner 5 - working with dates.

Vorheriges Thema: 2008 Winter Scripting Games are over now
Nächstes Thema: mod_perl for Perl 5.010