Mohammad Hasan
Mohammad Hasan

Reputation: 143

getcurrent time on second or millisecond

I am doing client server program.I need to know time when i send the filename to the server and also need to know the time of receiving file and difference between two time.

What does the following program mean?

The following program has three printf but i can understand which one is perfect for me?

int main(void)
{
  char buffer[30];
  struct timeval tv;

  time_t curtime,t;

  gettimeofday(&tv, NULL); 
  curtime=tv.tv_sec;
  t=mktime(localtime(&curtime));
  printf("%ld\n",t);
  printf("%ld\n",localtime(&curtime));
  strftime(buffer,30,"%m-%d-%Y  %T.",localtime(&curtime));
  printf("%s%ld\n",buffer,tv.tv_usec);
  return 0;
}

Upvotes: 2

Views: 1875

Answers (2)

utarid
utarid

Reputation: 1715

getting current time with milliseconds : http://ideone.com/RBqisP

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

int main(int argc, char *argv[]) 
{
    char cBuffer[100];
    time_t zaman;
    struct tm *ltime;
    static struct timeval _t;
    static struct timezone tz;

    time(&zaman);
    ltime = (struct tm *) localtime(&zaman);
    gettimeofday(&_t, &tz);

    strftime(cBuffer,40,"%d.%m.%y %H:%M:%S",ltime);
    sprintf(cBuffer, "%s.%d", cBuffer,(int)_t.tv_usec);

    printf(" %s \n",cBuffer);

    return 0;
}

duration : http://ideone.com/NvXEQv

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
    double cBuffer1;
    double cBuffer2;
    time_t zaman;
    struct tm *ltime;
    static struct timeval _t;
    static struct timezone tz;

    time(&zaman);
    ltime = (struct tm *) localtime(&zaman);

    gettimeofday(&_t, &tz);
    cBuffer1 = (double)_t.tv_sec + (double)_t.tv_usec/(1000*1000);
    printf(" %f \n",cBuffer1);

    sleep(1);

    gettimeofday(&_t, &tz);
    cBuffer2 = (double)_t.tv_sec + (double)_t.tv_usec/(1000*1000);  
    printf(" %f \n", cBuffer2);

    printf(" duration : %f \n", cBuffer2-cBuffer1);

    return 0;
}

Upvotes: 1

Corey Cothrum
Corey Cothrum

Reputation: 56

gettimeofday() returns a timeval struct:

struct timeval { 
    time_t      tv_sec;     /* seconds since the Epoch*/
    suseconds_t tv_usec;    /* microseconds since the Epoch*/
};

You can access this info directly, without using any of the formatting functions you have above [ localtime() or mktime() ]:

int main(void)
{
  struct timeval tv;

  gettimeofday(&tv, NULL);

  printf("seconds: %u :: microseconds: %u\n", (unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec);
  return 0;
}

To compute elapsed time, we need to get the time twice, and then compute the delta between them:

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

int main(void)
{
  struct timeval tv_start, tv_end;

  gettimeofday(&tv_start, NULL);

  /* do something */
  sleep(5);

  gettimeofday(&tv_end, NULL);

  printf("tv_start\n seconds: %u :: microseconds: %u\n\n", (unsigned int)tv_start.tv_sec, (unsigned int)tv_start.tv_usec);
  printf("tv_end\n seconds: %u :: microseconds: %u\n\n", (unsigned int)tv_end.tv_sec, (unsigned int)tv_end.tv_usec);

  /* convert both timeval structs to pure microseconds, and then subtract */
  int deltaInUSecs = (tv_end.tv_sec - tv_start.tv_sec)*1000000 - (tv_end.tv_usec - tv_start.tv_usec);
  printf("delta in microseconds\n %u\n\n", deltaInUSecs);

  return 0;
}

Upvotes: 1

Related Questions