Search code examples
c++timezonembedmktime

Convert a time string with DST to UTC timestamp


I'm trying to convert a human readable time string which has been set to a specific timezone with DST (e.g Tehran +4.30/3.30) to UTC timestamp. I'm using ARM mbed-os platform on an STM device which has a lack of some C time functions like strptime.

Anyway, I converted the time string to a timestamp but I can't figure it out how to change it to UTC. The string time is set for specific timezone which has DST so I can't simply add/remove the timezone gap.

I also prefer to get timezone as an argument for my current_time function, so I can use it with different timezones.

time_t current_time(void)
{
    time_t epoch = 0;
    parser.send("AT+CCLK?");
    string resp = read_until("OK\r\n");
    //result would be something like this:
    /*
    +CCLK: "19/06/23,19:33:42+18"

    OK

    */
    // extract time string
    unsigned int pos = resp.find("+CCLK: \"");
    unsigned int pos2 = resp.rfind("\"\r\n");
    if ((pos != string::npos) && (pos2 != string::npos))
    {
        string time_str = resp.substr(pos + 8, pos2 - pos - 11);

        //convert to timestamp
        int hh, mm, ss, yy, mon, day;
        struct tm when = {0};

        sscanf(time_str.c_str(), "%d/%d/%d,%d:%d:%d", &yy, &mon, &day, &hh, &mm, &ss);
        when.tm_hour = hh;
        when.tm_min = mm;
        when.tm_sec = ss;
        when.tm_year = 2000 + yy - 1900;
        when.tm_mon = mon - 1;
        when.tm_mday = day;
        epoch = mktime(&when);

        // below doesn't work all time because we have DST
        // add 3.30 (+1.00 daylight) offset to get UTC
        epoch = epoch - (time_t)((4 * 3600) + (30 * 60));
    }
    return epoch;
}

Solution

  • Try:

    when.tm_isdst = -1;
    

    prior to calling mktime, and removing the epoch UTC offset adjustment.