【发布时间】:2017-05-02 07:17:30
【问题描述】:
如何从 C 中的 gettimeofday 获取日期时间? 我需要将 tv.tv_sec 转换为 Hour:Minute:Second xx 没有 localtime 和 strftime 等功能......,只需通过计算得到它。例如 tv.tv_sec/60)%60 将是分钟
#include <stdio.h>
#include <time.h>
#include<sys/time.h>
int main ()
{
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
printf("TimeZone-1=%d\n", tz.tz_minuteswest);
printf("TimeZone-2=%d\n", tz.tz_dsttime);
printf("TimeVal-3=%d\n", tv.tv_sec);
printf("TimeVal-4=%d\n", tv.tv_usec);
printf ( "Current local time and date: %d-%d\n", (tv.tv_sec% (24*60*60)/3600,tz.tz_minuteswest);
return 0;
}
如何通过计算 tv 和 tz 来获取系统当前的 Hour,以及获取 Minute、Second 和 MS ~
【问题讨论】:
-
欢迎来到 Stack Overflow。请尽快阅读About 页面并访问描述How to Ask a Question 和How to create a Minimal, Complete, and Verifiable example 的链接。提供必要的详细信息,包括您的代码和相关错误(如果有),将允许这里的每个人帮助您解决问题。
标签: c gettimeofday