[SNMP4J] DateAndTime makeCalender improvement

Pim Moerenhout pim.moerenhout at gmail.com
Fri Apr 8 09:18:32 CEST 2016


Hi Frank,

I was using the org.snmp4j.agent.mo.snmp.DateAndTime class in my Snmp4j
agent.I'm using the latest 2.4.2 release. I discovered that an DateTime
encoded OctetString was not parsed correctly, and having a different
Timezone then UTC generated other epoch values then expected.

When I changed the makeCalendar method as follows, it  works:

public static GregorianCalendar makeCalendar(OctetString dateAndTimeValue) {
  GregorianCalendar gc;
  if (dateAndTimeValue.length() == 11) {
    String timezone = String.format("GMT%c%02d:%02d",
(char)(dateAndTimeValue.get(8)),
        dateAndTimeValue.get(9), dateAndTimeValue.get(10));
    gc = new GregorianCalendar(TimeZone.getTimeZone(timezone));
  }
  else{
    // localtime
    gc = new GregorianCalendar();
  }
  int year = (dateAndTimeValue.get(0) & 0xFF ) * 256 +
      (dateAndTimeValue.get(1) & 0xFF );
  int month = (dateAndTimeValue.get(2) & 0xFF );
  int date = (dateAndTimeValue.get(3) & 0xFF );
  int hour = (dateAndTimeValue.get(4) & 0xFF );
  int minute = (dateAndTimeValue.get(5) & 0xFF );
  int second = (dateAndTimeValue.get(6) & 0xFF );
  int deci = (dateAndTimeValue.get(7) & 0xFF );
  gc.set(year, month-1, date, hour, minute, second);
  gc.set(Calendar.MILLISECOND, deci*100);
  return gc;
}

Could you change this?

Regards, Pim



More information about the SNMP4J mailing list