C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。
有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。
结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:
struct tm {
int tm_sec; // 秒,正常范围从 0 到 59,但允许至 61
int tm_min; // 分,范围从 0 到 59
int tm_hour; // 小时,范围从 0 到 23
int tm_mday; // 一月中的第几天,范围从 1 到 31
int tm_mon; // 月,范围从 0 到 11
int tm_year; // 自 1900 年起的年数
int tm_wday; // 一周中的第几天,范围从 0 到 6,从星期日算起
int tm_yday; // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
int tm_isdst; // 夏令时
}
下面是 C/C++ 中关于日期和时间的重要函数。
time_t time(time_t *time);
该函数返回系统的当前日历时间,自 年 月 日以来经过的秒数。如果系统没有时间,则返回 .。
char *ctime(const time_t *time);
该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\。
struct tm *localtime(const time_t *time);
该函数返回一个指向表示本地时间的 tm 结构的指针。
clock_t clock(void);
该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 .。
char * asctime ( const struct tm * time );
该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\。
struct tm *gmtime(const time_t *time);
该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
time_t mktime(struct tm *time);
该函数返回日历时间,相当于 time 所指向结构中存储的时间。
double difftime ( time_t time2, time_t time1 );
该函数返回 time1 和 time2 之间相差的秒数。
size_t strftime();
该函数可用于格式化日期和时间为指定的格式。
下面的实例获取当前系统的日期和时间,包括本地时间和协调世界时(UTC)。
#include <iostream>
#include <ctime> using namespace std; int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(); // 把 now 转换为字符串形式
char* dt = ctime(&now); cout << "本地日期和时间:" << dt << endl; // 把 now 转换为 tm 结构
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "UTC 日期和时间:"<< dt << endl;
}
使用结构 tm 格式化时间
tm 结构在 C/C++ 中处理日期和时间相关的操作时,显得尤为重要。tm 结构以 C 结构的形式保存日期和时间。大多数与时间相关的函数都使用了 tm 结构。下面的实例使用了 tm 结构和各种与日期和时间相关的函数。
#include <iostream>
#include <ctime> using namespace std; int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(); cout << "1970 到目前经过秒数:" << now << endl; tm *ltm = localtime(&now); // 输出 tm 结构的各个组成部分
cout << "年: "<< + ltm->tm_year << endl;
cout << "月: "<< + ltm->tm_mon<< endl;
cout << "日: "<< ltm->tm_mday << endl;
cout << "时间: "<< ltm->tm_hour << ":";
cout << ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}

最新文章

  1. UIWebView获取网页点击事件
  2. 1、CC2541蓝牙4.0芯片中级教程——基于OSAL操作系统的运行流程了解+定时器和串口例程了解
  3. css-画三角箭头
  4. 对于for的一些认识
  5. Linux Kernel File IO Syscall Kernel-Source-Code Analysis(undone)
  6. 【rails3教材】博客构建过程2
  7. Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8
  8. Linux常用操作练习
  9. jdk各个版本的特性
  10. js题
  11. boost::asio网络传输错误码的一些实验结果(recv error_code)
  12. Prototype and Constructor in JavaScript
  13. Myeclipse 配置Tomcat 出现 “Value must be an existing directory”错误
  14. &lt;转&gt;LOG日志级别
  15. SpringBatch配置数据库
  16. 了解一下RabbitMQ
  17. 2018开源中国最受欢迎的中国软件MyBatis-Plus
  18. [编译] 6、开源两个简单且有用的安卓APP命令行开发工具和nRF51822命令行开发工具
  19. JS遍历数组的操作(map、forEach、filter等)
  20. python3 xml模块

热门文章

  1. JavaScript 中 new 关键字结合构造函数创建对象
  2. 关于fpga的后仿真重要性
  3. 吴裕雄--天生自然JAVA数据库编程:使用JDBC连接ORACLE数据库
  4. 如果shell到win上出现乱码怎么办
  5. bsearch的使用
  6. 在 Rolling Update 中使用 Health Check【转】
  7. elasticsearch kibana logstash(ELK)的安装集成应用
  8. Day8 - B - Non-Secret Cypher CodeForces - 190D
  9. ErrorCode=-2147217900 表已存在.
  10. mysql 模糊查询中包含特殊字符查询