Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mktime

      From cppreference.com
      <cpp‎ |chrono‎ |c
       
       
      Date and time library
       
       
      Defined in header<ctime>
      std::time_t mktime(std::tm* time);

      Converts local calendar time to a time since epoch as astd::time_t object.time->tm_wday andtime->tm_yday are ignored. The values intime are permitted to be outside their normal ranges.

      A negative value oftime->tm_isdst causesmktime to attempt to determine if Daylight Saving Time was in effect.

      If the conversion is successful, thetime object is modified. All fields oftime are updated to fit their proper ranges.time->tm_wday andtime->tm_yday are recalculated using information available in other fields.

      Contents

      [edit]Parameters

      time - pointer to astd::tm object specifying local calendar time to convert

      [edit]Return value

      Time since epoch as astd::time_t object on success or-1 iftime cannot be represented as astd::time_t object.

      [edit]Notes

      If thestd::tm object was obtained fromstd::get_time or the POSIXstrptime, the value oftm_isdst is indeterminate, and needs to be set explicitly before callingmktime.

      [edit]Example

      Construct a local time explicitly.

      Run this code
      #include <ctime>#include <iomanip>#include <iostream>#include <sstream> int main(){    setenv("TZ","/usr/share/zoneinfo/America/Los_Angeles",1);// POSIX-specific std::tm tm{};// Zero initialise    tm.tm_year=2020-1900;// 2020    tm.tm_mon=2-1;// February    tm.tm_mday=15;// 15th    tm.tm_hour=10;    tm.tm_min=15;    tm.tm_isdst=0;// Not daylight savingstd::time_t t= std::mktime(&tm);std::tm local=*std::localtime(&t); std::cout<<"local: "<<std::put_time(&local,"%c %Z")<<'\n';}

      Possible output:

      local: Sat Feb 15 10:15:00 2020 PST

      [edit]See also

      converts time since epoch to calendar time expressed as local time
      (function)[edit]
      C documentation formktime
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/c/mktime&oldid=183111"

      [8]ページ先頭

      ©2009-2025 Movatter.jp