表示时间的三种类型

  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:" << + current_time->tm_year << endl;
cout << "month:" << + 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(维基)

【c】time.h的更多相关文章

  1. 【CodeForces】914 H. Ember and Storm's Tree Game 动态规划+排列组合

    [题目]H. Ember and Storm's Tree Game [题意]Zsnuoの博客 [算法]动态规划+排列组合 [题解]题目本身其实并不难,但是大量干扰因素让题目显得很神秘. 参考:Zsn ...

  2. 【一】 sched.h

    第一个数据结构体是 task_struct ,这个数据结构被内核用来表示进程,包含其所有信息. 定义于文件 include/linux/sched.h 中,先看看其完整定义 struct task_s ...

  3. 【转】 jni.h头文件详解(二)

    原文网址:http://blog.csdn.net/shaohuazuo/article/details/42932813 作者:左少华 博客:http://blog.csdn.net/shaohua ...

  4. 【转】stropts.h: No such file or directory – How to Fix

    原文地址:stropts.h: No such file or directory – How to Fix 作者:xjc2694 It is a known issue that modern Li ...

  5. 【转载】limits.h

    limits.h专门用于检测整型数据数据类型的表达值范围. <limits.h>主要提供对整型和字符型范围的限制的宏,同样没有指定类型和函数的定义. 1.整型宏如下表: 2.字符型宏如下表 ...

  6. 【图像处理】DVR H.264视频编码基本知识

    视频编码技术基本是由ISO/IEC制定的MPEG-x和ITU-T制定的H.26x两大系列视频编码国际标准的推出.从H.261视频编码建议,到 H.262/3.MPEG-1/2/4等都有一个共同的不断追 ...

  7. 【操作说明】全能型H.265播放器如何使用?

    本播放器集成了公司业务的接口,包含了实播,回放,云台控制和回放速度控制,截图和全屏功能可以根据type直接初始化接口地址如果是第三方业务对接,也可以单独配置接口地址 正确使用H.265播放器需要按以下 ...

  8. 【转载】OpenCV 摄像头控制

    参考:[OpenCV] -- 简单摄像头操作 - 代码人生 - 博客频道 - CSDN.NET http://blog.csdn.net/qiurisuixiang/article/details/8 ...

  9. 伸展树的基本操作——以【NOI2004】郁闷的出纳员为例

    前两天老师讲了伸展树……虽然一个月以前自己就一直在看平衡树这一部分的书籍,也仔细地研读过伸展树地操作代码,但是就是没写过程序……(大概也是在平衡树的复杂操作和长代码面前望而生畏了)但是今天借着老师布置 ...

随机推荐

  1. mac下wget用来仿站

    wget -c -r -np -k -L -p http://www.domain.com 参考 http://www.v2ex.com/t/147870

  2. SQL Server 2008安装和配置过程

    下面我将用图解的方式,来介绍SQL Server 2008安装和配置过程,希望对大家有所帮助. 闲言少叙,直奔主题!点击setup.exe安装文件后,如果系统没有以下组件,则会出现如下提示! 安装20 ...

  3. Using self-defined Parcelable objects during an Android AIDL RPC / IPC call

    Using self-defined Parcelable objects during an Android AIDL RPC / IPC call In my previous post “Usi ...

  4. SiteMesh3 介绍和使用

        Sitemesh是由一个基于Web页面布局.装饰及与现存Web应用整合的框架.它能帮助我们再由大量页面工程的项目中创建一致的页面布局和外观,如一 致的导航条.一致的banner.一致的版权等. ...

  5. c++ read

    #include <fstream> #include <iostream> int main(void) { ] = {}; ; std::ifstream read(&qu ...

  6. 学习Ember遇到的一些问题

    1.在模板中不能省略结束标签: 在Ember的模板中,如果省略结束标签的话,会有好多无解的问题(可能是:不更新.更新后结构不对.model和view不同步等),苦苦找了很久.... 2.childVi ...

  7. Codeforces Beta Round #10 D. LCIS

    题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...

  8. 在Eclipse中使用Propertites Editor插件来解决property文件中文显示乱码

    在一般情况下,propertites文件在eclipse中的显示中文一直显示乱码,想要解决这个问题,需要通过在eclipse中安装一个Propertites Editor插件来进行解决. 在Eclip ...

  9. jquery中的事件

    一.事件参数   function(event){} 1.停止冒泡事件  event.stopPropagation()  <=>  return false;2.阻止默认行为  even ...

  10. javascript中alert()与console.log()的区别

    我们在做js调试的时候使用 alert 可以显示信息,调试程序,alert 弹出窗口会中断程序, 如果要在循环中显示信息,手点击关闭窗口都累死.而且 alert 显示对象永远显示为[object ]. ...