net-snmp的dateandtime数据类型
2015-06-23 12:08
651 查看
net-snmp的dateandtime数据类型
2015/06/12 16:35:59DateAndTime是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 */
相关文章推荐
- python的多线程编程 --- thread模块
- OkHttp+Stetho+Chrome调试android网络部分
- MFC注册表操作
- net-snmp-5.7.3配置编译安装
- 程序猿放松娱乐必去的6个网站推荐
- 本博客以逐步迁移到 http://mcdona1d.me ,欢迎访问
- hdu4614 二分法+段树
- 第十五周oj刷题——Problem L: C++习题 复数类--重载运算符2+
- WebRTC 学习笔记(5)--线程模型
- mysql-bin权限问题导致mysql无法启动
- Linux下编译安装Apache Http Server
- 日语学习之沪江N3基础 20150623 -1
- sp_addlinkedserver '(null)' is an invalid product name
- 使用Meteor配合Node.js编写实时聊天应用的范例
- iOS 终端命令行
- awk学习笔记
- HTTP-API-DESIGN 怎样设计一个合理的 HTTP API (二)
- osgi环境问题
- link流程 建立时需要注意的地方
- Handlebars 文档笔记