C Standard Libraries C++

mktime()

Declaration

time_t mktime(tm* qpTimePtr);

Description

This function converts a time specified with a tm struct variable into its time_t equivalent. The passed in pointer, qpTimePtr, holds the local time value and may be incomplete. Incomplete portions may be filled in.

Example

#include <iostream>
#include <ctime>

int main() {
    using namespace std;

    time_t qTime;
    tm qTimeSpec;

    // Initialize the date/time as Sat Sep 02 06:45:56 2000
    qTimeSpec.tm_year   = 100;
    qTimeSpec.tm_mon    = 8;
    qTimeSpec.tm_mday   = 2;
    qTimeSpec.tm_isdst  = 0;
    qTimeSpec.tm_hour   = 5;
    qTimeSpec.tm_min    = 45;
    qTimeSpec.tm_sec    = 56;

    // Convert the seconds to a tm struct variable
    qTime = mktime(&qTimeSpec);

    cout << "Seconds since midnight Jan. 1, 1970 = " << qTime << endl;

    cout << "The year is " << (1900 + qTimeSpec.tm_year) << endl;
    cout << "The month is " << qTimeSpec.tm_mon << endl;
    cout << "The day of the month is " << qTimeSpec.tm_mday << endl;
    cout << "The day of the week is " << qTimeSpec.tm_wday << endl;
    cout << "Daylight savings time? " << qTimeSpec.tm_isdst << endl;
    cout << "The hour is " << qTimeSpec.tm_hour << endl;
    cout << "The minute is " << qTimeSpec.tm_min << endl;
    cout << "The second is " << qTimeSpec.tm_sec << endl;
    cout << "The day of the year is " << qTimeSpec.tm_yday << endl;

    return 0;
}

Output

mktime() Output
 

© 2007–2023 XoaX.net LLC. All rights reserved.