网站首页 > 文章精选 正文
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; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
下面是 C/C++ 中关于日期和时间的重要函数。所有这些函数都是 C/C++ 标准库的组成部分,您可以在 C++ 标准库中查看一下各个函数的细节。
序号 | 函数 & 描述 |
1 | time_t time(time_t *time); |
2 | char *ctime(const time_t *time); |
3 | struct tm *localtime(const time_t *time); |
4 | clock_t clock(void); |
5 | char * asctime ( const struct tm * time ); |
6 | struct tm *gmtime(const time_t *time); |
7 | time_t mktime(struct tm *time); |
8 | double difftime ( time_t time2, time_t time1 ); |
9 | size_t strftime(); |
当前日期和时间
下面的实例获取当前系统的日期和时间,包括本地时间和协调世界时(UTC)。
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
// convert now to tm struct for UTC
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "The UTC date and time is:"<< dt << endl;
}
当上面的代码被编译和执行时,它会产生下列结果 -
The local date and time is: Sat Jan 8 20:07:41 2011
The UTC date and time is:Sun Jan 9 03:07:41 2011
使用结构 tm 格式化时间
tm 结构在 C/C++ 中处理日期和时间相关的操作时,显得尤为重要。tm 结构以 C 结构的形式保存日期和时间。大多数与时间相关的函数都使用了 tm 结构。下面的实例使用了 tm 结构和各种与日期和时间相关的函数。
在练习使用结构之前,需要对 C 结构有基本的了解,并懂得如何使用箭头 -> 运算符来访问结构成员。
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
cout << "Number of sec since January 1,1970:" << now << endl;
tm *ltm = localtime(&now);
// print various components of tm structure.
cout << "Year" << 1970 + ltm->tm_year<<endl;
cout << "Month: "<< 1 + ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
cout << "Time: "<< 1 + ltm->tm_hour << ":";
cout << 1 + ltm->tm_min << ":";
cout << 1 + ltm->tm_sec << endl;
}
当上面的代码被编译和执行时,它会产生下列结果 -
Number of sec since January 1, 1970:1294548238
Year: 2011
Month: 1
Day: 8
Time: 22: 44:59
猜你喜欢
- 2025-07-02 一文吃透ConcurrentHashMap的前世与今生
- 2025-07-02 钟表程序(C语言+SDL2)(时钟程序编写)
- 2025-07-02 教你实现高逼格Android弹幕效果(安卓弹幕助手)
- 2025-07-02 C语言的十大组数排序法:选择排序!竟然可以这么快!
- 2025-07-02 基于LockAI视觉识别模块:C++同时识别轮廓和色块
- 2025-07-02 国产教学实验箱_DSP教学实验箱_操作教程:4-4 有限冲激响应滤波器
- 2025-07-02 Android 系统核心机制binder(04)binder C++层 TestServer分析
- 2025-07-02 乐鑫esp32系列在睡眠模式蓝牙连接功耗测试,启明云端乐鑫代理商
- 2025-07-02 深度解析HashMap集合底层原理(使用hashmap集合迭代出元素的和元素存入的顺序)
- 2025-07-02 嵌入式开发中常用的软件工程方法有哪些?
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 计算机网络的拓扑结构是指() (45)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)
- mysql数据库面试题 (57)