我如何因为使用C午夜UTC毫秒? [英] How do I get milliseconds since midnight UTC in C?

查看:109
本文介绍了我如何因为使用C午夜UTC毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在time.h中的时间函数给出毫秒数,因为时代。

The time function in time.h gives milliseconds since the epoch.

推荐答案

这是precise方式:

This is the precise way:

struct timeval tv;
int msec = -1;
if (gettimeofday(&tv, NULL) == 0)
{
    msec = ((tv.tv_sec % 86400) * 1000 + tv.tv_usec / 1000);
}

这将存入毫秒的毫秒数自午夜。
(或者-1,如果有错误获取时间。)

That will store into msec the number of milliseconds since midnight. (Or -1 if there was an error getting the time.)

虽然它通常是一个坏主意,存储在一个 INT ,我是一个小骑士,并假设 INT 为至少32位,并能方便地适应范围(-1)至86,400,000

Although it's usually a bad idea to store time-values in an int, I'm being a little cavalier and assuming int is at least 32-bit, and can easily accommodate the range (-1) to 86,400,000.

但我不知道这是否是值得所有的努力。

But I don't know if it's worth all the effort.

这篇关于我如何因为使用C午夜UTC毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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