1

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;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • It is arm mbed platform it supposes to have c++ definitions. But I prefer to use C, but it doesn't matter for me if it works. – Masoud Rahimi Jun 23 '19 at 03:41

1 Answers1

1

Try:

when.tm_isdst = -1;

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

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • Thanks but how can I tell it the current time is set for a specific timezone (e.g current time has been set for Tehran(+3.30))? – Masoud Rahimi Jun 23 '19 at 03:45
  • 1
    My suggestion depends on your computer having its current time zone setting set to "Asia/Tehran". If this is not the case, there is no pure std C or C++ solution. However I do have a C++11 (and forward) 3rd party library that can do the job if you're interested in that. – Howard Hinnant Jun 23 '19 at 03:49
  • I found [this](https://stackoverflow.com/questions/9076494/how-to-convert-from-utc-to-local-time-in-c) but doing the opposite of what I want. – Masoud Rahimi Jun 23 '19 at 06:04
  • I somehow fix it by using a manual conversion, but I'm accepting this question as gave me insight on what the problem is. – Masoud Rahimi Jul 06 '19 at 10:11