ctime/time.h  
Author Message
Pinguino Girl





PostPosted: Visual C++ General, ctime/time.h Top

Hi, I'm new to C++, but am writing my current project in C++ using VS2005. I want to use the POSIX clock_gettime function, like this:

#include "time.h"

void foo(void){

timespec curr_time;

clock_gettime(CLOCK_REALTIME, &curr_time);

}

This fails, not recognizing timespec, or clock_gettime, or CLOCK_REALTIME, even though the same code will compile fine using another development tool/compiler.

As an alternative I tried:

#include "time.h"

void foo(void){

time_t = time(NULL);

}

but this produced a linker error. How is it possible that gettin the current time is this hard If someone can help me I would really appreciate it. The POSIX option is my top choice and the time_t = time(NULL) option is my second choice, but I can't use any functions that are Microsoft-specific.

Thanks in advance!



Visual C++5  
 
 
Jonathan Caves - MSFT





PostPosted: Visual C++ General, ctime/time.h Top

Visual C++ doesn't support this POSIX function: or at least it doesn't appear in any of the header files on my machine.

 
 
Viorel.





PostPosted: Visual C++ General, ctime/time.h Top

This fragment of a console application is made in VS 2005:

 

#include "stdafx.h"

#include <time.h>

 

int _tmain(int argc, _TCHAR* argv[])

{

      time_t t = time(NULL);

      tm s;

      gmtime_s(&s, &t);

      printf("UTC Time:   %02i:%02i:%02i\n",

             s.tm_hour, s.tm_min, s.tm_sec);

      localtime_s(&s, &t);

      printf("Local Time: %02i:%02i:%02i\n",

             s.tm_hour, s.tm_min, s.tm_sec);

      return 0;

}

 

It is working with no problem.

 

In case of MFC applications, you can use CTime class.

 

In case of Windows API, you can use GetSystemTime function.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

What kind of linker error do you receive


 
 
Pinguino Girl





PostPosted: Visual C++ General, ctime/time.h Top

The error I get is "LNK2019: unresolved external symbol time referenced in function..."

So the odd thing here is that at compile time the function is accepted as part of time.h, but at link time I think maybe the project can't find the ctime library. How can I fix this If I need to add the reference to the library, how would I go about doing that

Thanks for your response!!!

- Rachel