Timeticks to date format? - c++

How to convert time from Timeticks (ASN_TIMETICKS from net-snmp library) to any c++ datetime format?
Thx.

Time Ticks are units of 10ms elapsed since the agent last reinitialized; in order to convert to an absolute timestamp, you need to establish a mapping between agent uptime and wall clock time.
Typically, you query sysUptime.0 and note down when the response arrived in order to get the initialization time; you can either do this once at startup and everytime you receive one of the standard traps (cold/warm restart, link up) in order to catch agent restarts, or you include it in the GET request (for GETNEXT, ask for sysUptime, leaving out the instance ID).

Related

When will updates occur to the health status information?

Recently I started logging the lock's healthstatus info. What exactly is the Timestamp of these objects?
I have lots of round timestamps (like 2022-05-20 12:00:00.000), but also arbitrary timestamps (like 2022-05-20 16:16:37.617). What exactly does this Timestamp mean? Is it when a lock is last opened?
Stan
As I understand you are talking about the healthStatuses property of a Boundlock documented here:
https://developers.tapkey.io/openapi/tapkey_access_management_web_api_v1/#/Bound%20Locks/BoundLocks_GetById
The timestamp means that this specific status was collected at this time. The collection can be triggered, depending from the type, from different events. E.g. during unlock, during start or internal timer.
If the event was an internal timer it can lead to a round timestamp, if the source was an external event, then the timestamp will of course not be round.

Timer countdown even when program is not running QML

I am trying to have a 24 hour countdown on my user interface in a QML/Qt project. The time should update every second like 23:59:59 then 23:59:58. Additionally, I need the time to continue going down even when the application is not open. So if the time is 23:59:59 when I close the app, if I open it two hours later it should continue counting down from 21:59:59. If the timer had timed out when the app isn't running, it needs to reset to 24 and continue. Does anyone know how I could do this, either QML or connected c++? Any help would be greatly appreciated.
You need to store somewhere timer's end time according to system clock or equivalent information. So at each moment you can tell timer's value by taking difference between system clock's now() and timer's end.
Just use std::this_thread::sleep_until to wait to the exact moment you need to update the time for the next second. Don't use sleep_for(1s) as this way you'll accumulate inaccuracies.
Note: system clock has an issue that it can be adjusted. I don't fully know of a way around it - say your application turned off then how to tell how much time passed if system clock was adjusted? You can deal with clock adjustment during application run by using sleep_until with steady_clock. In C++ 20 they introduce utc_clock perhaps you can access that somehow which should solve the issue with daylight saving time adjustments. I don't think that it is theoretical possible to deal with all types of clock adjustments unless you have access to GPS clock.

how can I read timestamp with no system interruption?

I am using standard library (std::chrono ) to get current time to set time in several messages in a real time application, but this method seems to slow down message processing .
Is there a way to avoid system interruption or to enhance timestamp reading during program execution ?

Is it possible in C/C++ in Linux to get informed when a specified date/time is reached?

Is it possible using standard C++ in Linux to get informed when a specified date/time is reached by the system time (assuming my process is up of course)?
I could just set a timer to the time I need to wait, but what happens if the user changes the system time? Can I get informed by the system that the user changed the system time to reset my timers?
The Linux kernel has such system calls, although, they are not integrated into the libc API.
You can create a timer, get a file descriptor for it from the kernel, and do a select or epoll call on the descriptor to be notified when the timer fires.
The man page for it: http://www.kernel.org/doc/man-pages/online/pages/man2/timerfd_create.2.html
Sure, just write code to do exactly what you want. For example: You could keep a list of all such timers, the date/time they're supposed to be fired, and what code should be executed when they occur. Then every so often you could check the time to see if it's past the time any of your timers should fire. If so, fire them, remove them from the list, and go on about your business.
See the documentation for 'at' command (man at)
For example, at could send you an email at a given time, like 2:35 PM.
at 14:35
warning: commands will be executed using /bin/sh
at> mail -s "It is 2:35 PM" dbadmin < /dev/null
at><EOT> # After CTRL/D pressed.
job 9 at Tue May 8 14:35:00 2012
You can calculate the time from program start to the event, and call
sleep (difftime-1);
Then you could control if the time was reset, but this way you would only be able to correct the time to the future - if the time was set back, you would have skipped the event.

wait for a time

I have a requirement to start my application at certain time. I don’t want to be put in the corn job. My executable is an application and like to start on 2011-Jan-20
So I have to run it as
./app –date 2011-Jan-20
Here problem is, how I will calculates the time difference from current and date supplied in command line option.
I don’t want to write won function. Is there any in build function are available for such type of time difference. ( c and Linux)
I know you're expecting a C answer but this might interest you:
Since you're on linux, the system provides already an efficient way to schedule ponctual tasks: at
In your case, an user that would like to run his task on the 20.01.2011 at 8AM, would just type:
echo "./app" | at 08:00 20.01.2011
The task will be run using the credentials of the user. Note that at also accept relative time directives such as at now +1 day. It is a powerful tool which ships with most Linux distributions by default.
The list of scheduled jobs can be get using:
atq
And you can even remove scheduled jobs using:
atrm
Hope this helps.
You can calculate the difference between the start time and now in milliseconds and then wait for that many milliseconds by passing that number as a timeout argument to select() or epoll().
To calculate the difference, one way is to first convert your date string to struct tm using strptime() and then pass it to mktime() which is going to give you a number of seconds since unix epoch 1970-01-01 00:00:00. Then get the current time by using gettimeofday() or clock_gettime(), they also report time passed since unix epoch. Convert the start time and the current time to seconds and subtract the values.