首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::mktime

Defined in header <ctime>

std::time_t mktime( std::tm* time );

将本地日历时间作为time_t对象。time->tm_wdaytime->tm_yday被忽视了。中的值time被允许超出正常范围。

负值time->tm_isdst原因mktime尝试确定夏令时间是否有效。

如果转换成功,则time对象被修改。所有领域time更新以适应它们的适当范围。time->tm_wdaytime->tm_yday使用其他字段中可用的信息重新计算。

参数

time

-

pointer to a std::tm object specifying local calendar time to convert

返回值

作为一个时代std::time_t在成功或-1如果time不能表示为std::time_t对象。

注记

如果std::tm对象是从std::get_time或POSIX短时的价值tm_isdst是不确定的,需要在调用mktime...

显示100个月前的时间。

二次

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
 
int main()
{
    setenv("TZ", "/usr/share/zoneinfo/America/New_York", 1); // POSIX-specific
 
    std::time_t t = std::time(NULL);
    std::tm tm = *std::localtime(&t);
    std::cout << "Today is           " << std::put_time(&tm, "%c %Z")
              << " and DST is " << (tm.tm_isdst ? "in effect" : "not in effect") << '\n';
    tm.tm_mon -= 100;  // tm_mon is now outside its normal range
    std::mktime(&tm);  // tm_dst is not set to -1; today's DST status is used
    std::cout << "100 months ago was " << std::put_time(&tm, "%c %Z")
              << " and DST was " << (tm.tm_isdst ? "in effect" : "not in effect") << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Today is           Fri Apr 22 11:40:36 2016 EDT and DST is in effect
100 months ago was Sat Dec 22 10:40:36 2007 EST and DST was not in effect

二次

另见

localtime

converts time since epoch to calendar time expressed as local time (function)

c mktime文档

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券