'timespec' is a structure allowing to store the time since Epoch (1. Jan 1970) providing nano seconds along with seconds. It is a POSIX extension to the C Standard to be declared in '
Questions tagged [timespec]
36 questions
44
votes
3 answers
GCC with -std=c99 complains about not knowing struct timespec
When I try to compile this on Linux with gcc -std=c99, the compiler complains about not knowing struct timespec. However if I compile this without -std=c99 everything works fine.
#include
int main(void)
{
struct timespec asdf;
return…

Nils
- 13,319
- 19
- 86
- 108
22
votes
1 answer
Timespec redefinition error
While executing a Pthread program in C using Visual Studio 2015, I got the following error:
Error C2011 'timespec': 'struct' type redefinition
The following is my code:
#include
#include
#include
void…

Vijay Manohar
- 473
- 1
- 7
- 22
15
votes
2 answers
Is there a standard way to convert a struct timeval into a struct timespec?
struct timeval represents and instant in time with two members, tv_sec (seconds) and tv_usec (microseconds). In this representation, tv_usec is not by itself an absolute time it is a sub second offset off of tv_sec.
struct timespec works the same…

dicroce
- 45,396
- 28
- 101
- 140
13
votes
1 answer
Why is the time reported by time() sometimes 1 second behind the seconds component of timespec_get() in C code?
The following snippet of code:
struct timespec ts;
for (int x = 0; x < 100000000; x++) {
timespec_get(&ts, TIME_UTC);
long cTime = (long) time(NULL);
if (cTime != ts.tv_sec && ts.tv_nsec < 3000000) {
printf("cTime: %ld\n",…

Theo d'Or
- 783
- 1
- 4
- 17
8
votes
3 answers
Convert milliseconds to timespec for GNU port
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…

jparthj
- 1,606
- 3
- 20
- 44
5
votes
2 answers
Why isn't timespec_get defined on any C compiler on my Mac?
According to the C11 Standard (7.27.2.5), there is a function timespec_get specified in time.h. I have tried several compilers, including clang and several versions of gcc, which are supposed to support C11, but this function is always missing. …

Steve Siegel
- 504
- 5
- 11
3
votes
3 answers
Retrieve Linux Time using struct timespec
Can someone please suggest how can I retrieve linux time using
struct timespec ts
type? It just gives me time since Epoch. Can I get the actual Linux time using this datatype?
Brief Background: I am writing a logger utility on embedded device…

Irfan
- 2,713
- 3
- 29
- 43
3
votes
2 answers
Storage size of timespec isn't known
I have seen this solution but I am getting the same errors. For a class, we had to switch to the c99 compiler instead of gnu but now timespec seems to be broken. Along with "storage size of timespec isn't known", I am getting a bunch of other…

grizzle
- 69
- 1
- 5
2
votes
1 answer
Comparing a time_t to a struct timespec (in C)
I have multiple struct timespec values and a time_t value. I want to see which struct timespec value is the closest to the time_t value. Should I just compare the seconds and ignore the nanoseconds? Or should I convert both to total nanoseconds…

jetset
- 388
- 4
- 14
2
votes
2 answers
timespec on windows compilers
On posix it is possible to use timespec to calculate accurate time length (like seconds and milliseconds). Unfortunately I need to migrate to windows with Visual Studio compiler. The VS time.h library doesn't declare timespec so I'm looking for…

PLS
- 231
- 2
- 6
- 14
2
votes
2 answers
Where to find the source code of timespec_get?
The C11 standard provides the function timespec_get. If I run the example code on cppreference, or on my computer, it works:
#include
#include
int main(void)
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);
char…

Vincent
- 57,703
- 61
- 205
- 388
2
votes
1 answer
C Program crashes(Segmentation Fault) for large size of input array. How to prevent it without using static/global/malloc?
The following program is to sort a large array of random numbers using heapsort. The output of the program is the total execution time of the recursive heapSort function(in microseconds). The size of the input array is defined by the SIZE macro.…

Akash Das
- 163
- 15
2
votes
1 answer
C - Linux Kernel - How to use timespec_to_ns()?
I am trying to convert the start_time of the linux kernel task_struct into nanoseconds. I need to give it the argument of const struct timespec * but start_time is of type struct timespec.
How would I make it a constant and a pointer to the timespec…

Smreks
- 317
- 1
- 7
- 19
2
votes
3 answers
Is timespec not defined in Windows?
It seems weird to me that this answer is hard to find. I've included time.h and ctime, but vc9 is still complaining about an undefined type 'timespec'. I've searched here, MSDN, and the web (even with the exact compiler error), but I can't find the…

tjd
- 467
- 1
- 8
- 14
1
vote
3 answers
Calculate average time between timespec
I am working with an averaging function following the formula
new average = old average * (n-1) / n + (new value / n)
When passing in doubles this works great. My example code for a proof of concept is as follows.
double avg = 0;
uint16_t i;
…

Michael
- 399
- 2
- 17