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;

}

相关内容

热门资讯

解谜透视!wepoker破解器... 解谜透视!wepoker破解器激活码,wepoker提高好牌率,阶段教程(有挂总结)-哔哩哔哩wep...
玩家必备科普!新星游房间链接辅... 玩家必备科普!新星游房间链接辅助,wpk俱乐部有没有辅助,要领教程(果真有挂)-哔哩哔哩1、新星游房...
有挂透视!aapoker怎么选... 有挂透视!aapoker怎么选牌,hhpoker免费透视脚本,烘培教程(新版有挂)-哔哩哔哩1、hh...
信息共享!广西老友玩友破解,w... 信息共享!广西老友玩友破解,wejoker黑侠辅助器,窍门教程(有挂讲解)-哔哩哔哩1、上手简单,内...
揭露透视!wepoker底牌透... 揭露透视!wepoker底牌透视,菠萝德普辅助器免费版在哪里,手筋教程(真的有挂)-哔哩哔哩菠萝德普...
必看攻略!!熊猫辅助器视频教程... 必看攻略!!熊猫辅助器视频教程,pokemmo脚本最新版,诀窍教程(今日头条)-哔哩哔哩1、熊猫辅助...
解迷透视!德普之星有透视辅助吗... 解迷透视!德普之星有透视辅助吗,wepoker怎么挂底牌,方针教程(有挂讲解)-哔哩哔哩1.德普之星...
一分钟揭秘!微乐自建房脚本免费... 一分钟揭秘!微乐自建房脚本免费下载,hhpoker透视工具,技法教程(有挂实锤)-哔哩哔哩1、金币登...
有挂透视!wepoker有人用... 有挂透视!wepoker有人用过吗,智星菠萝有挂吗,技法教程(有挂透明挂)-哔哩哔哩1、wepoke...
玩家必知教程!!微信微乐开挂,... 玩家必知教程!!微信微乐开挂,哈糖大菠萝万能挂,资料教程(有挂细节)-哔哩哔哩1、哈糖大菠萝万能挂脚...