Linux 将毫秒转换为 GNU 端口的时间规范
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15024623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert milliseconds to timespec for GNU port
提问by jparthj
I want to convert milliseconds into timespec structure used by GNU Linux. I have tried following code for the same.
我想将毫秒转换为 GNU Linux 使用的 timespec 结构。我已经尝试过以下代码。
timespec GetTimeSpecValue(unsigned long milisec)
{
struct timespec req;
//long sec = (milisecondtime /1000);
time_t sec = (time_t)(milisec/1000);
req->tv_sec = sec;
req->tv_nsec = 0;
return req;
}
Running this code gives me the following error.
运行此代码给我以下错误。
expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘GetTimeSpecValue'
'GetTimeSpecValue' 之前的预期 '=', ',', ';', 'asm' 或 '__attribute__'
I have also include time.hfile in the code.
我还在代码中包含time.h文件。
采纳答案by hetepeperfan
The timespec
structure represents time in two portions — seconds and nanoseconds. Thus, the algorithm for conversion from milliseconds is pretty darn simple. One seconds has thousand milliseconds, one milliseconds has a thousand microseconds and one microsecond has a thousand nanoseconds, for which we are grateful to SI. Therefore, we first need to divide milliseconds by a thousand to get a number of seconds. Say, for example, 1500 milliseconds / 1000 = 1.5 seconds. Given integer arithmetics (not a floating point), the remainder is dropped (i.e. 1500 / 1000 is equal to just 1, not 1.5). Then we need to take a remainder that denotes a number of milliseconds that is definitely less than one second, and multiply it by a million to convert it to nanoseconds. To get a remainder of dividing by 1000, we use a module operator (%
)(i.e. 1500 % 1000 is equal to 500
). For example, let's convert 4321 milliseconds to seconds and nanoseconds:
该timespec
结构以两部分表示时间——秒和纳秒。因此,从毫秒转换的算法非常简单。一秒有一千毫秒,一毫秒有一千微秒,一微秒有一千纳秒,为此我们感谢SI。因此,我们首先需要将毫秒除以一千来得到秒数。比如说,1500 毫秒 / 1000 = 1.5 秒。给定整数算术(不是浮点数),余数被丢弃(即 1500 / 1000 等于 1,而不是 1.5)。然后我们需要取一个表示绝对小于一秒的毫秒数的余数,并将其乘以一百万以将其转换为纳秒。要得到除以 1000 的余数,我们使用模块运算符 ( %
)(即1500 % 1000 is equal to 500
)。例如,让我们将 4321 毫秒转换为秒和纳秒:
- 4321 (milliseconds) / 1000 = 4 (seconds)
- 4321 (milliseconds) % 1000 = 321 (milliseconds)
- 321 (milliseconds) * 1000000 = 321000000 (nanoseconds)
- 4321(毫秒)/ 1000 = 4(秒)
- 4321(毫秒)% 1000 = 321(毫秒)
- 321(毫秒)* 1000000 = 321000000(纳秒)
Knowing the above, the only thing that is left is to write a little bit of C code. There are few things that you didn't get right:
知道了上面的内容,剩下的就是写一点C代码了。有几件事你没有做对:
- In C, you have to prefix structure data types with
struct
. For example, instead of sayingtimespec
you saystruct timespec
. In C++, however, you don't have to do it (unfortunately, in my opinion). - You cannot return structures from the function in C. Therefore, you need to pass a structure by pointer into a function that does something with that structure.
- 在 C 中,您必须在结构数据类型前加上
struct
. 例如,与其说timespec
你说struct timespec
。但是,在 C++ 中,您不必这样做(不幸的是,在我看来)。 - 您不能从 C 中的函数返回结构。因此,您需要通过指针将结构传递给对该结构执行某些操作的函数。
Edit: This contradicts (Return a `struct` from a function in C).
编辑:这与(从 C 中的函数返回一个 `struct`相矛盾)。
OK, enough talking. Below is a simple C code example:
好了,聊够了。下面是一个简单的 C 代码示例:
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
static void ms2ts(struct timespec *ts, unsigned long ms)
{
ts->tv_sec = ms / 1000;
ts->tv_nsec = (ms % 1000) * 1000000;
}
static void print_ts(unsigned long ms)
{
struct timespec ts;
ms2ts(&ts, ms);
printf("%lu milliseconds is %ld seconds and %ld nanoseconds.\n",
ms, ts.tv_sec, ts.tv_nsec);
}
int main()
{
print_ts(1000);
print_ts(2500);
print_ts(4321);
return EXIT_SUCCESS;
}
Hope it helps. Good Luck!
希望能帮助到你。祝你好运!
回答by hetepeperfan
try this:
尝试这个:
struct timespec GetTimeSpecValue(unsigned long millisec) {
struct timespec req;
req.tv_sec= (time_t)(millisec/1000);
req.tv_nsec = (millisec % 1000) * 1000000;
return req;
}
I don't think struct timespec is typedef'ed,hence you need to prepend timespec with struct. And work out the nano second part if you want to be precise. Note that req is not a pointer. Thus members cannot be accessed with '->'
我不认为 struct timespec 是 typedef'ed,因此您需要在 timespec 前面加上 struct。如果你想精确的话,计算出纳秒部分。请注意, req 不是指针。因此不能使用“->”访问成员
回答by trench_digger
Incorporating a few tweaks to the answer including Geoffrey's comment, the code below avoids divides for small delay and modulo for long delay:
对答案进行了一些调整,包括 Geoffrey 的评论,下面的代码避免了小延迟的除法和长延迟的取模:
void msec_to_timespec(unsigned long msec, struct timespec *ts)
{
if (msec < 1000){
ts->tv_sec = 0;
ts->tv_nsec = msec * 1000000;
}
else {
ts->tv_sec = msec / 1000;
ts->tv_nsec = (msec - ts->tv_sec * 1000) * 1000000;
}
}