I have the following hook in my NodeUp and an equivalent in my node down. Apart from it being the longest way of doing this (first one I found when searching), I get a strange result.
Everything is correct - except - the month is out by 1. It reports month as being last month, not this month. I know the local time is correct(or bein extracted correctly) because I am comparing the Node_Up and Node_Down event times to the custom attributes - and everything matches - exactly - except the month - which is out by one.
SetCustomAttribute($node, "timelastcameup",localtime(time())->mday.".".localtime(time())->mon.".".localtime(time())->year.", ".localtime(time())->hour.":".localtime(time())->min.":".localtime(time())->sec);
Any ideas?
As per http://www.cplusplus.com/reference/ctime/tm/, I believe months are "months since January" and therefore go from 0-11, not 1-12.
Tursiops - correct as usual :)
https://wiki.netxms.org/wiki/NXSL:TIME
Would be nice for NXSL TIME to also have mona as the actual month - adding 1 to mon so I don't have to.
Adding that within SetCustomAttribute will be fun. Something like one of the following I hope - but probably not that lucky.
SetCustomAttribute($node, "timelastcameup",localtime(time())->mday.".".((localtime(time())->mon)+1).".".localtime(time())->year.", ".localtime(time())->hour.":".localtime(time())->min.":".localtime(time())->sec);
or
SetCustomAttribute($node, "timelastcameup",localtime(time())->mday.".".localtime(time())->mon+1.".".localtime(time())->year.", ".localtime(time())->hour.":".localtime(time())->min.":".localtime(time())->sec);
Or going the long way:
day = localtime($1)->mday;
mon = localtime($1)->mon+1;
year = localtime($1)->year;
hour = localtime($1)->hour;
min = localtime($1)->min;
SetCustomAttribute($node, "timelastcameup", day."-".mon."-".year.",".hour.":".min;
One of these should work.
You can simply use strftime (https://wiki.netxms.org/wiki/NXSL:strftime (https://wiki.netxms.org/wiki/NXSL:strftime)):
SetCustomAttribute($node, "timelastcameup", strftime("%d-%m-%Y, %H:%M:%S", $1));
Best regards,
Victor