?
Solved

getting system time using gettimeofday()

Posted on 2003-03-23
6
Medium Priority
?
407 Views
Last Modified: 2010-04-09
How can I get the system time in the format "HH:MM:SS:XXX:YYY", where XXX is miliseconds and YYY is microseconds, using the function gettimeofday()?

Thanks in advance

Leal
0
Comment
Question by:fileal2
[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
  • 4
6 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 300 total points
ID: 8191931
gettimeofday will get you integer only , u shud use .. strftime to get the time in your favorite format

http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?strftime+3

look at the manpage .. and from the manpage itself  here is an example

strftime(str, strsize, "%A %b %d %j", tmptr)
     This results in str containing "Thursday Aug 28 240".

u'll also need localtime() to convert integer-time to the localtime format..
try these things out and we can be of help whereever u r stuck
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8191989
for getimeofday ,by integer time i meant, it doesnt break time into hours ,minutes and secods ,, it gives as seconds and microseconds since  January 1, 1970, and stores it in the passed structure tp.

so u need to apply localtime() on the number of seconds part of the structure  filled up by gettimeofday.

and structure filled up by locatime will be used by strftime ..
if u dont have strftime or similar customized time formatting functions..
 then u need to sprintf the values in a string by urself.. in the format that u have decided


0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8192369
here is a sample from the time format function i use .. (for me it works  on linux and aix)
its a bit more than what u need .. but u'll get an idea how it works.

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

#define HAVE_STRFTIME

int hires =1; //high resolution

char *timestring(int hires)
{
     static char TimeBuf[256];
    struct timeval tp;
    time_t t;
    struct tm *tm;
     char *ptr;
     if (hires) {
     gettimeofday(&tp,NULL);
     t = (time_t)tp.tv_sec;
  } else {
     t = time(NULL);
  }
        tm = localtime(&t);
        if (!tm) {
        printf("didnt get tm\n");
                if (hires) {
                        sprintf(TimeBuf,
                                 "%ld.%06ld seconds since the Epoch",
                                 (long)tp.tv_sec,
                                 (long)tp.tv_usec);
                } else {
                        sprintf(TimeBuf,
                                 "%ld seconds since the Epoch",
                                 (long)t);
               }

        } else {
#ifdef HAVE_STRFTIME
                if (hires) {

          strftime(TimeBuf,sizeof(TimeBuf)-1,"%H:%M:%S",tm);
                        sprintf(TimeBuf+strlen(TimeBuf),
                                 ":%06ld",
                                 (long)tp.tv_usec);
                } else {
                        strftime(TimeBuf,100,"%H:%M:%S",tm);
                }
#else
                if (hires) {
                        sprintf(TimeBuf,
                                 "%s:%06ld",
                                 asctime(tm),
                                 (long)tp.tv_usec);
                } else {
                        strcpy(TimeBuf, asctime(tm));
                }
#endif
        }

return(TimeBuf);
}

main(){
printf("%s\n",timestring(1));
}
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8899429
when will this be closed
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10189706
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by akshayxx.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
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!

Question has a verified solution.

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

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…
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…
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.
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.
Suggested Courses

762 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