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

What I'm trying to do is this:
#!c:\Perl\bin\perl use SNMP; my $s = new SNMP::Session(DestHost => 'localhost', Community => 'publ +ic', Version => '2'); # gets an snmp table and returns a hashref in $j my $j = $s->gettable('hrSWInstalledTable'); # This statement returns the number of keys in the hashref. $numOfKeys = scalar keys %$j; # print out all of the values in the table for($key = 1; $key <= $numOfKeys; $key++) { print "Key = $key - 'hrSWInstalledIndex' = $$j{$key}{'hrSWInstall +edIndex'}\n"; print "Key = $key - 'hrSWInstalledName' = $$j{$key}{'hrSWInstall +edName'}\n"; print "Key = $key - 'hrSWInstalledID' = $$j{$key}{'hrSWInstall +edID'}\n"; print "Key = $key - 'hrSWInstalledType' = $$j{$key}{'hrSWInstall +edType'}\n"; print "Key = $key - 'hrSWInstalledDate' = $$j{$key}{'hrSWInstall +edDate'}\n"; }
However, the hrSWInstalledDate doesn't print correctly as you can see in the output below.

C:\perlprograms>perl snmp_test.pl
Key = 1 - 'hrSWInstalledIndex' = 1
Key = 1 - 'hrSWInstalledName' = Emaga4 Game Resources 1.0
Key = 1 - 'hrSWInstalledID' = zeroDotZero
Key = 1 - 'hrSWInstalledType' = 4
Key = 1 - 'hrSWInstalledDate' = ╓ ►"
Key = 2 - 'hrSWInstalledIndex' = 2
Key = 2 - 'hrSWInstalledName' = 3DGPAi1 Game Resources 1.0
Key = 2 - 'hrSWInstalledID' = zeroDotZero
Key = 2 - 'hrSWInstalledType' = 4
Key = 2 - 'hrSWInstalledDate' = ╓ ♥↕+*

I need some way to convert the value returned for hrSWInstalledDate to a human readable and printable format. Any help is greatly appreciated.
Thanks
perlchild

Replies are listed 'Best First'.
Re: Convert Octet String DateAndTime to printable string
by GrandFather (Saint) on Apr 27, 2007 at 23:34 UTC

    The documentation for the encoding may be found at http://net-snmp.sourceforge.net/docs/mibs/host.html#DateAndTime. In fact there may be 8 or 11 octets depending on whether time zone information is included or not. The information you need to decode the fields is:

    A date-time specification. field octets contents range ----- ------ -------- ----- 1 1-2 year* 0..65536 2 3 month 1..12 3 4 day 1..31 4 5 hour 0..23 5 6 minutes 0..59 6 7 seconds 0..60 (use 60 for leap-second) 7 8 deci-seconds 0..9 8 9 direction from UTC '+' / '-' 9 10 hours from UTC* 0..13 10 11 minutes from UTC 0..59 * Notes: - the value of year is in network-byte order - daylight saving time in New Zealand is +13 For example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be displayed as: 1992-5-26,13:30:15.0,-4:0 Note that if only local time is known, then timezone information (fields 8-10) is not present.

    DWIM is Perl's answer to Gödel
Re: Convert Octet String DateAndTime to printable string
by rhesa (Vicar) on Apr 28, 2007 at 00:30 UTC
    I believe the following will unpack your bytestring into the date and time components as described in Grandfather's reply:
    @date = unpack 'n C6 a C2', $octets;
    Suitable printf formats would be:
    $datetime = sprintf "%04d-%02d-%02d %02d:%02d:%02d", @date; # no time +zone $datetime = sprintf "%04d-%02d-%02d %02d:%02d:%02d.%d%s%02d:%02d", @da +te; # with tz info
    I didn't manage to coax the displayed text into sensible values though, so the pack format might be wrong.

    Can you give us the output of print join '-', unpack "C*", $octets; if it didn't work out?

      Thanks rhesa, that worked just like I wanted! Thanks for everyone's reply also. This is the best site I've come across in a long time. Good job!
        Hi, I am wokring on the same problem. Could you help me also. my contact is kevin.zhang@mblox.com
      Hi
      One of the varbind of snmptrap carrying date and time in Octet String. When i am trying to ocnvert it into string i got value that is not macthing the data and time. "DateAndTime (Octet String) (SIZE(8|11))"

      Here is my code

      $octets="D8 06 01 01 27 3B 00 "; @date = unpack 'n C6 a C2', $octets; $datetime = sprintf "%04d-%02d-%02d %02d:%02d:%02d", @date; # no time zone print $datetime $datetime = sprintf "%04d-%02d-%02d %02d:%02d:%02d.%d%s%02d:%02d", @date; # with tz info ~ perl tt2.pl 17464-32-48 54:32:48
      Hi
      One of the varbind of snmptrap carrying date and time in Octet String. When i am trying to ocnvert it into string i got value that is not macthing the data and time. "DateAndTime (Octet String) (SIZE(8|11))"

      Here is my code

      $octets="D8 06 01 01 27 3B 00 ";

      @date = unpack 'n C6 a C2', $octets;

      $datetime = sprintf "%04d-%02d-%02d %02d:%02d:%02d", @date; # no time zone

      print $datetime

      $datetime = sprintf "%04d-%02d-%02d %02d:%02d:%02d.%d%s%02d:%02d", @date; # with tz info
      ~
      perl tt2.pl

      17464-32-48 54:32:48
      My email id:umkamath@in.ibm.com
Re: Convert Octet String DateAndTime to printable string
by Herkum (Parson) on Apr 27, 2007 at 23:24 UTC

    The best thing you can do for looking at an arbitrary data structure is use Data::Dump or Data::Dumper

    use Data::Dump qw(dump); warn "Dump " . dump( $j );

    This should do what you want to do.