struct timeval用两个成员表示并及时,tv_sec(秒)和tv_usec(微秒).在这种表示中,tv_usec它本身不是绝对时间,它是亚秒的偏移量tv_sec.
struct timespec以相同的方式工作,除了微秒,它的offset(tv_nsec)以纳秒为单位存储.
问题是:有没有一种标准的方法来转换这两者?
我正在编写一个套接字程序来维护两个输入套接字的FIFO队列.在决定要服务的队列时,程序从每个队列中提取最新的时间戳.
我需要一种可靠的方法来比较两个timeval结构.我尝试使用timercmp(),但我的gcc版本不支持它,文档说明该功能不符合POSIX.
我该怎么办?
如何从time.h重新初始化timeval结构?
我意识到我可以将结构的两个成员重置为零,但是我还有其他方法可以忽略吗?
标题说明了一切.
我必须实现一个接收std :: chrono :: system_clock :: duration值的函数,并且需要将其转换为timeval sruct,以便将其传递给某个系统函数.
在C/C++中,假设我有以下值:
int year = 2010;
int month = 6;
int day = 15;
int hour = 14;
int minute = 52;
int seconds = 34;
int microseconds = 141231;
Run Code Online (Sandbox Code Playgroud)
将此转换为时间段的最简单方法是什么?我认为timeval是自1970年1月1日以来的时间?但每次手动计算这个似乎非常繁琐.什么是最简单(也是最快)的方法?
谢谢
我有一个应用程序,我移植到Microsoft Visual Studio 2008,在Linux上构建和运行良好.
我遇到时间例程的麻烦,我的Linux代码看起来像这样:
#include <sys/types.h>
#include <sys/time.h>
typedef long long Usec;
inline Usec timevalToUsec(const timeval &tv)
{
return (((Usec) tv.tv_sec) * 1000000) + ((Usec) tv.tv_usec);
}
Run Code Online (Sandbox Code Playgroud)
但编译器在sys/time.h头文件上失败:
fatal error C1083: Cannot open include file:
'sys/time.h': No such file or directory
Run Code Online (Sandbox Code Playgroud)
如果我将include更改为只是time.h我得到一个不同的错误timeval未定义:
error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
Run Code Online (Sandbox Code Playgroud)
这是由于timeval没有定义.
包含time.h而不是sys/time.h正确,如果是,我在哪里可以获得struct timevalMicrosoft Visual Studio 2008中的定义?
我有一个包含结构的 C 程序
struct S{
int x;
struct timeval t;
};
Run Code Online (Sandbox Code Playgroud)
和一个函数
int func(struct S s1, struct S s2)
Run Code Online (Sandbox Code Playgroud)
我需要从我的 python 程序中调用这个函数。我正在使用 ctypes.Python 上的并行结构
import ctypes
from ctypes import *
class S(Structure):
_fields_ = [("x",c_int),
("t", ?)]
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是我将在 ? 位置以及与之相关的任何依赖项。提前致谢。
我有一个函数,它接受两个当前类级别的成员变量并将它们设置为timeval结构,并返回timeval obj(按值).
在设置类级别成员timeval对象与在每次get()调用时创建新的timeval对象时,我发现了一个问题.
在课堂上
protected:
int time[2];
timeval tv;
// work done on setting the time array
timeval getTimeval()
{
tv.tv_sec = (time_t)time[0];
tv.tv_usec = time[1];
return tv;
}
Run Code Online (Sandbox Code Playgroud)
这不会返回正确的时间值.tv.tv_sec将被覆盖,但tv_usec保持不变.但是,当我在get调用中创建timeval对象时,它将返回正确的值.
timeval getTimeval()
{
timeval t;
t.tv_sec = (time_t)time[0];
t.tv_usec = time[1];
return t;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由在成员变量上设置timeval对象应该与创建新对象和设置其值不同?
给定time_t或struct timeval,如何在当天获得EST/EDT午夜(当地时区)的timeval或time_t?假设当地时区为EST/EDT,给定time_t对应于美国东部时间/美国东部时间2010-11-30 08:00:00,预期答案为time_t,对应于2010-11-30 00:00:00 EST /美东时间
尝试1(错误:因为它不处理DST,并假设EST/EDT总是落后于UTC 5小时):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
return ( (5*3600) + ((( temp_time_t_ - 5*3600 )/86400 ) * 86400) );
}
Run Code Online (Sandbox Code Playgroud)
尝试2(错误:因为它返回的time_t对应于午夜时间而不是EST/EDT,本地时区):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
boost::posix_time::ptime temp_ptime_ = boost::posix_time::from_time_t ( temp_time_t_ );
boost::gregorian::date temp_date_ = temp_ptime_.date();
boost::posix_time::ptime temp_ptime_midnight_ ( temp_date_,
boost::posix_time::time_duration ( 0, 0, 0 ) );
return to_time_t ( temp_ptime_midnight_ );
}
time_t to_time_t ( const boost::posix_time::ptime & temp_ptime_ )
{
boost::posix_time::ptime temp_epoch_ptime_(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration::sec_type temp_sec_type_ …Run Code Online (Sandbox Code Playgroud) 我想从timeval类型的变量中检索以毫秒为单位的值.以下是我的尝试:
timeval* time;
long int millis = (time->tv_sec * 1000) + (time->tv_usec / 1000);
printf("Seconds : %ld, Millis : %ld", time->tv_sec, millis);
Output => Seconds : 1378441469, Millis : -243032358
Run Code Online (Sandbox Code Playgroud)
问题是我得到毫秒的毫秒值.这个片段有什么问题?
我收到了一些奇怪的编译错误.这是一个家庭作业(帮助还可以).我们的想法是实现一个程序来测试用户每秒可以进入"输入"的程度.我应该使用gettimeofday为每个"输入"获取一些时间值,然后找出平均时间和标准偏差...我试图通过检查标准输入'\n'来做到这一点,然后如果是的,使用gettimeofday填充timeval结构,然后将所述结构存储在一个数组中供以后使用...
我在编译(gcc -Wextra homework1.c)时得到的错误是:
homework1.c: In function ‘main’:
homework1.c:19:29: error: expected ‘]’ before ‘;’ token
homework1.c:27:17: error: expected ‘)’ before ‘;’ token
homework1.c:32:4: error: ‘entry_array’ undeclared (first use in this function)
homework1.c:32:4: note: each undeclared identifier is reported only once for each function it appears in
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我得到前两个语法错误然后我无法理解为什么当我在"main"的开头明确地声明它时,"entry_array"是未声明的.建议?
我觉得我因为不知道如何使用timeval结构而被烧毁......最初我试图像对待任何其他结构一样全局定义struct timeval但是在覆盖struct timeval的定义时遇到错误.这是因为它是在"sys/time.h"库中定义的吗?
这是代码:
GNU nano 2.2.6 File: homework1.c
//prototypes
int GetAverage(long array[]);
int GetStdDev(long array[]);
//# of keystrokes tracked by user
#define MAX_STROKES 1;
int main(int argv, char ** argc) …Run Code Online (Sandbox Code Playgroud) 我在下面有一个片段 - 我已经tv_usec用几种方式调整了我,但是select()无论tv_usec设置的是什么,我都会在一个循环中停留几乎整整10秒钟.
char buffer[512];
fd_set readfds;
struct timeval tv;
tv.tv_usec = 50;
int rv = 1;
// clear the set ahead of time
FD_ZERO(&readfds);
// add our descriptors to the set
FD_SET(mySocket, &readfds);
// the n param for select()
int n = mySocket + 1;
while(rv != 0)
{
rv = select(n, &readfds, NULL, NULL, &tv);
if (rv == -1)
perror("select"); // error occurred in select()
bzero(buffer,512);
int n = recvfrom(mySocket,buffer,512,0,(struct sockaddr *)&server, …Run Code Online (Sandbox Code Playgroud) 我试图以微秒为单位测量C时间.我试过这段代码,但是值time_passed很大,而不是0(或1).
struct timeval start;
settimeofday(&start,NULL);
struct timeval stop;
settimeofday(&stop,NULL);
unsigned long long int time_passed =
(stop.tv_sec-start.tv_sec)*1000000 + (stop.tv_usec - start.tv_usec);
printf("time passed: %llu us\n",time_passed);
Run Code Online (Sandbox Code Playgroud)