?
Solved

How to get current time from the system?

Posted on 2006-10-29
6
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

649 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