Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Convert string into incremental date

Reply
Thread Tools

Convert string into incremental date

 
 
djray
Guest
Posts: n/a
 
      05-30-2006
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

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      05-30-2006

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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-30-2006
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

 
Reply With Quote
 
Mothra
Guest
Posts: n/a
 
      05-30-2006
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:uration;
use DateTime::Format::Strptime;

my $Strp = new DateTime::Format::Strptime(
pattern => '%m/%d/%y',
time_zone => 'GMT',
);
my $dur = DateTime:uration->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


 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      05-30-2006
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)

 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      05-31-2006

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:uration;
> use DateTime::Format::Strptime;
>
> my $Strp = new DateTime::Format::Strptime(
> pattern => '%m/%d/%y',
> time_zone => 'GMT',
> );
> my $dur = DateTime:uration->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

 
Reply With Quote
 
rjulich@gmail.com
Guest
Posts: n/a
 
      05-31-2006
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

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      06-01-2006
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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I convert String into Date object MrPink Python 8 08-13-2011 08:05 PM
Convert date recevied as String to date in local time zone deepak_kamath_n@yahoo.co.in C++ 8 05-01-2007 12:26 PM
how can I convert date infomation to a string just includes the date not the time wgan Java 7 07-08-2004 07:08 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57