Formats time points as relative time strings.
More...
#include <relative_time_formatter.hpp>
|
| | relative_time_formatter (numeric_style style=numeric_style::automatic) |
| | Constructs a formatter with the specified numeric style.
|
| |
| std::string | format (const std::chrono::system_clock::time_point &tp) const |
| | Formats a time point relative to now.
|
| |
| std::string | format (const std::chrono::system_clock::time_point &tp, const std::chrono::system_clock::time_point &reference) const |
| | Formats a time point relative to a reference time.
|
| |
| std::string | format (long long value, time_unit unit) const |
| | Formats a numeric value and unit as a relative time string.
|
| |
Formats time points as relative time strings.
Inspired by ECMAScript Intl.RelativeTimeFormat, this class provides human-readable relative time strings like "just now", "5 minutes ago", "in 3 days", etc.
- See also
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat
◆ relative_time_formatter()
Constructs a formatter with the specified numeric style.
- Parameters
-
| style | The numeric style to use for formatting. |
◆ format() [1/3]
| std::string format |
( |
const std::chrono::system_clock::time_point & |
tp | ) |
const |
Formats a time point relative to now.
- Parameters
-
| tp | The time point to format. |
- Returns
- Human-readable relative time string.
◆ format() [2/3]
| std::string format |
( |
const std::chrono::system_clock::time_point & |
tp, |
|
|
const std::chrono::system_clock::time_point & |
reference |
|
) |
| const |
Formats a time point relative to a reference time.
- Parameters
-
| tp | The time point to format. |
| reference | The reference time point to compare against. |
- Returns
- Human-readable relative time string.
◆ format() [3/3]
| std::string format |
( |
long long |
value, |
|
|
time_unit |
unit |
|
) |
| const |
Formats a numeric value and unit as a relative time string.
Based on ECMAScript Intl.RelativeTimeFormat.format().
- Parameters
-
| value | The numeric value (positive for future, negative for past). |
| unit | The time unit. |
- Returns
- Human-readable relative time string.