在线文档教程
C++
应用 | Utilities

std::mktime

STD::mktime

Defined in header
std::time_t mktime( std::tm* time

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

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

如果转换成功,则time对象被修改。所有领域time更新以适应它们的适当范围。time->tm_wday和time->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个月前的时间。

二次

#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'; }

二次

产出:

二次

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

二次

另见

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

c mktime文档

© cppreference.com

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

http://en.cppreference.com/w/cpp/crono/c/mktime