C语言获取当前时间
创始人
2025-01-08 05:34:46
0

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

获取当前时间

#include

#include

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

#include

#include

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;

}

相关内容

热门资讯

透视肯定!aapoker辅助是... 透视肯定!aapoker辅助是真的吗(透视)详细辅助2025新版(确实真的有挂)1、许多玩家不知道a...
透视代打!wepoker辅助器... 透视代打!wepoker辅助器软件下载(透视)原先是有挂(详细辅助黑科技教程);1分钟了解详细教程(...
透视脚本!wepoke真的有挂... 透视脚本!wepoke真的有挂,wepoke软件透明挂测试,详细透视科技教程1、这是跨平台的wepo...
透视好友房!wepoker底牌... 透视好友房!wepoker底牌透视脚本怎样安装(透视)详细辅助分享教程(竟然存在有挂)1、玩家可以在...
透视免费!wepoker-h5... 自定义wepoker-h5下载系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助...
透视神器!wepoke辅助德之... 透视神器!wepoke辅助德之星,wopoker系统机制,详细透视普及教程1、起透看视 wepoke...
透视模拟器!wepoker脚本... 透视模拟器!wepoker脚本下载(透视)详细辅助可靠教程(果然有挂);一、wepoker脚本下载软...
透视ai!wepoker正确养... 透视ai!wepoker正确养号方法(透视)好像有挂(详细辅助攻略教程);1分钟了解详细教程(微信 ...
透视挂透视!wepoke一定有... 透视挂透视!wepoke一定有挂,wopoker苹果怎么下载,详细透视透明挂教程1、起透看视 wep...
透视有挂!wepoker辅助工... 透视有挂!wepoker辅助工具(透视)详细辅助科技教程(确实存在有挂)wepoker辅助工具辅助器...