ant2009
ant2009

Reputation: 22486

converting source code from window to linux

c++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

I am converting some source code from windows to running on ubuntu.

However, the windows is using the code below to log messages and get the current time the message is logged. However, this is windows. What is the best way to convert this to running on linux?

struct  _timeb timebuffer;
char    *timeline;

_ftime(&timebuffer);
timeline = ctime(&(timebuffer.time));

Many thanks for any advice,

Upvotes: 1

Views: 2904

Answers (3)

techcraver
techcraver

Reputation: 411

Use Boost libraries (datetime) as your questions look to be more about date/time. Most of the boost lib is portable. If you look for GUI libraries QT is the best one which is again portable b/w windows/*nix.

Upvotes: 0

Tony Delroy
Tony Delroy

Reputation: 106096

The windows code is calling ctime, which is available on Linux too and prepares a date-time string such as:

"Wed Jun 30 21:49:08 1993\n"

The man page for ctime() on Linux documents:

char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);

So, you don't want to use ftime() on linux... that populates a struct timeb buffer which isn't accepted by ctime().

Instead, you can get/use a time_t value using the time() function as in:

#include <time.h>
time_t my_time_t;
time(&my_time_t);
if (my_time_t == -1)
    // error...
char buffer[26]; // yes - a magic number - see the man page
if (ctime_r(&my_time_t, buffer) == NULL)
    // error...
// textual representation of current date/time in buffer...

Upvotes: 0

hhafez
hhafez

Reputation: 39750

in linux a similar function ftime is used which gives you the time do

 #include <sys/timeb.h>
 /* .... */
 struct timeb tp;
 ftime(&tp);
 printf("time = %ld.%d\n",tp.time,tp.millitm);

this will give you the time in second and milliseconds.

Upvotes: 3

Related Questions