Solved

How to get current time from the system?

Posted on 2006-10-29
6
219 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

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.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

840 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