C语言获取当前时间

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

获取当前时间

#include <stdio.h>

#include <time.h>

int main() {

time_t rawtime;

struct tm * timeinfo;

char buffer20;

// 获取当前时间

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;

}

相关推荐
是隼人2 分钟前
buuctf-pwn pwnable_start(ret2shellcode)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
stark张宇36 分钟前
分布式事务最全图解(6种方案):从强一致的2PC到最终对账,彻底搞懂数据一致性
分布式·后端
IT毕设实战小研1 小时前
基于大数据的WTA职业网球赛事演变与竞技格局数据可视化分析
大数据·后端·爬虫·python·信息可视化·课程设计
ysu_03141 小时前
02-单链表完全指南
c语言·数据结构·算法·leetcode
IT_陈寒1 小时前
Java字符串判等踩坑记:==和equals真的不能乱用
前端·人工智能·后端
ltqshs1 小时前
C语言-链表例程
c语言·开发语言·链表
YYYing.1 小时前
【C++进阶系列 (一)】关于线程堆栈的那些事 (上篇)
c语言·开发语言·c++·线程堆栈
Discipline~Hai1 小时前
Linux网络编程05-sqlite3数据库
linux·c语言·网络·数据库·sqlite·linux应用软件编程
小马过河R2 小时前
微信小程序自定义登录态维护:从入门到生产级落地
后端·微信小程序·小程序·架构·登录态
计算机学姐2 小时前
基于SpringBoot的高校爱心慈善管理系统
java·vue.js·spring boot·后端·spring·tomcat·mybatis