R11G
R11G

Reputation: 1970

How to convert a string variable containing time to time_t type in c++?

I have a string variable containing time in hh:mm:ss format. How to convert it into time_t type? eg: string time_details = "16:35:12"

Also, how to compare two variables containing time so as to decide which is the earliest? eg : string curr_time = "18:35:21" string user_time = "22:45:31"

Upvotes: 53

Views: 160656

Answers (5)

selfboot
selfboot

Reputation: 1520

use strptime.

struct tm tm;
memset(&tm, 0, sizeof(tm));
char *res = strptime(strtime.c_str(), format.c_str(), &tm);
if (res == nullptr) {
    // err process
}
ti = mktime(&tm);

Must init tm, and check the return value.

Upvotes: 0

omar Enayet
omar Enayet

Reputation: 61

Here's the complete C implementation with date & time.

        enum DateTimeFormat {
            YearMonthDayDash, // "YYYY-MM-DD hh:mm::ss"
            MonthDayYearDash, // "MM-DD-YYYY hh:mm::ss"
            DayMonthYearDash  // "DD-MM-YYYY hh:mm::ss"
        };

        //Uses specific datetime format and returns the Linux epoch time.
        //Returns 0 on error
        static time_t ParseUnixTimeFromDateTimeString(const std::wstring& date, DateTimeFormat dateTimeFormat)
        {
            int YY, MM, DD, hh, mm, ss;
            struct tm when = { 0 };
            int res;

            if (dateTimeFormat == DateTimeFormat::YearMonthDayDash) {
                res = swscanf_s(date.c_str(), L"%d-%d-%d %d:%d:%d", &YY, &MM, &DD, &hh, &mm, &ss);
            }
            else if (dateTimeFormat == DateTimeFormat::MonthDayYearDash) {
                res = swscanf_s(date.c_str(), L"%d-%d-%d %d:%d:%d", &MM, &DD, &YY, &hh, &mm, &ss);
            }
            else if (dateTimeFormat == DateTimeFormat::DayMonthYearDash) {
                res = swscanf_s(date.c_str(), L"%d-%d-%d %d:%d:%d", &DD, &MM, &YY, &hh, &mm, &ss);
            }
            //In case datetime was in bad format, returns 0.
            if (res == EOF || res == 0) {
                return 0;
            }

            when.tm_year = YY - 1900; //Years from 1900
            when.tm_mon = MM - 1; //0-based
            when.tm_mday = DD; //1 based

            when.tm_hour = hh;
            when.tm_min = mm;
            when.tm_sec = ss;

            //Make sure the daylight savings is same as current timezone.
            time_t now = time(0);
            when.tm_isdst = std::localtime(&now)->tm_isdst;

            //Convert the tm struct to the Linux epoch
            time_t converted;
            converted = mktime(&when);

            return converted;
        }

Upvotes: 0

v2blz
v2blz

Reputation: 791

With C++11 you can now do

struct std::tm tm;
std::istringstream ss("16:35:12");
ss >> std::get_time(&tm, "%H:%M:%S"); // or just %T in this case
std::time_t time = mktime(&tm);

see std::get_time and strftime for reference

Upvotes: 75

Adam Rosenfield
Adam Rosenfield

Reputation: 400174

You can use strptime(3) to parse the time, and then mktime(3) to convert it to a time_t:

const char *time_details = "16:35:12";
struct tm tm;
strptime(time_details, "%H:%M:%S", &tm);
time_t t = mktime(&tm);  // t is now your desired time_t

Upvotes: 65

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29519

This should work:

int hh, mm, ss;
struct tm when = {0};

sscanf_s(date, "%d:%d:%d", &hh, &mm, &ss);


when.tm_hour = hh;
when.tm_min = mm;
when.tm_sec = ss;

time_t converted;
converted = mktime(&when);

Modify as needed.

Upvotes: 18

Related Questions