Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

getting system time

by akrrs7 (Acolyte)
on Dec 02, 2011 at 15:55 UTC ( [id://941347]=perlquestion: print w/replies, xml ) Need Help??

akrrs7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm using the following code:
(my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday, my + $yday, my $isdst ) = localtime(time); my $adjustedYear = $year + 1900; my $adjustedMon = $mon + 1; my $timestamp = sprintf( "%4.4d%2.2d%2.2d%2.2d%2.2d_%d", $adjustedYear, $adjustedMon, $mday, $hour, $min, $$ );
this works fine...except when I use
use Time::localtime;
Then I get 1900.... Doesn't make sense Thanks !!!

Replies are listed 'Best First'.
Re: getting system time
by choroba (Cardinal) on Dec 02, 2011 at 16:29 UTC
    Instead of
    (my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday, my + $yday, my $isdst ) = localtime(time);
    (which can be written as
    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = l +ocaltime(time);
    ) under Time::localtime you should use
    my $localtime = localtime(time); my $sec = $localtime->sec; my $min = $localtime->min;
    etc.
Re: getting system time
by TJPride (Pilgrim) on Dec 02, 2011 at 16:25 UTC
    For whatever reason, it changes the return value from localtime() from an array to an array ref. Change to this instead:

    (my $sec, my $min, my $hour, my $mday, my $mon, my $year, my $wday, my $yday, my $isdst ) = @{localtime()};

    There are probably better ways to manage dates, but I don't have the patience to explain at the moment.

      The reason, according to perldoc Time::localtime, is that the module replaces the core localtime function which returns an array with one that returns a Time::tm object instead. So you're expected to use the object's methods to get at its values. In this case, since the object is a blessed array reference, treating it as an ordinary array reference also works, but it probably isn't safe to assume it always will.

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.

Re: getting system time
by Anonymous Monk on Dec 02, 2011 at 16:12 UTC

    Doctor Doctor it hurts when I do this, what should I do?

    Don’t do it if it hurts

      I need to use
      use Time::localtime;
      because later in the script, I am using
      $dateModified = ctime($attrs->mtime);
      to get the timestamp of a file.

      And if don't have the 'use Time' defined, then I get a 'Undefined subroutine &main::ctime called' error

      So not just a question of If it hurts don't do it

        Try not importing stuff from the module, i.e.

        use Time::localtime (); # note the parentheses

        and then call the function fully qualified

        $dateModified = Time::localtime::ctime($attrs->mtime);

        (Or import only ctime, which should not clash.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://941347]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-24 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found