• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

How to get current time from the system?

I am using Linux Fedora Core 5.
What is the api to get the current time up to millisecond from the system?
0
william007
Asked:
william007
  • 3
  • 2
3 Solutions
 
MysidiaCommented:
man 2 gettimeofday

Will get the system clock time to microsecond resolution, actually.
0
 
MysidiaCommented:
This is an excerpt from the Linux Programmer's Manual, refer to that documentation on your system
with the command above, or  do a google search for gettimeofday:

"GETTIMEOFDAY(2)     Linux Programmer's Manual     GETTIMEOFDAY(2)

NAME
       gettimeofday, settimeofday - get / set time

SYNOPSIS
       #include <sys/time.h>
       #include <unistd.h>

       int gettimeofday(struct timeval *tv, struct timezone *tz);
       int settimeofday(const struct timeval *tv , const struct timezone *tz);

DESCRIPTION
       gettimeofday and settimeofday can set the time as well as a timezone.  tv is a timeval struct, as specified  in
       /usr/include/sys/time.h.
"
0
 
_iskywalker_Commented:
a program code:
struct timeval tv;
gettimeofday(&tv,NULL);
printf( " sec %i mili %i \n",tv.sec, tv.usec/1000);
the tv.usec must be divided by 1000 since it is the micro seconds (greek mu).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
_iskywalker_Commented:
what you mean the current time up?
if you mean uptime:
http://www.tutorials.de/forum/c-c/239813-gnu-c-gettickcount-unter-linux.html

check man 2 times
0
 
MysidiaCommented:
Of course that all depends on how you intend to format the time when you get it.

Don't necessarily need to divide anything:

struct timeval val;

if ( gettimeofday(&val, NULL) != -1 ) {
        while(val.tv_usec >= 1000000) {
              val.tv_sec++;
              val.tv_usec -= 1000000;
        }
     printf("time = %d.%.05d\n",  val.tv_sec,  val.tv_usec);
}
0
 
william007Author Commented:
Thanks=)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now