Skip to content

Commit fa5103a

Browse files
author
MarcoFalke
committed
Add ChronoFormatter to serialize
1 parent fa253d3 commit fa5103a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/serialize.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,29 @@ struct CompactSizeFormatter
520520
}
521521
};
522522

523+
template <typename U, bool LOSSY = false>
524+
struct ChronoFormatter {
525+
template <typename Stream, typename Tp>
526+
void Unser(Stream& s, Tp& tp)
527+
{
528+
U u;
529+
s >> u;
530+
// Lossy deserialization does not make sense, so force Wnarrowing
531+
tp = Tp{typename Tp::duration{typename Tp::duration::rep{u}}};
532+
}
533+
template <typename Stream, typename Tp>
534+
void Ser(Stream& s, Tp tp)
535+
{
536+
if constexpr (LOSSY) {
537+
s << U(tp.time_since_epoch().count());
538+
} else {
539+
s << U{tp.time_since_epoch().count()};
540+
}
541+
}
542+
};
543+
template <typename U>
544+
using LossyChronoFormatter = ChronoFormatter<U, true>;
545+
523546
class CompactSizeWriter
524547
{
525548
protected:

0 commit comments

Comments
 (0)