Here is a implementation that I wrote to add hrs, min and sec in C++
Is there a way to implement this by converting all this to seconds and do a simpler implementation?
For some reason it wouldn’t let me add the code in this post 🙁
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Whichever way you go, you’ll need to change the time between seconds and hour/minute/second quite often, so there’s not necessarily a huge benefit to using one or the other for storage. Put another way, seconds-since-midnight is probably nicer, but maybe not worth a rewrite. For things like
+and-, if you’re not storing just seconds then you can calculate it easily (see Mark’s post), do your arithmetic then have a function to convert from seconds back to hour/min/sec to store the result into your data members:Say you’re adding 120 seconds to 23:59:00, you’ll get a value past midnight, so you can use “total_seconds % 24 * 60 * 60” to get just the 60 seconds past the following midnight, equivalent to wrapping from “24:01:00” to 00:01:00.
(If you want to actually use this, and not just doing this to teach yourself, then consider using the
time()function and associated conversion routines likelocaltime())