mktime 传参struct tm赋值说明

 time_t mktime (struct tm *__tp)

/* ISO C `broken-down time' structure.  */
struct tm
{
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
  int tm_min;			/* Minutes.	[0-59] */
  int tm_hour;			/* Hours.	[0-23] */
  int tm_mday;			/* Day.		[1-31] */
  int tm_mon;			/* Month.	[0-11] */
  int tm_year;			/* Year	- 1900.  */
  int tm_wday;			/* Day of week.	[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/

# ifdef	__USE_MISC
  long int tm_gmtoff;		/* Seconds east of UTC.  */
  const char *tm_zone;		/* Timezone abbreviation.  */
# else
  long int __tm_gmtoff;		/* Seconds east of UTC.  */
  const char *__tm_zone;	/* Timezone abbreviation.  */
# endif
};

Broken-down Time (The GNU C Library)

对tm结构体的注释ISO C `broken-down time' structure.

说明是ISO标准的C时间分解结构。值得注意的是这个是ISO标准的。

上面连接里有一句话:

The mktime function ignores the specified contents of the tm_wdaytm_ydaytm_gmtoff, and tm_zone members of the broken-down time structure. 

 mktime在传参时会忽略 tm_wdaytm_ydaytm_gmtoff和 tm_zone。我们先暂时不管。

对于tm_sec,tm_min,tm_hour,tm_mday,tm_mon,tm_year;这几个参数,好理解,就是需要转换的年月日。

对于tm_isdst表示是否是夏令时,通过网上查询可知,我国只在1986年到1991实时过夏令时。因此这里可以传0,表示不是夏令时。

mktime不使用tm_gmtoff和 tm_zone来说明时区,而是使用tzset函数来说明时区,mktime会根据tzset设置的时区信息来初始化tm_gmtoff和 tm_zone。

Calling mktime also sets the current time zone as if tzset were called; mktime uses this information instead of brokentime’s initial tm_gmtoff and tm_zone members. See Functions and Variables for Time Zones.

对于tm_wdaytm_yday,则会根据tm结构的其他字段被mktime函数改变。如下是mktime的man说明

The  mktime()  function  modifies  the fields of the tm structure as follows: tm_wday and tm_yday are set to values determined from the contents of the other fields;

最后,一般使用时就把tm_sec,tm_min,tm_hour,tm_mday,tm_mon,tm_year,外加tm_isdst这几个参数赋值即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值