I'm trying to write a function which takes in a current date time (expressed as an epoch) and also takes in an alarm time (expressed as a hour, minute and second) and then calculates the seconds to the next such alarm.
If the current time exceeds the alarm time, it should return the seconds to alarm time on the next day (e.g. if the time is 15 June 2020 at 15:30:22 and the alarm is set at 15:10:22 then the function should return the seconds until 16 June 2020 at 15:30:22). If the time and alarm are the same it should return 0.
I have come up with the below which seems to work but would appreciate any thoughts. If there is an easier way of doing this (using a c library but not an external library), please let me know.
yea you should be able to get the difference directly, subtract, and display it in seconds.
I think this does it if you use the <chrono> time tools? Take a look at those and see if its a better choice for this task.
chrono::duration_cast<chrono::seconds>(end_time - start_time).count()
I don't think chrono has a concept of 'days' though. If it isnt appropriate to your needs, the above may be close. I think it can be simplified a bit from what you have... do you need to talk in calendar days with the user?