mktime时间函数

1.函数说明

1.1描述

time_t mktime(struct tm *timeptr)timeptr 所指向的结构转换为自 1970 年 1 月 1 日以来持续时间的秒数,发生错误时返回-1。

1.2.声明

​ time_t mktime(struct tm *timeptr)

1.3.参数

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,11] */ 
    int tm_year; 	  /* 年 - 取值从1900开始 */  
    int tm_wday; 	  /* 星期–取值[0,6],其中0代表星期天,1代表星期一*/  
    int tm_yday; 	  /* 从每年的1月1日开始的天数–取值[0,365] */  
    int tm_isdst; 	  /* 是否采用夏令时–取值[-1,0,1] :采用夏令时,tm_isdst为1;不采用夏令时,tm_isdst为0;不了解情况时,tm_isdst()为-1;*/  
# ifdef	__USE_MISC
  long int tm_gmtoff;		/* 指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数  */
  const char *tm_zone;		/* 时区(与环境变量TZ有关)*/
# else
  long int __tm_gmtoff;		
  const char *__tm_zone;	
# endif
}

1.4.返回值

​ 该函数返回自 1970 年 1 月 1 日以来持续时间的秒数。如果发生错误,则返回 -1 值

2.实例

#include <stdio.h>
#include <time.h>
int main () {
      int ret;
      struct tm info;
      char buffer[80];

      info.tm_year = 2021 - 1900;
      info.tm_mon = 7 - 1;
      info.tm_mday = 4;
      info.tm_hour = 0;
      info.tm_min = 0;
      info.tm_sec = 1;
      info.tm_isdst = -1;

      ret = mktime(&info);
      if( ret == -1 ) {
        printf("Error: unable to make time using mktime**\n**");
      } else {
        strftime(buffer, sizeof(buffer), "%c", &info );
        printf(buffer);
      }
      return(0);
}

6.注意要点

6.1.夏令时

​ 到夏天就将时间提前一小时,也有提前半小时或几小时的;到了冬季,又将拨快的时间拨回来

您指定的日期有夏令时生效,但在调用mktime时,storage.tm_isdst为零。 mktime看到了这一点,并认为“嘿,他们给了我一个日期不正确的夏令时标志,让我们解决它”。然后,它将tm_isdst设置为1并更改tm_hour。

6.2关于tm_isdst

​ 如果未对struct tm变量初始化,或对tm_isdst初始化,那么tm_isdst会被被设置为随机值,结果可能出现异常。(UBUNTU18.04返回结果正常,UBUNU20.04可能返回-1)。

  • 0
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
static __time64_t __cdecl _make__time64_t ( struct tm *tb, int ultflag ) { __time64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long timezone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __time64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR64 + 1) ) goto err_mktime; /* * Adjust month value so it is in the range 0 - 11. This is because * we don't know how many days are in months 12, 13, 14, etc. */ if ( (tb->tm_mon < 0) || (tb->tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) < 0 ) { tb->tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 < _BASE_YEAR - 1) || (tmptm1 > _MAX_YEAR64 + 1) ) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed years *****/ /* * Calculate days elapsed minus one, in the given year, to the given * month. Check for leap year and adjust if necessary. */ tmptm2 = _days[tb->tm_mon]; if ( _IS_LEAP_YEAR(tmptm1) && (tb->tm_mon > 1) ) tmptm2++; /* * Calculate elapsed days since base date (midnight, 1/1/70, UTC) * * * 365 days for each elapsed year since 1970, plus one more day for * each elapsed leap year. no danger of overflow because of the range * check (above) on tmptm1. */ tmptm3 = (tmptm1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(tmptm1); /* * elapsed days to current month (still no possible overflow) */ tmptm3 += tmptm2; /* * elapsed days to current date. */ tmptm1 = tmptm3 + (tmptm2 = (__time64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for timezone. No need to check for overflow since * localtime() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_timezone(&timezone;)); tmptm1 += timezone; /* * Convert this second count back into a time block structure. * If localtime returns NULL, return an error. */ if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; /* * Now must compensate for DST. The ANSI rules are to use the * passed-in tm_isdst flag if it is non-negative. Otherwise, * compute if DST applies. Recall that tbtemp has the time without * DST compensation, but has set tm_isdst correctly. */ if ( (tb->tm_isdst > 0) || ((tb->tm_isdst < 0) && (tbtemp.tm_isdst > 0)) ) { tmptm1 += dstbias; if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; } } else { if ( _gmtime64_s(&tbtemp;, &tmptm1;) != 0) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local time if requested *****/ *tb = tbtemp; return tmptm1; err_mktime: /* * All errors come to here */ errno = EINVAL; return (__time64_t)(-1); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷咪哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值