C语言获取当前时间

一共有 两段 代码,一个是 获取当前时间 ,一个是 获取到现在的总毫秒数 ++求关注😄 互粉必回++

获取当前时间

#include <stdio.h>

#include <time.h>

int main() {

time_t rawtime;

struct tm * timeinfo;

char buffer[20];

// 获取当前时间

time(&rawtime);

timeinfo = localtime(&rawtime);

// 使用 strftime 来格式化时间

strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);

// 输出格式化后的时间

printf("当前时间是: %s\n", buffer);

return 0;

}

获取到现在的总毫秒数

#include <stdio.h>

#include <sys/time.h>

#include <time.h>

int main() {

struct timeval tv;

struct tm *now;

long total_milliseconds;

// 获取当前时间

gettimeofday(&tv, NULL);

// 将时间戳转换为可读的tm结构体

now = localtime(&tv.tv_sec);

// 计算总毫秒数

total_milliseconds = (now->tm_year + 1900) * 31536000000L; // 一年大约有31536000000毫秒

total_milliseconds += (now->tm_mon + 1) * 2628000000L; // 一个月大约有2628000000毫秒

total_milliseconds += now->tm_mday * 86400000L; // 一天有86400000毫秒

total_milliseconds += now->tm_hour * 3600000L; // 一小时有3600000毫秒

total_milliseconds += now->tm_min * 60000L; // 一分钟有60000毫秒

total_milliseconds += now->tm_sec * 1000L; // 一秒有1000毫秒

total_milliseconds += tv.tv_usec / 1000; // 将微秒转换为毫秒

// 输出总毫秒数

printf("总毫秒数: %ld\n", total_milliseconds);

return 0;

}

相关推荐
m0_6398171513 小时前
基于springboot教学资料管理系统【带源码和文档】
java·spring boot·后端
i***665013 小时前
SpringBoot实战(三十二)集成 ofdrw,实现 PDF 和 OFD 的转换、SM2 签署OFD
spring boot·后端·pdf
qq_124987075313 小时前
基于springboot的建筑业数据管理系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计
Want59514 小时前
C/C++跳动的爱心②
c语言·开发语言·c++
IT_陈寒14 小时前
Vite 5.0实战:10个你可能不知道的性能优化技巧与插件生态深度解析
前端·人工智能·后端
大牙Adela14 小时前
在Mac上通过Multipass虚拟机中的Ubuntu系统使用Graphviz工具
c语言·qt·ubuntu·macos·multipass·graphviz
z***33514 小时前
SQL Server2022版+SSMS安装教程(保姆级)
后端·python·flask
无限进步_14 小时前
C语言动态内存管理:掌握malloc、calloc、realloc和free的实战应用
c语言·开发语言·c++·git·算法·github·visual studio
zxguan15 小时前
Springboot 学习 之 下载接口 HttpMessageNotWritableException
spring boot·后端·学习