Solved

How to get current time from the system?

Posted on 2006-10-29
6
216 Views
Last Modified: 2010-05-18
I am using Linux Fedora Core 5.
What is the api to get the current time up to millisecond from the system?
0
Comment
Question by:william007
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 17830004
man 2 gettimeofday

Will get the system clock time to microsecond resolution, actually.
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 400 total points
ID: 17830012
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
 
LVL 6

Assisted Solution

by:_iskywalker_
_iskywalker_ earned 100 total points
ID: 17842884
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17842969
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
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 400 total points
ID: 17843047
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
 
LVL 9

Author Comment

by:william007
ID: 17853550
Thanks=)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now