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;

}

相关内容

热门资讯

出现新变化!pokermast... 出现新变化!pokermaster破解版,微信小游戏万能修改器,一贯真的有辅助脚本(哔哩哔哩)1、游...
透视诀窍!werplan外卦神... 透视诀窍!werplan外卦神器(透视)切实存在有辅助修改器(哔哩哔哩)小薇(辅助器软件下载)致您一...
2024教程!微乐小程序黑科技... 2024教程!微乐小程序黑科技(外挂),微乐挖坑内购破解确实有挂方法1、完成微乐小程序黑科技辅助器v...
解谜透视!佛手大菠萝13道挂哪... 解谜透视!佛手大菠萝13道挂哪里,newpoker怎么安装脚本(透视)总是是有辅助app(哔哩哔哩)...
备受关注的!hhpoker为什... 备受关注的!hhpoker为什么一直输,微信大厅辅助挂,好像真的有辅助工具(哔哩哔哩)1)微信大厅辅...
透视指南!poker mast... 透视指南!poker master辅助(透视)好像真的有辅助平台(哔哩哔哩)小薇(辅助器软件下载)致...
一分钟了解!!微信小程序黑科技... 一分钟了解!!微信小程序黑科技(外挂),微乐自建房怎么提高胜率原来有挂秘笈亲,关键说明,微信小程序黑...
推荐透视!竞技联盟辅助,we ... 推荐透视!竞技联盟辅助,we poker免费辅助器(透视)切实是真的有辅助工具(哔哩哔哩)1、we ...
据了解!hhpoker到底可以... 据了解!hhpoker到底可以作必弊吗,呗兔辅助怎么样,好像有辅助脚本(哔哩哔哩)一、呗兔辅助怎么样...
总算清楚!!微乐小程序免费黑科... 您好,微乐小程序免费黑科技这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很...