Converting std::chrono::time_point to/from std::string Consider these functions that allow to convert std::chrono::time_point to/from std::string with a predefined date-time format.

constexpr size_t log10(size_t xx) { return xx == 1 ? 0 : 1 + log10(xx/10); }

template &lt; typename Double, size_t Precision = std::numeric_limits&lt;Double&gt;::digits10, typename TimePoint &gt;
requires (TimePoint::period::den % 10 != 0) &amp;&amp; std::is_floating_point_v&lt;Double&gt; &amp;&amp; Precision &lt;= std::numeric_limits&lt;Double&gt;::digits10
inline bool toString(const TimePoint&amp; timePoint, std::string&amp; str)
{
    Double seconds = timePoint.time_since_epoch().count();
    (seconds *= TimePoint::period::num) /= TimePoint::period::den;
    auto zeconds = std::modf(seconds,&amp;seconds);
    time_t tt = seconds;
    std::ostringstream oss;
    oss &lt;&lt; std::put_time(std::localtime(&amp;tt), "%Y-%m-%d %H:%M:")
        &lt;&lt; std::setw(Precision+3) &lt;&lt; std::setfill('0') &lt;&lt; std::fixed &lt;&lt; std::setprecision(Precision) &lt;&lt; (tt%60)+zeconds;
    return oss &amp;&amp; (str = oss.str(), true);
}

template &lt; typename TimePoint &gt; requires (TimePoint::period::den % 10 == 0)
inline bool toString(const TimePoint&amp; timePoint, std::string&amp; str)
{
    uint64_t feconds = timePoint.time_since_epoch().count() * TimePoint::period::num;
    time_t tt = feconds / TimePoint::period::den;
    std::ostringstream oss;
    oss &lt;&lt; std::put_time(std::localtime(&amp;tt), "%Y-%m-%d %H:%M:%S.")
        &lt;&lt; std::setw(log10(TimePoint::period::den)) &lt;&lt; std::setfill('0') &lt;&lt; feconds % TimePoint::period::den;
    return oss &amp;&amp; (str = oss.str(), true);
}

template &lt; typename TimePoint &gt;
bool fromString(TimePoint&amp; timePoint, const std::string&amp; str)
{
    std::istringstream iss(str);
    std::tm tm{};
    if (!(iss &gt;&gt; std::get_time(&amp;tm, "%Y-%m-%d %H:%M:%S")))
        return false;
    timePoint  = {};
    timePoint += std::chrono::seconds(std::mktime(&amp;tm));
    if (iss.eof())
        return true;
    if (iss.get() != '.')
        return false;
    std::string zz;
    if (!(iss &gt;&gt; zz))
        return false;
    static_assert(std::chrono::high_resolution_clock::period::num == 1 &amp;&amp; std::chrono::high_resolution_clock::period::den % 10 == 0);
    zz.resize(log10(std::chrono::high_resolution_clock::period::den),'0');
    size_t zeconds = 0;
    try { zeconds = std::stoul(zz); } catch (const std::exception&amp;) { return false; }
    timePoint += std::chrono::high_resolution_clock::duration(zeconds);
    return true;
}


with usage:   

        std::string str;
        auto now = std::chrono::system_clock::now();
        toString(now,str); std::cerr &lt;&lt; "==== " &lt;&lt; str &lt;&lt; std::endl;
        using DD = std::chrono::duration&lt;size_t,std::ratio&lt;2,3&gt;&gt;;
        using TP = std::chrono::time_point&lt;std::chrono::system_clock,DD&gt;;
        toString&lt;double&gt;(TP(DD(0)),str); std::cout &lt;&lt; "==== " &lt;&lt; str &lt;&lt; std::endl;
        toString&lt;double&gt;(TP(DD(1)),str); std::cout &lt;&lt; "==== " &lt;&lt; str &lt;&lt; std::endl;
        toString&lt;double&gt;(TP(DD(2)),str); std::cout &lt;&lt; "==== " &lt;&lt; str &lt;&lt; std::endl;
        toString&lt;double&gt;(TP(DD(3)),str); std::cout &lt;&lt; "==== " &lt;&lt; str &lt;&lt; std::endl;

        std::chrono::system_clock::time_point tp;
        str = "2017-Mar-01";                          fromString(tp,str); toString(tp,str); std::cerr &lt;&lt; "---- " &lt;&lt; str &lt;&lt; std::endl;
        str = "1969-Dec-31 19:00:00.666666666666667"; fromString(tp,str); toString(tp,str); std::cerr &lt;&lt; "---- " &lt;&lt; str &lt;&lt; std::endl;


Any suggestions to make them faster, c-functions free or more compact?

As the objective is to do conversions with a predefined date-time format the std::locale support of std::ostringstream and std::istringstream is useless and it is a significant slowdown. But how can I avoid this?
Headers and typenames

We're missing

#include &lt;chrono&gt;
#include &lt;cmath&gt;
#include &lt;cstdint&gt;
#include &lt;ctime&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;


And we consistently misspell the types std::size_t, std::time_t, std::uint64_t.

A name collision

On systems that define a global log10 as well as std::log10, there's ambiguity on the calls to our log10.  I suggest using a name that conveys intent, such as digits() (even that's a bit risky - it would be better in a private namespace).  It costs nothing to make it terminate if passed 0 as argument, too:

static constexpr std::size_t feconds_width(std::size_t x)
{
    return x &lt;= 1 ? 0 : 1 + feconds_width(x/10);
}


Interface consistency

