1
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,

1
  • check first replacing _ftime to ftime, include time.h, and sys/time.h Commented Jan 7, 2011 at 5:22

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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...

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.