Solved

How to get current time from the system?

Posted on 2006-10-29
6
217 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 141
C: GetDiskFreeSpaceEx() for default drive 14 104
UPD maximums on Red Hat 6 110
Setting nameservers after res_init fails doing res_query 2 86
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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 how to use strings and some functions related to them in the C programming language.

920 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