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;
}
相关推荐
炸膛坦客9 小时前
单片机/C/C++八股:(二十六)IIC 专题(I²C)---- 上集
c语言·c++·单片机
灯澜忆梦9 小时前
GO_并发编程---定时器
开发语言·后端·golang
-银雾鸢尾-9 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-10 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
大模型码小白11 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
段一凡-华北理工大学12 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe13 小时前
C++——多态
开发语言·c++
心平气和量大福大13 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
从零开始的代码生活_15 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸15 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++