tm结构

在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

#ifndef _TM_DEFINED
struct tm {
          int tm_sec;       /* 秒 – 取值区间为[0,59] */
          int tm_min;       /* 分 - 取值区间为[0,59] */
          int tm_hour;      /* 时 - 取值区间为[0,23] */
          int tm_mday;      /* 一个月中的日期 - 取值区间为[1,31] */
          int tm_mon;       /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
          int tm_year;      /* 年份,其值等于实际年份减去1900 */
          int tm_wday;      /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
          int tm_yday;      /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
          int tm_isdst;     /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
          };
#define _TM_DEFINED
#endif

ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。

而日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数:

#ifndef _TIME_T_DEFINED
typedef long time_t;           /* 时间值 */
#define _TIME_T_DEFINED       /* 避免重复定义 time_t */
#endif

 我们可以通过time()函数来获得日历时间(Calendar Time),其原型为:

time_t time(time_t * timer);

如果你已经声明了参数timer,你可以从参数timer返回现在的日历时间,同时也可以通过返回值返回现在的日历时间,即从一个时间点(例如:1970年1月1日0时0分0秒)到现在此时的秒数。如果参数为空(NULL),函数将只通过返回值返回现在的日历时间,比如下面这个例子用来显示当前的日历时间:

#i nclude "time.h"
#i nclude "stdio.h"
int main(void)
{
struct tm *ptr;
time_t lt;
lt =time(NULL);
printf("The Calendar Time now is %d\n",lt);
return 0;
}

运行的结果与当时的时间有关,我当时运行的结果是:

The Calendar Time now is 1122707619

其中1122707619就是我运行程序时的日历时间。即从1970年1月1日0时0分0秒到此时的秒数。

获得日期和时间

这里说的日期和时间就是我们平时所说的年、月、日、时、分、秒等信息。从第2节我们已经知道这些信息都保存在一个名为tm的结构体中,那么如何将一个日历时间保存为一个tm结构的对象呢?

其中可以使用的函数是gmtime()和localtime(),这两个函数的原型为:

struct tm * gmtime(const time_t *timer);                                          
struct tm * localtime(const time_t * timer);

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。比如现在用gmtime()函数获得的世界标准时间是2005年7月30日7点18分20秒,那么我用localtime()函数在中国地区获得的本地时间会比时间标准时间晚8个小时,即2005年7月30日15点18分20秒。下面是个例子:

#i nclude "time.h"
#i nclude "stdio.h"
int main(void)
{
struct tm *local;
time_t t;
t=time(NULL);
local=localtime(&t);
printf("Local hour is: %d\n",local->tm_hour);
local=gmtime(&t);
printf("UTC hour is: %d\n",local->tm_hour);
return 0;
}

运行结果是:

Local hour is: 15
UTC hour is: 7



4.6 分解时间转化为日历时间

这里说的分解时间就是以年、月、日、时、分、秒等分量保存的时间结构,在C/C++中是tm结构。我们可以使用mktime()函数将用tm结构表示的时间转化为日历时间。其函数原型如下:

time_t mktime(struct tm * timeptr);

其返回值就是转化后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行操作了,下面的例子可以计算出1997年7月1日是星期几:

#i nclude "time.h"
#i nclude "stdio.h"
#i nclude "stdlib.h"
int main(void)
{
struct tm t;
time_t t_of_day;
t.tm_year=1997-1900;
t.tm_mon=6;
t.tm_mday=1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
t_of_day=mktime(&t);
printf(ctime(&t_of_day));
return 0;
}

运行结果:

Tue Jul 01 00:00:01 1997

现在注意了,有了mktime()函数,是不是我们可以操作现在之前的任何时间呢?你可以通过这种办法算出1945年8月15号是星期几吗?答案是否定的。因为这个时间在1970年1月1日之前,所以在大多数编译器中,这样的程序虽然可以编译通过,但运行时会异常终止。


### C语言中 `tm` 结构与时间戳之间的转换方法 #### 1. 时间戳的概念 时间戳是指自 Unix 纪元(即 1970 年 1 月 1 日 00:00:00 UTC)以来经过的秒数。它是一个整数值,通常用于表示特定的时间点。 #### 2. `struct tm` 的定义 `struct tm` 是 `<time.h>` 头文件中的一个结构体,用来存储日历时间和时钟时间的相关信息。其成员变量如下: ```c struct tm { int tm_sec; /* 秒 - 取值区间为 [0, 60] */ 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] */ int tm_isdst; /* 是否夏令时 */ }; ``` #### 3. 时间戳转 `struct tm` 可以使用 `localtime` 或 `_gmtime32_s` 函数将时间戳转换为本地时间或协调世界时间(UTC)的 `struct tm` 表示形式。 ##### 使用 `localtime` 转换为本地时间 `localtime` 接受一个指向时间戳的指针并返回一个指向 `struct tm` 的指针。 ```c #include <stdio.h> #include <time.h> int main() { time_t timestamp = 1684567890; struct tm *timeinfo; timeinfo = localtime(&timestamp); if (timeinfo != NULL) { printf("Local Time: %d-%02d-%02d %02d:%02d:%02d\n", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); } else { perror("Error converting local time"); } return 0; } ``` 上述代码展示了如何通过 `localtime` 将时间戳转换为本地时间[^1]。 ##### 使用 `_gmtime32_s` 转换为 UTC 时间 如果需要获取协调世界时间,则可使用 `_gmtime32_s` 函数。 ```c #include <stdio.h> #include <time.h> int main() { time_t timestamp = 1684567890; struct tm utc_time; errno_t err = _gmtime32_s(&utc_time, &timestamp); if (err == 0) { printf("UTC Time: %d-%02d-%02d %02d:%02d:%02d\n", utc_time.tm_year + 1900, utc_time.tm_mon + 1, utc_time.tm_mday, utc_time.tm_hour, utc_time.tm_min, utc_time.tm_sec); } else { perror("Error converting UTC time"); } return 0; } ``` 此代码片段说明了如何利用 `_gmtime32_s` 安全地将时间戳转换为 UTC 时间[^2]。 #### 4. `struct tm` 转时间戳 可以通过调用 `mktime` 函数来完成这一过程。该函数接受一个指向已填充好的 `struct tm` 对象的指针,并将其转换回时间戳。 ```c #include <stdio.h> #include <time.h> int main() { struct tm input_time = {0}; input_time.tm_year = 2023 - 1900; // 年份减去 1900 input_time.tm_mon = 4; // 五月(注意是从零开始) input_time.tm_mday = 20; // 当月的第 20 天 input_time.tm_hour = 15; // 下午三点 input_time.tm_min = 30; // 半小时 input_time.tm_sec = 0; // 零秒 time_t timestamp = mktime(&input_time); if (timestamp != -1) { printf("Timestamp: %ld\n", (long)timestamp); } else { perror("Error calculating timestamp"); } return 0; } ``` 这段代码演示了如何手动设置 `struct tm` 各字段并将它们转化为时间戳[^3]。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值