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;
}
相关推荐
怪我冷i1 小时前
zig语言学习笔记——heap-memory
开发语言·golang·zig
.千余2 小时前
【C++】手写双向链表:list容器模拟实现
开发语言·c++·笔记·学习·其他
人道领域2 小时前
【LeetCode刷题日记】93.复原IP地址
java·开发语言·算法·leetcode
caimouse2 小时前
Reactos 第 3 章 内存管理 — 【中篇】Hyperspace、系统空间、API 与异常
c语言·开发语言·windows·架构
摇滚侠3 小时前
JavaWeb 全套教程 Listener 112-113
java·开发语言·servlet·tomcat·intellij-idea
ysu_03143 小时前
leetcode数据结构与算法1~4
c语言·数据结构·学习·算法·leetcode
hixiong1233 小时前
C# Tokenizers.DotNet测试工具
开发语言·人工智能·llm
曹牧3 小时前
Java:Deprecated 是
java·开发语言
caimouse3 小时前
Reactos 第 4 章 对象管理 — 4.1 对象与对象目录
服务器·c语言·开发语言·windows·架构