【c】time.h

简介:

表示时间的三种类型

  1. 日历时间:从一个时间点到现在的秒数,用time_t表示
  2. 始终滴答时间:从进程启动到现在时钟的滴答数(每秒一般包含1000个)。用clock_t表示
  3. 分解时间:分解的数据结构如下。用tm表示

                              

 

从计算机里获得时间的方法

  1. tim_t time(time_t *time),从某一时间到现在的秒数,一般问1970年1月1日午夜
  2. clock_t clock(void),从进程启动到现在累计的始终滴答数
复制代码
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    time_t times = time(NULL);
    const clock_t clocks = clock();
    cout << "times:" << times << endl;
    cout << "clocks:" << clocks << endl;

    cout << "-------------------" << endl;
    const time_t times2 = time(&times);
    cout << "times2:" << times2 << endl;
}
复制代码

结果

 

三种时间转换函数

  1. struct tm* gmtime(const time_t* time)
  2. struct tm* localtime(const time_t* time)
  3. time_t mktime(struct tm* ptm)

 

时间日期数据个格式化输出

  1. char* asctime(const struct tm* tmptr)  周几 月份数 日数 小时数:分钟数:秒钟数 年份数
  2. char* ctime(const time_t* time)  周几 月份数 日数 小时数:分钟数:秒钟数 年份数

对时间的操作

double difftime(time_t time2, time_t time1)

 

案例

复制代码
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    const time_t t = time(NULL);
    cout << "second from (1970.01.01:00:00)" << t << endl;
    tm* current_time = localtime(&t);

    cout << "year:" << 1900 + current_time->tm_year << endl;
    cout << "month:" << 1 + current_time->tm_mon << endl;

    cout << "=--------------=" << endl;
    cout << "day of year:" << current_time->tm_yday << endl;
    cout << "day of month:" << current_time->tm_mday << endl;
    cout << "day of week:" << current_time->tm_wday << endl;
    cout << "=--------------=" << endl;

    cout << "hour:" << current_time->tm_hour << endl;
    cout << "minute:" << current_time->tm_min << endl;
    cout << "second:" << current_time->tm_sec << endl;
    cout << "------------" << endl;
    cout << "time:" << ctime(&t) << endl;
    cout << "time:" << asctime(current_time) << endl;

}
复制代码

结果

 

参考

time.h(维基)





本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3683301.html,如需转载请自行联系原作者

相关文章
|
Web App开发 域名解析 缓存
如何在 Ubuntu 20.04 上安装 Node.js 和 npm
本文我们主要为大家介绍在 Ubuntu 20.04 上安装 Node.js 和 npm 的三种不同的方式。
161777 7
如何在 Ubuntu 20.04 上安装 Node.js 和 npm
|
数据采集 存储 JSON
【一文读不懂Jsoncpp】3.序列化和反序列化
【一文读不懂Jsoncpp】3.序列化和反序列化
280 0
|
开发工具 git
git blame
git blame 是一个 Git 命令,用于显示某个文件中每一行代码的修改历史。它会显示每行代码的最后一次修改者、修改日期和修改内容。通过 git blame 命令,你可以轻松追踪代码的修改记录,了解团队成员在开发过程中的协作情况。
390 10
|
编译器 C语言 C++
VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程
VSCode上搭建C/C++开发环境(vscode配置c/c++环境)Windows系统---保姆级教程
8498 0
|
存储 算法 知识图谱
RGB与YUV相互转换
本文介绍YUV跟RGB互转的各种公式,以及推导原理
540 0
成功解决ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or
成功解决ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or
|
6天前
|
人工智能 运维 安全
|
3天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
5天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
499 14