计算时间差 [英] Calculating difference in time

查看:138
本文介绍了计算时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要每50秒打印一次.我认为通过从上次时间中减去当前时间可以算出经过了50秒.

假设我知道上一个和当前时间的小时,分​​钟和秒,如何计算以秒为单位的时间?

我正在使用C

I need to print something out every 50 seconds. I figured I would calculate that 50 seconds had passed by subtracting my current time from the previous time.

Let''s say I know the hour, minute and seconds of the previous and current time, how do I calculate how much time had passed in seconds?

I''m using C

推荐答案

计算无济于事.您确实需要一个当前线程处于睡眠状态的计时器,这样可以避免旋转等待和浪费CPU时间.您可以通过操作系统关闭线程的方式来执行此操作,并且不要将其调度回执行,直到超时唤醒它以继续执行为止.这只是一个基本概念,API取决于您可能使用的平台和/或库.

太糟糕了,您没有提供必要的细节.

—SA
Calculations do not help. You really need either a timer of a sleep state of a current thread, this way you can avoid spin-wait and wasting CPU time. You can do it the way the OS switches off your thread and don''t schedule it back to execution until it is awaken by a timeout to continue execution. This is just a basic idea, and the API depends on your platform and/or libraries you might use.

Too bad you did not provide essential details.

—SA


感谢您的回答.这就是我所做的.

我使用了别人给我的答案的修改版本:

((Cur_hour-Prev_hour)* 3600)+((Cur_min-Prev_min)* 60)+(cur_sec-Prev_sec).

我使用的是C,因此我通过以下方式计算了差异:
(cur_hour * 3600)+(cur_min * 60)+(cur_sec)-(prev_hour * 3600)+(prev_min * 60)+(prev_sec).
Thanks for the answers. Here''s what I did.

I used a modified version of an answer someone else gave me:

( (Cur_hour - Prev_hour)*3600) + ((Cur_min - Prev_min)*60) + (cur_sec - Prev_sec).

I was using C, so I calculated the difference by:
(cur_hour *3600)+(cur_min*60)+(cur_sec) - (prev_hour * 3600) + (prev_min*60)+(prev_sec).


亲爱的CSLexy您正在使用C,然后使用头文件 dos.h delay(t) 函数,延迟函数中持续50秒的t是50000 .
Dear CSLexy u are using C then use delay(t) function of header file dos.h and there t in delay function for50 second is 50000.


这篇关于计算时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