Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Convert string into incremental date (http://www.velocityreviews.com/forums/t898253-convert-string-into-incremental-date.html)

djray 05-30-2006 07:51 PM

Convert string into incremental date
 
My question is two part:
1. I am reading a date from a file in the form mm/dd/yy (i.e.
05/30/06). I need to convert that string into a date.
2. I need to be able to increment that date:
Example:
$date = 05/30/06;
$date = $date + 1; ($date = 05/31/06)
$date = $date + 1; ($date = 06/01/06)

Any help would be greatly appreciated.

-Ray


it_says_BALLS_on_your forehead 05-30-2006 07:58 PM

Re: Convert string into incremental date
 

djray wrote:
> My question is two part:
> 1. I am reading a date from a file in the form mm/dd/yy (i.e.
> 05/30/06). I need to convert that string into a date.
> 2. I need to be able to increment that date:
> Example:
> $date = 05/30/06;
> $date = $date + 1; ($date = 05/31/06)
> $date = $date + 1; ($date = 06/01/06)
>
> Any help would be greatly appreciated.


See the Add_Delta_YMD function.

http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod


Paul Lalli 05-30-2006 08:07 PM

Re: Convert string into incremental date
 
djray wrote:
> My question is two part:
> 1. I am reading a date from a file in the form mm/dd/yy (i.e.
> 05/30/06). I need to convert that string into a date.
> 2. I need to be able to increment that date:
> Example:
> $date = 05/30/06;
> $date = $date + 1; ($date = 05/31/06)
> $date = $date + 1; ($date = 06/01/06)
>
> Any help would be greatly appreciated.


There are probably 10 different modules on CPAN that can help you with
this. Have you looked there yet? http://search.cpan.org search for
"date" and/or "time".

If your data really is that structured, however, you might not need to
bother with a CPAN module. Parse out the three numbers using a regular
expression (see perldoc perlre), convert them to seconds since the
epoch using Time::Local's timelocal() (see perldoc Time::Local), and
add one day (24 * 60 * 60), and convert to a string of your choosing
using POSIX's strftime (see `man strftime`) and localtime() (see
perldoc -f localtime).

(standard warnings about daylight savings time apply...)

Hope this helps,
Paul Lalli


Mothra 05-30-2006 11:06 PM

Re: Convert string into incremental date
 
djray wrote:
> My question is two part:
> 1. I am reading a date from a file in the form mm/dd/yy (i.e.
> 05/30/06). I need to convert that string into a date.
> 2. I need to be able to increment that date:
> Example:
> $date = 05/30/06;
> $date = $date + 1; ($date = 05/31/06)
> $date = $date + 1; ($date = 06/01/06)
>
> Any help would be greatly appreciated.
>
> -Ray


This might get you started :-)

use strict;
use warnings;
use DateTime;
use DateTime::Duration;
use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
pattern => '%m/%d/%y',
time_zone => 'GMT',
);
my $dur = DateTime::Duration->new( days => 1 );

while (<DATA>) {
chomp;
my $dt = $Strp->parse_datetime($_);
print $dt + $dur;
}
__DATA__
05/31/06
06/01/06


I hope this helps

Mothra



usenet@DavidFilmer.com 05-30-2006 11:33 PM

Re: Convert string into incremental date
 
djray wrote:
> My question is two part:
> 1. I am reading a date from a file in the form mm/dd/yy (i.e.
> 05/30/06). I need to convert that string into a date.


Let a module do that for you.

> 2. I need to be able to increment that date:


Let a module do that for you as well.

#!/usr/bin/perl

use Date::Manip;

my $date = "05/30/06";

$date = DateCalc($date, "+ 1 day");
$date = DateCalc($date, "+ 1 day");

print UnixDate($date, "%D"); #display like mm/dd/yy

__END__

--
David Filmer (http://DavidFilmer.com)


DJ Stunks 05-31-2006 12:10 AM

Re: Convert string into incremental date
 

Mothra wrote:
> djray wrote:
> > My question is two part:
> > 1. I am reading a date from a file in the form mm/dd/yy (i.e.
> > 05/30/06). I need to convert that string into a date.
> > 2. I need to be able to increment that date:
> > Example:
> > $date = 05/30/06;
> > $date = $date + 1; ($date = 05/31/06)
> > $date = $date + 1; ($date = 06/01/06)
> >
> > Any help would be greatly appreciated.
> >
> > -Ray

>
> This might get you started :-)
>
> use strict;
> use warnings;
> use DateTime;
> use DateTime::Duration;
> use DateTime::Format::Strptime;
>
> my $Strp = new DateTime::Format::Strptime(
> pattern => '%m/%d/%y',
> time_zone => 'GMT',
> );
> my $dur = DateTime::Duration->new( days => 1 );
>
> while (<DATA>) {
> chomp;
> my $dt = $Strp->parse_datetime($_);
> print $dt + $dur;
> }
> __DATA__
> 05/31/06
> 06/01/06
>
>
> I hope this helps


it helps me!

thanks,
-jp


rjulich@gmail.com 05-31-2006 04:10 PM

Re: Convert string into incremental date
 
Thank you to everyone that helped me out with this. I tried them all,
but David's was the simplest to implement. Thanks again.

-Ray


usenet@DavidFilmer.com 06-01-2006 06:55 AM

Re: Convert string into incremental date
 
rjulich@gmail.com wrote:
> Thank you to everyone that helped me out with this. I tried them all,
> but David's was the simplest to implement.


I'm glad you liked my solution. I must point out, however, that
simplicity (for the user/programmer) often comes at the cost of
efficiency (for the machine), and my solution is probably the least
efficient of those offered. But, unless you're crunching thousands of
dates, it probably doesn't really matter on modern hardware.

--
David Filmer (http://DavidFilmer.com)



All times are GMT. The time now is 09:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.