net-snmp的dateandtime数据类型
net-snmp的dateandtime数据类型
2015/06/12 16:35:59
DateAndTime是Snmpv2中的一种数据类型,它主要提供了对日期时间的描述。
在开发一个snmp相关程序的时候,碰到了这个东西。
本文由乌合之众瞎写http://www.cnblogs.com/oloroso
原由
在开发的时候,某个节点的值是DateAndTime类型的,因为是通过挂载脚本来获取的值,是一个字符串,形似 "2015-06-12 17:04:03" 这样的。现在要将这个值转换为net-snmp管理端可以获取的DateAndTime值。
关于这个结构的描述,在net-snmp源代码目录下的snmplib/snmp-tc.c文件中可以找到。
net-snmp中没有定义这个类型,而是当做一个unsigned char类型的数组,根据各个字段的宽度来操作。
结构描述如下
| 字段 | 字节数 | 内容 | 取值范围 | 
|---|---|---|---|
| 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 | 
| * 注意:-年份这个值使用网络字节序 | 
下面是文件中的内容拷贝过来的。
#ifndef NETSNMP_FEATURE_REMOVE_NETSNMP_DATEANDTIME_SET_BUF_FROM_VARS
/*
  DateAndTime ::= TEXTUAL-CONVENTION
    DISPLAY-HINT "2d-1d-1d,1d:1d:1d.1d,1a1d:1d"
    STATUS       current
    DESCRIPTION
            "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."
    SYNTAX       OCTET STRING (SIZE (8 | 11))
*/
数据操作
net-snmp这个库已经提供了一些操作这个"数据类型"(这里当做一个数据类型)的操作函数,比较游泳的就是netsnmp_dateandtime_set_buf_from_vars这个函数。这个函数用于利用相关的日期时间数值来构建这个数据。注意参数bufsize是传入传出参数,传入buf的大小,传出构建后的数据内存字节数。
int
netsnmp_dateandtime_set_buf_from_vars(u_char *buf, size_t *bufsize,
                                      u_short year, u_char month, u_char day,
                                      u_char hour, u_char minutes,
                                      u_char seconds, u_char deci_seconds,
                                      int utc_offset_direction,
                                      u_char utc_offset_hours,
                                      u_char utc_offset_minutes)
{
    u_short tmp_year = htons(year);
    /*
     * if we have a utc offset, need 11 bytes. Otherwise we
     * just need 8 bytes.
     */
    if(utc_offset_direction) {
        if(*bufsize < 11)
            return SNMPERR_RANGE;
        /*
         * set utc offset data
         */
        buf[8] = (utc_offset_direction < 0) ? '-' : '+';
        buf[9] = utc_offset_hours;
        buf[10] = utc_offset_minutes;
        *bufsize = 11;
    }
    else if(*bufsize < 8)
        return SNMPERR_RANGE;
    else
        *bufsize = 8;
    /*
     * set basic date/time data
     */
    memcpy(buf, &tmp_year, sizeof(tmp_year));
    buf[2] = month;
    buf[3] = day;
    buf[4] = hour;
    buf[5] = minutes;
    buf[6] = seconds;
    buf[7] = deci_seconds;
    return SNMPERR_SUCCESS;
}
#endif /* NETSNMP_FEATURE_REMOVE_NETSNMP_DATEANDTIME_SET_BUF_FROM_VARS */

                
            
        
浙公网安备 33010602011771号