当前位置: 首页 > news >正文

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;

}

http://www.lryc.cn/news/393937.html

相关文章:

  • 【每日一练】python三目运算符的用法
  • CentOS 7.9 停止维护(2024-6-30)后可用在线yum源 —— 筑梦之路
  • Git 常用命令备忘
  • Ubuntu24.04安装Skynet环境
  • 【C++】cout.self()函数
  • VueQuill 富文本编辑器技术文档快速上手
  • 链式二叉树oj题
  • Curator 是一个开源工具为 Elasticsearch 集群设计,用于自动化索引的维护任务。
  • STM32-PWR和WDG看门狗
  • C++循环队列 经典示例
  • 【程序大侠传】大表分库分表切换数据库类型导致pagehelper生成sql语法报错
  • 7、Redis 队列与 Stream
  • FFT剖析
  • git clone报错RPC failed; curl 92 HTTP/2 stream 7 was not closed cleanly
  • Apispec,一个用于生成 OpenAPI(Swagger)规范的 Python 库
  • SpringBoot 自定义异常返回数据格式
  • 【xinference】(15):在compshare上,使用docker-compose运行xinference和chatgpt-web项目,配置成功!!!
  • 【Unity 3D角色移动】
  • 个人视角,社会影响力:自媒体的魅力所在
  • 算法训练营day70
  • EtherCAT转Profinet网关配置说明第二讲:上位机软件配置
  • 日志自动分析-Web---360星图GoaccessALBAnolog
  • 【面试八股文】java基础知识
  • ssrf结合redis未授权getshell
  • 魔法自如:精通 IPython %automagic 命令的切换艺术
  • 基于CentOS Stream 9平台搭建MinIO以及开机自启
  • shell-awk语法整理
  • 关于忠诚:忠于自己的良知、理想、信念
  • 探索Linux:开源世界的无限可能
  • 深度学习之半监督学习:一文梳理目标检测中的半监督学习策略