I have UTC time which is coming from UDP, I have a program to calculate Day and Time of UTC, how can I set my system clock at that day and time, kindly give me some direction so that I can make it possible.
I am using Window OS.
To set the current system time, use the SetSystemTime Win32 API function.
Related
Could someone help me figure out what clock I can use on iOS in c++ to get a steady/monotonic class that doesn't tick if the app is suspended? I'm trying to measure an approximate time it takes to process something but a regular clock doesn't help since it can inflate times by including time when the app was suspended and doing nothing.
Something equivalent to the QueryUnbiasedInterruptTime on Windows.
mach_absolute_time doesn't tick if the iphone is suspended, but I want a clock that stops ticking if the app is suspended.
I'm using an SD card with an Arduino Mega. Each time I write to the card, which is about once every 2 or 3 seconds, I want there to be a time stamp next to the text, the same way it works on the Serial Monitor.
I was wondering if there's any way to do this programmatically instead of getting an RTC.
There are software RTC libraries out there, like swRTC, and Arduino-RTC has a software-only mode.
These libraries rely on the accuracy of the Arduino's clock, so they won't be all that accurate and will need adjustment every so often. Also, you will need to set them every time you reset your Arduino.
Setting and adjusting these software RTCs requires a current time from somewhere, like your PC if connected over serial, WiFi for your network time, GPS time, an SNTP server on the interwebs, etc.
The simplest way would be to set a time on start-up by hand, and keep time yourself with millis(). Also not very accurate, but better than nothing.
Having said that: I would just get a hardware RTC; they are cheap, fairly accurate, and they have a backup battery so they always keep time. They are also better than the RTC in Arduinos that do have one in hardware.
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.
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.
Is there a way in qt to get the up time of the application as well as the up time for the system?
Thanks in advance.
You can use the QElapsedTimer class from Qt 4.7 to get uptime for your app. This class will use monotonic clocks if it can.
Just create an instance, and call start on it at the start of your program. From then on, you can get the number of milliseconds your program has been running (or more precisely, since the call to start) by calling
myElapsedTimer.elapsed()
On Windows you can simply calculate by calling Winapi function to get process start datetime.
More information you can find at http://www.codeproject.com/KB/threads/ProcessTime.aspx
On Linux, you can use the times system call to tell you elapsed processor time. This will not count the time your program has been idle waiting for input, or blocked waiting for input, or the time that it's been preempted by other programs also running on the system. (Therefore, this makes it very good for benchmarks.)