Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Hex, Localtime and strings

by theneil (Novice)
on Sep 26, 2012 at 17:11 UTC ( #995829=perlquestion: print w/ replies, xml ) Need Help??
theneil has asked for the wisdom of the Perl Monks concerning the following question:

Trying to print the time from a substring (time is in Hex):

Why does this work?

print MYFILE scalar(localtime(0x506143ca)); # #Prints: Tue Sep 25 00:40:26 2012 #

But not this:

my $daTime = "0x506143ca"; print MYFILE scalar(localtime($daTime)); # #Prints: The default date - something like Jan1 1969 #
Any help is greatly appreciated! Thanks!

Comment on Hex, Localtime and strings
Select or Download Code
Re: Hex, Localtime and strings
by Fletch (Chancellor) on Sep 26, 2012 at 17:18 UTC

    Because the former is a numeric literal where the 0x prefix means something; the later is a string treated as a number which presumes base10 (which means it gives up when it sees a non-digit "x" and is numerically 0). You need to use oct to get the string value treated like it would be as a numeric literal.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Hex, Localtime and strings
by davido (Archbishop) on Sep 26, 2012 at 17:18 UTC

    Because the first example uses a hex representation of an integer, and the second example uses a string of characters that happen to include a 0x and some stuff that looks (to a human) hex-ish (but not to Perl). Perl converts that string to a number following Perl's numerification of a string rules, which in this case results in a '0'.

    use strict; use warnings; use diagnostics; my $daTime = "0x506143ca"; print scalar localtime $daTime, "\n";

    ...the output...

    Argument "0x506143ca" isn't numeric in localtime at mytest.pl line 10 +(#1) (W numeric) The indicated string was fed as an argument to an oper +ator that expected a numeric value instead. If you're fortunate the me +ssage will identify which operator was so unfortunate. Wed Dec 31 16:00:00 1969

    As for the "Wed Dec 31 16:00:00 1969": perl -E 'say localtime 0' will yield the same result.


    Dave

      Thank you guys! Worked like a charm! I'm slowly learning :)

        I know this is late, but I'll share what worked for me!

        $hexval = hex '51C07405'; print scalar(localtime($hexval));
Re: Hex, Localtime and strings
by toolic (Chancellor) on Sep 26, 2012 at 17:31 UTC
    You can also use hex:
    print scalar(localtime(hex($daTime)));
Re: Hex, Localtime and strings
by sundialsvc4 (Abbot) on Sep 26, 2012 at 18:09 UTC

    Now might also be a good time to look at, for example, Date::Calc::Object, and the many other date/time routines available on CPAN.

    I particularly like this one because, as the name implies, it introduces the notion of a “date/time object.”   Which just turns out to be an extremely useful notion to have.   A date value, regardless of how you introduced it to the system, becomes “a thing” that you can query and manipulate and “do things with” in a very natural way.

    Every kind of date/time manipulation can be found somewhere in CPAN ... even for archaic calendars.   (It’s great fun to go poking around in there to see what you find.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-12-19 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (70 votes), past polls