知识大全 手动解析snmp的dateAndTime类型

Posted 字节

篇首语:百日连阴雨,总有一朝晴。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 手动解析snmp的dateAndTime类型相关的知识,希望对你有一定的参考价值。

手动解析snmp的dateAndTime类型  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  DateAndTime是Snmpv 中的一种数据类型 它主要提供了对日期时间的描述 我在使用Snmp j开发包时 发现其不提供对DateAndTime类型的支持 而有时又需要用到该类型 于是 自己写了一个方法解析该类型 可以实现对DateAndTime类型的格式化 并且可以方便地提取各时间项

  下面是RFC中对DateAndTime的定义

  DateAndTime ::= OCTET STRING (SIZE ( | ))

   A date time specification for the local time of day

   This data type is intended to provide a consistent

   method of reporting date information

  

   field octets contents range

   _____ ______ ________ _____

   year

   (in neork byte order)

   month

   day

   hour

   minutes

   seconds

   (use for leap second)

   deci seconds

   direction from UTC + /

   (in ascii notation)

   hours from UTC

   minutes from UTC

  

   Note that if only local time is known then

   timezone information (fields ) is not present

  由定义可见 DateAndTime仍然是OCTET STRING类型的数据 只是对每个字节进行了具体的定义 比如前两个字节表示年 第五个字节表示小时等 所以如果某个开发包中没有DateAndTime类型 那么大可以用Octet类型去代替 但是这样做得到的只是一些无意义的乱码而已 因此 实现的关键就在于按照定义对特殊的Octet数据进行正确的解析

  既然DateAndTime也是Octet String 那么我们就可以按照字节数组来处理他

  import java util Vector;

  import apache log j Logger;

  import snmp j CommandResponderEvent;

  import snmp j PDU;

  import snmp j smi OctetString;

  import snmp j smi Variable;

  import snmp j smi VariableBinding;

  import diation snmp SNMPManager;

  import diation util INMSLog;

  public class SNMPAdapter  extends SNMPManager

  private Logger adapterlog = INMSLog getInstance() getAdapterLog()

  private Logger recvlog = INMSLog getInstance() getReceiveLog()

  public SNMPAdapter(String snmpIP String snmpPort)

  super(snmpIP snmpPort)

  

  @Override

  protected void notifyMsg(CommandResponderEvent event)

  ( Mapper::notifyMsg》    =====NOTIFY_ALarm===== )

  PDU pdu = event getPDU()

  Vector vec = pdu getVariableBindings()

  ( YYWifiMapper::process》    oid size = + vec size())

  for (int i = ; i < vec size() i++)

  String oidNameValue = ;

  VariableBinding vb = (VariableBinding)vec get(i)

  if (vb getVariable() instanceof OctetString)

  String value = new String(((OctetString)vb getVariable()) getValue())

  if(vb getOid() toString() trim() equals( ) )

  oidNameValue = OID: +vb getOid()+ \\t + value: + parseDateAndTime(vb getVariable())

  

  else

  oidNameValue = OID: +vb getOid()+ \\t + value: + value trim()

  

  

  else

  //略

  

  

  

  public String parseDateAndTime(Variable v)

  ( YYWifiMapper::parseDateAndTime》    v= +v)

  //        ( YYWifiMapper::parseDateAndTime》    v string= +v toString())

  OctetString oct = (OctetString)v;

  //        ( YYWifiMapper::parseDateAndTime》    v hex= + oct toHexString())

  byte[] bts = oct getValue()

  byte[] format_str = new byte[ ];   //保存格式化过后的时间字符串

  int year;

  int month;

  int day;

  int hour;

  int minute;

  int second;

  int msecond;

  //        for(byte b:bts)

  //            ( YYWifiMapper::parseDateAndTime》    bts: +b)

  //       

  year=bts[ ]* + +bts[ ];        //( YYWifiMapper::parseDateAndTime》    year: +year)

  month=bts[ ];

  day=bts[ ];

  hour=bts[ ];

  minute=bts[ ];

  second=bts[ ];

  msecond=bts[ ];

  //以下为格式化字符串

  int index= ;

  int temp=year;

  for( index>= ; index )

  format_str[index]=(byte)( +(temp temp/ * ))

  temp/= ;

  

  format_str[ ]= ;

  index= ;

  temp=month;

  for( index>= ; index )

  format_str[index]=(byte)( +(temp temp/ * ))

  temp/= ;

  

  format_str[ ]= ;

  index= ;

  temp=day;

  for( index>= ; index )

  format_str[index]=(byte)( +(temp temp/ * ))

  temp/= ;

  

  format_str[ ]= ;

  index= ;

  temp=hour;

  for( index>= ; index )

  format_str[index]=(byte)( +(temp temp/ * ))

  temp/= ;

  

  format_str[ ]= : ;

  index= ;

  temp=minute;

  for( index>= ; index )

  format_str[index]=(byte)( +(temp temp/ * ))

  temp/= ;

  

  format_str[ ]= : ;

  index= ;

  temp=second;

  for( index>= ; index )

  format_str[index]=(byte)( +(temp temp/ * ))

  temp/= ;

  

  //        format_str[ ]= ;

  //        index= ;

  //        temp=msecond;

  //        for( index>= ; index )

  //            format_str[index]=(byte)( +(temp temp/ * ))

  //            temp/= ;

  //       

  //

  //        format_str[ ]= ;

  //        ( YYWifiMapper::parseDateAndTime》    format_str = + new String(format_str))

  return new String(format_str)

  

  

  实际运行log如下(注意值为 进制)

  YYWifiMapper::parseDateAndTime》  v= :dc: : : : : b: : b: :

  YYWifiMapper::parseDateAndTime》  format_str = : :

  处理年的时候稍有不同 因为年是由两个字节表示 所以要用高位字节乘 再加低位字节 处理语句为year=bts[ ]* + +bts[ ];标准的时间输出格式应该为YYYY MM DD HH:MM:SS形式

  其实自己用计算器就能算出时间 很简单了

  主要是年的计算byte是可以为负值 所以手动计算的公式

   * + dc = * + = 年 或者 * + ( + dc) = * + ( + ( )) = 年

   = 月

   = 日

   = 时

   = 分

cha138/Article/program/Java/hx/201311/26305

相关参考