Skip to main content
added 19 characters in body
Source Link
cpplearner
  • 1.2k
  • 1
  • 7
  • 9

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

std::chrono::sys_seconds from_tm(std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}
constexpr std::chrono::sys_seconds from_tm(const std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock. For example:

std::chrono::time_point<std::chrono::utc_clock> tp
  = std::chrono::clock_cast<std::chrono::utc_clock>(res);

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

std::chrono::sys_seconds from_tm(std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock. For example:

std::chrono::time_point<std::chrono::utc_clock> tp
  = std::chrono::clock_cast<std::chrono::utc_clock>(res);

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

constexpr std::chrono::sys_seconds from_tm(const std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock. For example:

std::chrono::time_point<std::chrono::utc_clock> tp
  = std::chrono::clock_cast<std::chrono::utc_clock>(res);
Added example for clock_cast
Source Link
user149408
  • 6.3k
  • 4
  • 41
  • 89

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

std::chrono::sys_seconds from_tm(std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock. For example:

std::chrono::time_point<std::chrono::utc_clock> tp
  = std::chrono::clock_cast<std::chrono::utc_clock>(res);

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

std::chrono::sys_seconds from_tm(std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock.

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

std::chrono::sys_seconds from_tm(std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock. For example:

std::chrono::time_point<std::chrono::utc_clock> tp
  = std::chrono::clock_cast<std::chrono::utc_clock>(res);
Source Link
cpplearner
  • 1.2k
  • 1
  • 7
  • 9

If you already have a struct tm with requested fields, you can convert it to a time_point of system_clock as follows:

std::chrono::sys_seconds from_tm(std::tm& t)
{
    using namespace std::chrono;

    const auto y = static_cast<year>(t.tm_year + 1900);
    const auto m = static_cast<month>(t.tm_mon + 1);
    const auto d = static_cast<day>(t.tm_mday);
    const hours h{t.tm_hour};
    const minutes m{t.tm_min};
    const seconds s{t.tm_sec};

    sys_seconds res = sys_days{y/m/d};
    res = res + h + m + s;

    return res;
}

You can use std::chrono::clock_cast to convert the result to a time point of a different, compatible clock.