美文网首页
C语言显示当前时间

C语言显示当前时间

作者: StormerX | 来源:发表于2020-06-27 19:36 被阅读0次
#include <stdio.h>
#include <time.h>
int main(void)
{
   char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
   time_t t;
   struct tm *p;
   int year = 0, mon = 0, mday = 0;
   int hour = 0, min = 0, sec = 0;
   char *week = NULL;

   time(&t);
   p = gmtime(&t);
   year = 1900 + p->tm_year;
   mon = 1 + p->tm_mon;
   mday = p->tm_mday;
   week = wday[p->tm_wday];
   hour = 8 + p->tm_hour; //获取当地时间,与UTC时间相差8小时   
   min = p->tm_min;
   sec = p->tm_sec;
   printf("%d-%d-%d %s", year, mon, mday, week);
   printf(" %.2d:%.2d:%.2d\n", hour, min, sec);

   return 0;
}

相关文章

网友评论

      本文标题:C语言显示当前时间

      本文链接:https://www.haomeiwen.com/subject/advvfktx.html