简体   繁体   English

将time_t转换为timeval

[英]Convert time_t to timeval

I am facing problem in converting the time_t to struct timeval . 我在将time_t转换为struct timeval I need the tv_usec & tv_sec value to be get populated from the time_t value. 我需要从time_t值中填充tv_usectv_sec值。

In order to populate tv_sec and tv_usec we need to call gettimeofday() . 为了填充tv_sectv_usec我们需要调用gettimeofday() But i dont want to call that function since i have time in time_t . 但是我不想调用该函数,因为我有时间在time_t From the time_t i want to extract/convert to milliseconds. 我想从time_t中提取/转换为毫秒。 Since existing code they use struct timeval for populating. 由于现有代码,因此它们使用struct timeval进行填充。

If you could suggest how to get millisecond from time_t would be great. 如果您可以建议如何从time_t中获取毫秒数,那将是很好的。

Any help on this would be really appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

Definition of struct timeval (in <sys/time.h> ) : struct timeval定义(在<sys/time.h> ):

struct timeval 
{
     time_t      tv_sec;     /* seconds */
     suseconds_t tv_usec;    /* microseconds */
};

And as you can see, time_t can only hold seconds , you cannot get milliseconds from time_t . 如您所见, time_t只能保留 ,而time_t不能获得毫秒 If you really want to work with a timeval object, you can simply define a timeval object, and manually add the time_t variable to it. 如果您确实想使用timeval对象,则只需定义一个timeval对象,然后手动向其中添加time_t变量。 ie,

time_t time_here; //Your time_t variable.
timeval time_now; //Your timeval object.
time_now.tv_sec = time_here; //Assign time_here to this object.
time_now.tv_usec = 0; //As time_t can hold only seconds.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM