1

I try to create a code for generating of timestamp. I found two different structures, but its not clear what is the difference? Can anybody help me to understand what is the difference between these two structures? struct timespec and timespec now?

yole
  • 92,896
  • 20
  • 260
  • 197
check
  • 33
  • 1
  • 5
  • See https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c – sashoalm Mar 07 '15 at 13:48
  • It doesnt answer my question. If I dont use struct I schould write typedef struct. Is there a diference if I write struct timespec or just timespec? – check Mar 07 '15 at 13:56
  • @check There's no such type as just `timespec`, so you can't use that in C code unless you've created a typedef yourself . (Though if your code is C++, it's a different matter - `timespec` and `struct timespec` would be the same) – nos Mar 07 '15 at 17:31

1 Answers1

2

The following structure:

struct timespec

is provided by the POSIX.1b standard, defined in time.h. From the GNU libc documentation, 21.2 Elapsed Time:

Data Type: struct timespec The struct timespec structure represents an elapsed time. It is declared in time.h and has the following members:

long int tv_sec This represents the number of whole seconds of elapsed time.

long int tv_nsec This is the rest of the elapsed time (a fraction of a second), represented as the number of nanoseconds. It is always less than one billion.

If you saw the following:

timespec now

Someone probably wrote a typedef for the struct, and "now" would refer to a variable name. If you wrote a typedef for the struct, there would be no difference between "struct timespec" and "timespec".

There are many functions in time.h that can help you. You can find full documentation here: http://pubs.opengroup.org/onlinepubs/007908775/xsh/time.h.html

Dan Paulat
  • 152
  • 6