2020-01-11 23:15:24 +02:00
|
|
|
#pragma once
|
|
|
|
#include "toml_common.h"
|
|
|
|
|
|
|
|
namespace toml
|
|
|
|
{
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief A local date.
|
2020-01-11 23:15:24 +02:00
|
|
|
struct date final
|
|
|
|
{
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The year component.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint16_t year;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The month component, from 1 - 12.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint8_t month;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The day component, from 1 - 31.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint8_t day;
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
|
|
|
|
/// \brief Equality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS date.
|
|
|
|
/// \param rhs The RHS date.
|
|
|
|
///
|
|
|
|
/// \returns True if the dates represented the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator == (date lhs, date rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.year == rhs.year
|
|
|
|
&& lhs.month == rhs.month
|
|
|
|
&& lhs.day == rhs.day;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Inequality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS date.
|
|
|
|
/// \param rhs The RHS date.
|
|
|
|
///
|
|
|
|
/// \returns True if the dates did not represent the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator != (date lhs, date rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.year != rhs.year
|
|
|
|
|| lhs.month != rhs.month
|
|
|
|
|| lhs.day != rhs.day;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
|
|
|
|
/// \brief Prints a date out to a stream as `YYYY-MM-DD` (per RFC 3339).
|
2020-01-11 23:15:24 +02:00
|
|
|
template <typename CHAR>
|
|
|
|
friend inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const date& rhs)
|
|
|
|
TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
impl::print_to_stream(rhs, lhs);
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief A local time-of-day.
|
2020-01-11 23:15:24 +02:00
|
|
|
struct time final
|
|
|
|
{
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The hour component, from 0 - 23.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint8_t hour;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The minute component, from 0 - 59.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint8_t minute;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The second component, from 0 - 59.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint8_t second;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The fractional nanoseconds component, from 0 - 999999999.
|
2020-01-11 23:15:24 +02:00
|
|
|
uint32_t nanosecond;
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Equality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS time.
|
|
|
|
/// \param rhs The RHS time.
|
|
|
|
///
|
|
|
|
/// \returns True if the times represented the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator == (const time& lhs, const time& rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.hour == rhs.hour
|
|
|
|
&& lhs.minute == rhs.minute
|
|
|
|
&& lhs.second == rhs.second
|
|
|
|
&& lhs.nanosecond == rhs.nanosecond;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Inequality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS time.
|
|
|
|
/// \param rhs The RHS time.
|
|
|
|
///
|
|
|
|
/// \returns True if the times did not represent the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator != (const time& lhs, const time& rhs) noexcept
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Prints a time out to a stream as `HH:MM:SS.FFFFFF` (per RFC 3339).
|
2020-01-11 23:15:24 +02:00
|
|
|
template <typename CHAR>
|
|
|
|
friend inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const time& rhs)
|
|
|
|
TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
impl::print_to_stream(rhs, lhs);
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief A timezone offset.
|
2020-01-11 23:15:24 +02:00
|
|
|
struct time_offset final
|
|
|
|
{
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief Offset from UTC+0, in minutes.
|
2020-01-11 23:15:24 +02:00
|
|
|
int16_t minutes;
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Creates a timezone offset from separate hour and minute totals.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// std::cout << time_offset::from_hh_mm(2, 30) << std::endl;
|
|
|
|
/// std::cout << time_offset::from_hh_mm(-2, 30) << std::endl;
|
|
|
|
/// std::cout << time_offset::from_hh_mm(-2, -30) << std::endl;
|
|
|
|
/// std::cout << time_offset::from_hh_mm(0,0) << std::endl;
|
|
|
|
///
|
|
|
|
/// // output:
|
|
|
|
/// // +02:30
|
|
|
|
/// // -01:30
|
|
|
|
/// // -02:30
|
|
|
|
/// // Z
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \param hours The total hours.
|
|
|
|
/// \param minutes The total minutes.
|
|
|
|
///
|
|
|
|
/// \returns A time_offset.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
static constexpr time_offset from_hh_mm(int8_t hours, int8_t minutes) noexcept
|
|
|
|
{
|
|
|
|
return time_offset{ static_cast<int16_t>(hours * 60 + minutes) };
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Equality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS time_offset.
|
|
|
|
/// \param rhs The RHS time_offset.
|
|
|
|
///
|
|
|
|
/// \returns True if the time_offsets represented the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator == (time_offset lhs, time_offset rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.minutes == rhs.minutes;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Inequality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS time_offset.
|
|
|
|
/// \param rhs The RHS time_offset.
|
|
|
|
///
|
|
|
|
/// \returns True if the time_offsets did not represent the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator != (time_offset lhs, time_offset rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.minutes != rhs.minutes;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Prints a time_offset out to a stream as `+-HH:MM or Z` (per RFC 3339).
|
2020-01-11 23:15:24 +02:00
|
|
|
template <typename CHAR>
|
|
|
|
friend inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const time_offset& rhs)
|
|
|
|
TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
impl::print_to_stream(rhs, lhs);
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief A date-time.
|
2020-01-11 23:15:24 +02:00
|
|
|
struct date_time final
|
|
|
|
{
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The date component.
|
2020-01-11 23:15:24 +02:00
|
|
|
toml::date date;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The time component.
|
2020-01-11 23:15:24 +02:00
|
|
|
toml::time time;
|
2020-01-13 08:31:49 +02:00
|
|
|
/// \brief The timezone offset component.
|
2020-02-03 11:12:43 +02:00
|
|
|
///
|
|
|
|
/// \remarks The date_time is said to be 'local' if the time_offset is empty.
|
2020-01-11 23:15:24 +02:00
|
|
|
std::optional<toml::time_offset> time_offset;
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Returns true if this date_time does not contain timezone offset information.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
constexpr bool is_local() const noexcept
|
|
|
|
{
|
|
|
|
return !time_offset.has_value();
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Equality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS date_time.
|
|
|
|
/// \param rhs The RHS date_time.
|
|
|
|
///
|
|
|
|
/// \returns True if the date_times represented the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator == (const date_time& lhs, const date_time& rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.date == rhs.date
|
|
|
|
&& lhs.time == rhs.time
|
|
|
|
&& lhs.time_offset == rhs.time_offset;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Inequality operator.
|
|
|
|
///
|
|
|
|
/// \param lhs The LHS date_time.
|
|
|
|
/// \param rhs The RHS date_time.
|
|
|
|
///
|
|
|
|
/// \returns True if the date_times did not represent the same value.
|
2020-01-11 23:15:24 +02:00
|
|
|
[[nodiscard]]
|
|
|
|
friend constexpr bool operator != (const date_time& lhs, const date_time& rhs) noexcept
|
|
|
|
{
|
|
|
|
return lhs.date != rhs.date
|
|
|
|
|| lhs.time != rhs.time
|
|
|
|
|| lhs.time_offset != rhs.time_offset;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:43 +02:00
|
|
|
/// \brief Prints a date_time out to a stream in RFC 3339 format.
|
2020-01-11 23:15:24 +02:00
|
|
|
template <typename CHAR>
|
|
|
|
friend inline std::basic_ostream<CHAR>& operator << (std::basic_ostream<CHAR>& lhs, const date_time& rhs)
|
|
|
|
TOML_MAY_THROW
|
|
|
|
{
|
|
|
|
impl::print_to_stream(rhs, lhs);
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|