【问题标题】:getcurrent time on second or millisecond以秒或毫秒获取当前时间
【发布时间】:2013-12-01 04:06:00
【问题描述】:

我正在做客户端服务器程序。我需要知道我将文件名发送到服务器的时间,还需要知道接收文件的时间和两个时间之间的差异。

以下程序是什么意思?

以下程序有三个 printf 但我能理解哪一个最适合我?

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;
}

【问题讨论】:

  • 你能在你的问题中包含输出吗?
  • 第 1 次 1385872297 第 2 次 140692907040544 第 3 次 11-30-2013 22:31:37.476092

标签: c


【解决方案1】:

以毫秒为单位获取当前时间: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;
}

持续时间: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;
}

【讨论】:

    【解决方案2】:

    gettimeofday() 返回一个 timeval 结构:

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

    您可以直接访问此信息,而无需使用上述任何格式化函数 [localtime() 或 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;
    }
    

    要计算经过的时间,我们需要两次获取时间,然后计算它们之间的增量:

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-25
      • 2017-02-20
      • 1970-01-01
      • 2023-03-06
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多