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
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
時間、分、秒を秒に変換するのは簡単です。
total_seconds = hours*60*60 + minutes*60 + seconds;
これをすべて秒に変換して実装し、より簡単な実装を行う方法はありますか?
いずれにせよ、秒と時/分/秒の間の時間をかなり頻繁に変更する必要があるため、ストレージにどちらか一方を使用することに必ずしも大きな利点があるとは限りません。別の言い方をすれば、seconds-since-midnight の方がおそらく優れていますが、書き直す価値はないかもしれません。+
やのようなものについては-
、秒だけを保存していない場合は簡単に計算できます (Mark の投稿を参照)。演算を行ってから、秒から時/分/秒に変換して結果をデータに保存する関数を用意します。メンバー:
hours = seconds_since_midnight / 60 / 60;
minutes = seconds_since_midnight / 60 % 60;
seconds = seconds_since_midnight % 60;
23:59:00 に 120 秒を追加すると、真夜中を過ぎた値が得られるので、「total_seconds % 24 * 60 * 60」を使用して、次の真夜中を過ぎた 60 秒を取得できます。 「24:01:00」から 00:01:00 まで。
(これを実際に使用したい場合は、これを自分で勉強するだけでなく、time()
関数と関連する変換ルーチンの使用を検討してくださいlocaltime()
)