|
From: Jose R. F. A. <jr...@gm...> - 2018-07-15 21:55:01
|
Hello Net-SNMP fellows!
I would like to have some help from who always code something like i”m trying to do.
I’m storing a DateAndTime in a text file in format : 2018-5-25,12:34:30.0,+0:0
and I’m trying to use netsnmp_dateandtime_set_buf_from_vars() or some other
strategy to recover the value in a compatible way to be show thru snmpget().
Could someone tell the best (right way) to recover DatAndTime values to be returned by Get_Scalar() or
Get_Table(0 functions after to have this value stored in a text file in a form I described above?
Below is the segment code i”m using to recover the DateAndTime value from text file to byte encoded as
mentioned in he snmp_tc.c source file.
Any help will be very appreciated!
Best regards for all you!
u_short year;
u_char month;
u_char day;
u_char hour;
u_char minutes;
u_char seconds;
u_char deci_seconds = 0;
int utc_offset_direction = 0;
u_char utc_offset_hours = 0;
u_char utc_offset_minutes = 0;
u_char now[11];
struct tm timeDate;
data = (u_char *) var_data;
printf("value to hint Date Time: %s Length:%d\n", var_data, dataLength);
memset(&timeDate, 0, sizeof(struct tm));
if (strptime((const char *) data, "%Y-%m-%d,%H:%M:%S", &timeDate) == NULL) {
printf("OPS++++ strptime\n");
goto UnknownFormat;
}
year = htons((u_short) (timeDate.tm_year + 1900));
month = (u_char) timeDate.tm_mon +1;
day = (u_char) timeDate.tm_mday;
hour = (u_char) timeDate.tm_hour;
minutes = (u_char) timeDate.tm_min;
seconds = (u_char) timeDate.tm_sec;
#if defined(HAVE_STRUCT_TM_TM_GMTOFF) || defined(HAVE_TIMEZONE_VARIABLE)
// normally we wil have 24 byte length …
if (length > 14) {
u_char *pch;
// decompound TIMEZONE/GMTOFF data after seconds tm structure …
// having for instance stored values like: ‘2018-5-25,12:34:30.0,+0:0”
if ((pch = (u_char *) strrchr((char *) data, '.')) == NULL) goto UnknownFormat;
pData = data + (pch - data + 1);
deci_seconds = (u_char) atoi((const char *) ++pData);
if ((pch = (u_char *) strrchr((char *) data, ',')) == NULL) goto UnknownFormat;
pData = data + (pch - data + 1);
utc_offset_direction = (*pData == '-') ? -1 : 1;
utc_offset_hours = atoi((const char *) ++pData);
if ((pData = (u_char *) strrchr(((char *) data), ':')) == NULL) goto UnknownFormat;
pData = data + (pch - data + 1);
utc_offset_minutes = atoi((const char *) ++pData);
if (netsnmp_dateandtime_set_buf_from_vars((u_char *) var_data,
(size_t *) &length,
year,
month,
day,
hour,
minutes,
seconds,
deci_seconds,
utc_offset_direction,
utc_offset_hours,
utc_offset_minutes) != SNMPERR_SUCCESS)
goto UnknownFormat;
} |