从微秒转换为小时,分钟,秒和毫秒

时间:2013-01-29 09:50:02

标签: algorithm date datetime pseudocode

我需要将微秒(保存在unsigned long long int变量中)传递给它的表示形式,如小时,分钟,秒,毫秒,即:

from 47072349659 to 13:04:32.350

我找到了this conversion from milliseconds,但我似乎无法让它在我的案例中发挥作用。也许问题是数字太长而无法存储在某些变量类型中?我正在使用unsigned long long int作为输入时间,并尝试使用int,long,unsigned long long int作为输出。

这是我的C ++代码:

unsigned long long int timestamp;

long milliseconds   = (long) (timestamp / 1000000) % 1000;
long seconds    = (long) ((timestamp / (1000)) % 60);
long minutes    = (long) ((timestamp / (60000)) % 60);
long hours      = (long) ((timestamp / (3600000)) % 24);

1 个答案:

答案 0 :(得分:2)

我认为你的错误在于你的错误:

long milliseconds   = (long) (timestamp / 1000) % 1000;
long seconds    = (((long) (timestamp / 1000) - milliseconds)/1000)%60 ;
long minutes    = (((((long) (timestamp / 1000) - milliseconds)/1000) - seconds)/60) %60 
long hours      = ((((((long) (timestamp / 1000) - milliseconds)/1000) - seconds)/60) - minutes)/60