The two alternative forms for toString() need to be instantiated differently, depending on the timepoint's denominator value.  If we make Double default to double, then both can be called without explicit template arguments.

Incidentally, did you measure a performance difference between the two implementations that proved a benefit?  If so, it's worth quantifying that in a comment so that future readers understand that it's worthwhile having both versions.

Document the assumption

This code only works when converting to or from TimePoint classes which have the same epoch as std::time_t.

Prefer to return values over status codes

Rather than returning a status code, I'd prefer to throw an exception when conversion fails; then the return value can be used directly.  Failure in toString() seems especially unlikely - I think the only possible cause is running out of room in the output string and being unable to allocate more.

if (!oss) throw std::runtime_error("timepoint-to-string");
return oss.str();


If you really dislike exceptions, it's still easier on the caller to return a std::optional rather than return a boolean and modify a reference argument.

Be careful with negative times

If tt&lt;0, then tt%60 can be negative.  That's not what we want.  However, since we create a std::tm, we can read seconds from that:

auto tm = std::localtime(&amp;tt);
if (!tm) throw std::runtime_error(std::strerror(errno));
oss &lt;&lt; std::put_time(tm, "%Y-%b-%d %H:%M:")
    &lt;&lt; std::setw(Precision+3) &lt;&lt; std::setfill('0')
    &lt;&lt; std::fixed &lt;&lt; std::setprecision(Precision)
    &lt;&lt; tm-&gt;tm_sec+zeconds;


Peek the '.' instead of reading it

If we leave '.' in the input stream, we can read that as part of a floating-point value:

double zz;
if (iss.peek() != '.' || !(iss &gt;&gt; zz))
    throw std::invalid_argument("decimal");
using hr_clock = std::chrono::high_resolution_clock;
std::size_t zeconds = zz * hr_clock::period::den / hr_clock::period::num;
timePoint += hr_clock::duration(zeconds);


Test cases seem inconsistent with the code

The test cases use month names, which require a %b conversion, not %m.
We could do with a few more test cases.

Consider a streamable wrapper

If your main use-case is to stream dates in and out, it may be better to bypass returning a string, and instead create an object that knows how to stream directly to a std::ostream.  As I don't know if that's your intended use, I won't demonstrate that.



Simplified code

#include &lt;cerrno&gt;
#include &lt;chrono&gt;
#include &lt;cmath&gt;
#include &lt;cstdint&gt;
#include &lt;ctime&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;stdexcept&gt;
#include &lt;string&gt;

template&lt;typename Double = double,
         std::size_t Precision = std::numeric_limits&lt;Double&gt;::digits10,
         typename TimePoint&gt;
    requires std::is_floating_point_v&lt;Double&gt;
          &amp;&amp; Precision &lt;= std::numeric_limits&lt;Double&gt;::digits10
inline std::string toString(const TimePoint&amp; timePoint)
{
    auto seconds = Double(timePoint.time_since_epoch().count())
        * TimePoint::period::num / TimePoint::period::den;
    auto const zeconds = std::modf(seconds,&amp;seconds);
    std::time_t tt(seconds);
    std::ostringstream oss;
    auto const tm = std::localtime(&amp;tt);
    if (!tm) throw std::runtime_error(std::strerror(errno));
    oss &lt;&lt; std::put_time(tm, "%Y-%b-%d %H:%M:")
        &lt;&lt; std::setw(Precision+3) &lt;&lt; std::setfill('0')
        &lt;&lt; std::fixed &lt;&lt; std::setprecision(Precision)
        &lt;&lt; tm-&gt;tm_sec+zeconds;
    if (!oss) throw std::runtime_error("timepoint-to-string");
    return oss.str();
}

template&lt;typename TimePoint&gt;
TimePoint fromString(const std::string&amp; str)
{
    std::istringstream iss{str};
    std::tm tm{};
    if (!(iss &gt;&gt; std::get_time(&amp;tm, "%Y-%b-%d %H:%M:%S")))
        throw std::invalid_argument("get_time");
    TimePoint timePoint{std::chrono::seconds(std::mktime(&amp;tm))};
    if (iss.eof())
        return timePoint;
    double zz;
    if (iss.peek() != '.' || !(iss &gt;&gt; zz))
        throw std::invalid_argument("decimal");
    using hr_clock = std::chrono::high_resolution_clock;
    std::size_t zeconds = zz * hr_clock::period::den / hr_clock::period::num;
    return timePoint += hr_clock::duration(zeconds);
}



int main()
{
    using std::chrono::system_clock;
    auto now = system_clock::now();
    std::clog &lt;&lt; "==== " &lt;&lt; toString(now) &lt;&lt; std::endl;
    using DD = std::chrono::duration&lt;std::size_t,std::ratio&lt;2,3&gt;&gt;;
    using TP = std::chrono::time_point&lt;std::chrono::system_clock,DD&gt;;
    for (int i = 0;  i &lt;= 3;  ++i)
        std::clog &lt;&lt; "==== " &lt;&lt; toString(TP(DD(i))) &lt;&lt; std::endl;

    for (std::string s: { "2017-May-01 00:10:15", "2017-May-01 00:10:15.25",
                "2017-Mar-01", "1969-Dec-31 19:00:00.666666666666667",
                "2018", // underspecified - but succeeds as 2017-12-31
                "1752-May-5",   // out of range
                "not a date",
                })
    {
        try {
            std::clog &lt;&lt; "---- "
                      &lt;&lt; toString(fromString&lt;system_clock::time_point&gt;(s))
                      &lt;&lt; std::endl;
        } catch (const std::exception&amp; e) {
            std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
        }
    }
}