将毫秒转换为GNU端口的时间规格 [英] Convert milliseconds to timespec for GNU port
问题描述
我想将毫秒转换为GNU Linux使用的timespec结构。我已经尝试了以下代码来实现同样的目的。
I want to convert milliseconds into timespec structure used by GNU Linux. I have tried following code for the same.
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.
在'GetTimeSpecValue'之前的'=',',',';','asm'或'__attribute __'
expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘GetTimeSpecValue’
我还在代码中包含了 time.h 文件。
I have also include time.h file in the code.
推荐答案
timespec
结构以两部分表示时间-秒和纳秒。因此,从毫秒转换的算法非常简单。一秒有千毫秒,一毫秒有千微秒,一微秒有千微秒,对此我们非常感谢: SI 。因此,我们首先需要将毫秒除以一千以得到秒数。举例来说,假设1500毫秒/ 1000 = 1.5秒。给定整数算术(不是浮点数),其余部分将被丢弃(即1500/1000等于1,而不是1.5)。然后,我们需要取一个表示绝对小于一秒的毫秒数的余数,并将其乘以一百万以将其转换为纳秒。要获得除以1000的余数,我们使用模块运算符(%
)(即 1500%1000等于500
)。例如,让我们将4321毫秒转换为秒和纳秒:
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:
- 4321(毫秒)/ 1000 = 4(秒)
- 4321(毫秒)%1000 = 321(毫秒)
- 321(毫秒)* 1000000 = 321000000(纳秒)
了解上述内容后,剩下的唯一事情就是编写一点C代码。很少有您做不到的事情:
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语言中,必须在结构数据类型前加上
结构
。例如,不是说timespec
,而是说struct timespec
。但是,在C ++中,您不必这样做(不幸的是,在我看来)。 - 您无法从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 )。
好的,足够多的谈话。下面是一个简单的C代码示例:
OK, enough talking. Below is a simple C code example:
#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!
这篇关于将毫秒转换为GNU端口的时间规格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!