FAQ
Hi ,
I have written a PERL script which asks user to key in date and then it
does some calculations in that date and stores it in different
variables.
The objective of the script is to make changes at certain places in a
UNIX file whenever you run this file.Basically it replaces the existing
dates in the UNIX file and substitute it with the new calculated dates.

Now the problem is how do i input the dates from the user?
Because right now i have just defined a simple variable $input to get
the dates.
so if user keys in 20010731 and if i add 7 to it it becomes 20010738
instead of going into the month of August.
Is there a way to define a date variable in PERL.I am a beginner in PERL
so dont know how to do it.
Can somebody please help me out?
Thanks
Rishi Kabra

Search Discussions

  • Prakash Kailasa at Aug 2, 2001 at 1:34 pm

    Rishi Kabra wrote:

    Hi ,
    I have written a PERL script which asks user to key in date and then it
    does some calculations in that date and stores it in different
    variables.
    The objective of the script is to make changes at certain places in a
    UNIX file whenever you run this file.Basically it replaces the existing
    dates in the UNIX file and substitute it with the new calculated dates.

    Now the problem is how do i input the dates from the user?
    Because right now i have just defined a simple variable $input to get
    the dates.
    so if user keys in 20010731 and if i add 7 to it it becomes 20010738
    instead of going into the month of August.
    Is there a way to define a date variable in PERL.I am a beginner in PERL
    so dont know how to do it.
    Can somebody please help me out?
    Thanks
    Rishi Kabra
    There are several modules on the CPAN (Comprehensive Perl Archive
    Network)
    to help you deal with dates (and times). Two of them are Date::Calc and
    Date::Manip. You can look at their documentation on search.cpan.org at
    these locations.

    http://search.cpan.org/doc/STBEY/Date-Calc-4.3/Calc.pm
    http://search.cpan.org/doc/SBECK/DateManip-5.40/Manip.pod

    http://search.cpan.org/search?mode=module&query=Date will show you many
    more.

    If you are beginner in perl, then you are better off wandering to the
    perl-beginners list (http://lists.perl.org/showlist.cgi?name=beginners).
    This list is geared towards module development and related issues.

    HTH,
    /prakash
  • Jonathan Scott Duff at Aug 2, 2001 at 2:20 pm

    On Wed, Aug 01, 2001 at 10:47:49AM -0700, Rishi Kabra wrote:
    Because right now i have just defined a simple variable $input to get
    the dates. so if user keys in 20010731 and if i add 7 to it it becomes
    20010738 instead of going into the month of August. Is there a way to
    define a date variable in PERL. I am a beginner in PERL so dont know
    how to do it. Can somebody please help me out?
    If you have a recent Perl, you probably should just use the
    Time::Local module that comes with it. Here's an example:

    use Time::Local 'timelocal_nocheck';

    $input = "20010731";
    ($year,$month,$mday) = $input =~ /^(\d\d\d\d)(\d\d)(\d\d)$/;

    $mday += 7; # add 7 days

    $gmt = timelocal_nocheck(0,0,0,$mday,$month,$year-1900);
    ($year,$month,$mday) = (localtime($gmt))[3,4,5];
    $year += 1900;
    print "$year $month $mday\n";

    The reason I say a recent Perl is that the "timelocal_nocheck" routine
    was a recent addition to the Time::Local module.

    For more information try typing "perldoc Time::Local" and "perldoc -f
    localtime" or "perldoc -f gmtime" at your favorite shell prompt.

    -Scott
    --
    Jonathan Scott Duff
    duff@cbi.tamucc.edu
  • Jonathan Scott Duff at Aug 2, 2001 at 2:25 pm

    On Thu, Aug 02, 2001 at 09:18:07AM -0500, Jonathan Scott Duff wrote:
    ($year,$month,$mday) = (localtime($gmt))[3,4,5];
    Oops, I got the args backwards. That should be

    ($year,$month,$mday) = (localtime($gmt))[5,4,3];

    -Scott
    --
    Jonathan Scott Duff
    duff@cbi.tamucc.edu

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdatetime @
categoriesperl
postedAug 1, '01 at 6:10p
activeAug 2, '01 at 2:25p
posts4
users3
websitemetacpan.org...

People

Translate

site design / logo © 2019 Grokbase