C中日历时间转换

日历时间转换

cpp 复制代码
#include <stdio.h>
#include <time.h>

int main()
{
   time_t timep;
   struct tm *localtmp;
   struct tm *gmtmp;
   timep = time(NULL);
   gmtmp = gmtime(&timep);
   printf("gmtmp      :%s", asctime(gmtmp));
   localtmp = localtime(&timep);
   printf("localtmp   :%s", asctime(localtmp));
   printf("ctime      :%s", ctime(&timep));

   timep = mktime(gmtmp);
   printf("gmtieme   :%ld\n",timep);
   timep = mktime(localtmp);
   printf("localtmp  :%ld\n",timep);
}

指定日期转换形式

cpp 复制代码
#include <stdio.h>
#include <time.h>
 
int main ()
{
   time_t rawtime;
   struct tm *info;
   char buffer[80];

   time(&rawtime);
   info = localtime(&rawtime);

   strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
   printf("本地时间 : |%s|\n", buffer);

   info = gmtime(&rawtime);
   strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
   printf("UTC时间  : |%s|\n", buffer);
   return (0);
}

获取时间

cpp 复制代码
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

int main(void)
{
    struct timeval tvStart, tvEnd;
    struct timespec timeStart, timeEnd;
    double fTimeInterval = 0.0;
    int i = 0;

    for (i = 0; i < 3; i++)
    {
        // --- Test gettimeofday ---
        gettimeofday(&tvStart, NULL);
        usleep(50000); // Delay for 50ms
        gettimeofday(&tvEnd, NULL);

        // Calculate elapsed time in milliseconds
        fTimeInterval = (tvEnd.tv_sec - tvStart.tv_sec) * 1000.0 +
                        (tvEnd.tv_usec - tvStart.tv_usec) * 0.001;

        printf("gettimeofday  =  %f ms\n", fTimeInterval);

        // --- Test clock_gettime ---
        clock_gettime(CLOCK_REALTIME, &timeStart);
        usleep(50000); // Delay for 50ms
        clock_gettime(CLOCK_REALTIME, &timeEnd);

        // Calculate elapsed time in milliseconds
        fTimeInterval = (timeEnd.tv_sec - timeStart.tv_sec) * 1000.0 +
                        (timeEnd.tv_nsec - timeStart.tv_nsec) * 0.000001;

        printf("clock_gettime =  %f ms\n", fTimeInterval);
    }

    return 0;
}
相关推荐
Tizzy JJ8 小时前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
海兰8 小时前
【 Python 量化交易】第8章:量化工具箱
开发语言·python
牛油果子哥q8 小时前
C++大型项目工程精讲:CMake完整实战、静态库&动态库、模块化拆分、单元测试、gdb调试、性能工具、工程踩坑全解
开发语言·c++·单元测试
Dream Cosmos8 小时前
C++ 多态上篇:从 virtual 到抽象类,彻底理解多态的使用
开发语言·c++
TheBestRucy8 小时前
Python九阳神功之柒:数据分析三剑客·执剑问道
开发语言·python·数据分析
qq_339191148 小时前
go cpu占比高排查,cpu100%排查,go pprof cpu命令
开发语言·后端·golang
西西弗Sisyphus8 小时前
Qt 配置文件图标和文件版本信息
开发语言·qt
AAA代码批发商8 小时前
Days37 Linux C 网络编程:UDP、TCP 与 HTTP 协议实战详解
linux·c语言·网络
秋田君8 小时前
Qt_Qt(c++)开发中常见错误与解决方法
开发语言·c++·qt
夏霞9 小时前
c# 不支持的目标框架 解决方案
开发语言·c#