omapi-perl leases Time Format

Heiko Großstück heiko.grossstueck at gmx.net
Tue Mar 20 19:24:09 UTC 2007


Uwe.Buchwitz at gavi.de schrieb:
 > Usinf omapi-perl I am trying to get a report of all my active leases 
including the start time and the end time of the lease. I get an result, 
but the Date and time is not what I expected. Here some examples:
 > 12.03.1907.09.09.41
 > 26.01.1917.16.16.21
 >
 > Its really not that far in the past the clients got their leases.
 >
 > Here part of my code:
 >
 > use OMAPI::DHCP;
 > use MIME::Base64;
 > use Time::localtime;
 >
 >                                 $s =  $$L1{"starts"};
 >                                 $ss = localtime($s);
 >  printf("%02d.%02d.%04d.%02d.%02d.%02d", $ss->mday, $ss->mon+1, 
$ss->year + 1900, $ss->hour, $ss->min, $ss->sec );
 >
 >
 > Has anybody an Idea how to get the correct format??
 >
 >
 > Uwe Buchwitz
 >

Hello Uwe,

I have a small skill in Perl (you may know :) ) so I can't help you to 
solve the problem at a glance.

I hope it's legal for me to post parts of a JAVA code I made for the 
company i was during the last practical term. My supervisor wasn't able 
to get the okay to publish the "OMAPI in Java" project to Open Source... :)

the bytes you get via the net is - convert to an int value - the seconds 
counted from 1 january 1970 0:00. You get a value in the past because 
the old format was an inverted byte order, and the first bit was 1 by 
chance. So the whole timestamp was negativ (counting time backward is a 
feature on Un*x machines. Not working on Windows as far as I know).

In Java it looks like:
protected static String hex2date(byte[] b) {
     long val = Convert.unsignedIntToLong(b);
     Calendar cal = new GregorianCalendar();
     cal.setTimeInMillis(val * 1000);
     DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
     fmt.setTimeZone(new SimpleTimeZone(0, "UTC"));
     return fmt.format(cal.getTime());
}

A hint may be to convert the bytes to a long value and search for a 
feature docoding "timestamps".

Heiko





More information about the dhcp-hackers mailing list