关键词搜索

源码搜索 ×
×

如何利用C语言来获取当地时间和UTC时间? (设当地时间是北京时间)

发布2013-10-27浏览31520次

详情内容

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main()
  4. {
  5. time_t t = time(NULL);
  6. tm *tp = localtime(&t);
  7. // 北京时间
  8. printf("%d/%d/%d\n", tp->tm_mon+1, tp->tm_mday, tp->tm_year + 1900);
  9. printf("%d:%d:%d\n", tp->tm_hour, tp->tm_min, tp->tm_sec);
  10. return 0;
  11. }

 

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main()
  4. {
  5. time_t t = time(NULL); // UTC秒数
  6. tm *tp = localtime(&t);
  7. // 北京时间
  8. printf("%d/%d/%d\n", tp->tm_mon+1, tp->tm_mday, tp->tm_year + 1900);
  9. printf("%d:%d:%d\n", tp->tm_hour, tp->tm_min, tp->tm_sec);
  10. // UTC今天秒数
  11. int second1 = t % (24 * 60 * 60);
  12. printf("%d\n", second1);
  13. // 北京时间今天秒数
  14. int second2 = tp->tm_hour * 3600 + tp->tm_min * 60 + tp->tm_sec;
  15. printf("%d\n", second2);
  16. // UTC时间与北京时间相隔的小时数
  17. printf("%d\n", (second1 - second2) / 3600); // -8
  18. return 0;
  19. }

 

  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <wtypes.h>
  4. int main(int argc, char* argv[])
  5. {
  6. SYSTEMTIME stCurTime = {0};
  7. GetSystemTime(&stCurTime); // UTC时间
  8. printf("%d/%d/%d\n", stCurTime.wMonth, stCurTime.wDay, stCurTime.wYear);
  9. printf("%d:%d:%d\n", stCurTime.wHour, stCurTime.wMinute, stCurTime.wSecond);
  10. printf("----------\n");
  11. GetLocalTime(&stCurTime); // 北京时间
  12. printf("%d/%d/%d\n", stCurTime.wMonth, stCurTime.wDay, stCurTime.wYear);
  13. printf("%d:%d:%d\n", stCurTime.wHour, stCurTime.wMinute, stCurTime.wSecond);
  14. return 0;
  15. }

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载