Convert string into incremental date

Discussion in 'Perl Misc' started by djray, May 30, 2006.

  1. djray

    djray Guest

    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
     
    djray, May 30, 2006
    #1
    1. Advertising

  2. 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
     
    it_says_BALLS_on_your forehead, May 30, 2006
    #2
    1. Advertising

  3. djray

    Paul Lalli Guest

    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
     
    Paul Lalli, May 30, 2006
    #3
  4. djray

    Mothra Guest

    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
     
    Mothra, May 31, 2006
    #4
  5. djray

    Guest

    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)
     
    , May 31, 2006
    #5
  6. djray

    DJ Stunks Guest

    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
     
    DJ Stunks, May 31, 2006
    #6
  7. djray

    Guest

    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
     
    , May 31, 2006
    #7
  8. djray

    Guest

    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)
     
    , Jun 1, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,202
    Janaka
    Feb 18, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,360
    Michael Borgwardt
    May 30, 2004
  3. wgan
    Replies:
    7
    Views:
    616
    Roedy Green
    Jul 8, 2004
  4. Replies:
    8
    Views:
    815
    Boris
    May 1, 2007
  5. MrPink
    Replies:
    8
    Views:
    1,639
    MrPink
    Aug 14, 2011
Loading...

Share This Page