HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
date.hpp
1// Copyright Take Vos 2019.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "assert.hpp"
8#include <date/date.h>
9#include <fmt/format.h>
10#include <string>
11
12namespace tt {
13
14[[nodiscard]] std::string to_string(date::year y) noexcept {
15 return fmt::format("{}", static_cast<int>(y));
16}
17
18class quarter {
19 unsigned int q;
20
21public:
22 explicit constexpr quarter(date::month m) noexcept :
23 q(((static_cast<unsigned>(m) - 1) / 3) + 1) {}
24
25 [[nodiscard]] explicit constexpr operator unsigned () const noexcept {
26 return q;
27 }
28
29 [[nodiscard]] constexpr date::month first_month() const noexcept {
30 return date::month{((q-1) * 3) + 1};
31 }
32
33 [[nodiscard]] constexpr date::month last_month() const noexcept {
34 return first_month() + date::months{2};
35 }
36
37 [[nodiscard]] constexpr date::month_day first() const noexcept {
38 return date::month_day{first_month(), date::day{1}};
39 }
40
41 [[nodiscard]] constexpr date::month_day last() const noexcept {
42 switch (q) {
43 case 1: return date::month_day{last_month(), date::day{31}};
44 case 2: return date::month_day{last_month(), date::day{30}};
45 case 3: return date::month_day{last_month(), date::day{30}};
46 case 4: return date::month_day{last_month(), date::day{31}};
47 default: tt_no_default();
48 }
49 }
50
51 [[nodiscard]] constexpr bool increment_carry() noexcept {
52 if (++q > 4) {
53 q = 1;
54 return true;
55 } else {
56 return false;
57 }
58 }
59
60 [[nodiscard]] bool contains(date::month_day const &md) const noexcept {
61 return md >= first() && md <= last();
62 }
63
64 [[nodiscard]] friend constexpr bool operator==(quarter const &lhs, quarter const &rhs) noexcept { return lhs.q == rhs.q; }
65 [[nodiscard]] friend constexpr bool operator!=(quarter const &lhs, quarter const &rhs) noexcept { return lhs.q != rhs.q; }
66 [[nodiscard]] friend constexpr bool operator<(quarter const &lhs, quarter const &rhs) noexcept { return lhs.q < rhs.q; }
67 [[nodiscard]] friend constexpr bool operator>(quarter const &lhs, quarter const &rhs) noexcept { return lhs.q > rhs.q; }
68 [[nodiscard]] friend constexpr bool operator<=(quarter const &lhs, quarter const &rhs) noexcept { return lhs.q <= rhs.q; }
69 [[nodiscard]] friend constexpr bool operator>=(quarter const &lhs, quarter const &rhs) noexcept { return lhs.q >= rhs.q; }
70
71 [[nodiscard]] friend std::string to_string(quarter const &rhs) noexcept {
72 return fmt::format("{}", rhs.q);
73 }
74
75 friend std::ostream &operator<<(std::ostream &lhs, quarter const &rhs) noexcept {
76 return lhs << to_string(rhs);
77 }
78
79};
80
82 date::year y;
83 quarter q;
84
85public:
86 explicit constexpr year_quarter(date::year_month const &ym) :
87 y(ym.year()), q(ym.month()) {}
88
89 explicit constexpr year_quarter(date::year_month_day const &ymd) :
90 year_quarter(date::year_month{ymd.year(), ymd.month()}) {}
91
92 constexpr year_quarter &operator++() noexcept {
93 if (q.increment_carry()) {
94 ++y;
95 }
96 return *this;
97 }
98
99 [[nodiscard]] constexpr date::year_month first_year_month() noexcept {
100 return date::year_month{y, q.first_month()};
101 }
102
103 [[nodiscard]] constexpr date::year_month last_year_month() noexcept {
104 return date::year_month{y, q.last_month()};
105 }
106
107 [[nodiscard]] constexpr date::year_month_day first() noexcept {
108 ttlet md = q.first();
109 return date::year_month_day{y, md.month(), md.day()};
110 }
111
112 [[nodiscard]] constexpr date::year_month_day last() noexcept {
113 ttlet md = q.last();
114 return date::year_month_day{y, md.month(), md.day()};
115 }
116
117 [[nodiscard]] bool contains(date::year_month_day const &ymd) const noexcept {
118 return y == ymd.year() && q.contains(date::month_day(ymd.month(), ymd.day()));
119 }
120
121 [[nodiscard]] friend constexpr bool operator==(year_quarter const &lhs, year_quarter const &rhs) noexcept {
122 return lhs.y == rhs.y && lhs.q == rhs.q;
123 }
124
125 [[nodiscard]] friend constexpr bool operator<(year_quarter const &lhs, year_quarter const &rhs) noexcept {
126 if (lhs.y == rhs.y) {
127 return lhs.q < rhs.q;
128 } else {
129 return lhs.y < rhs.y;
130 }
131 }
132
133 [[nodiscard]] friend constexpr bool operator!=(year_quarter const &lhs, year_quarter const &rhs) noexcept { return !(lhs == rhs); }
134 [[nodiscard]] friend constexpr bool operator>(year_quarter const &lhs, year_quarter const &rhs) noexcept { return rhs < lhs; }
135 [[nodiscard]] friend constexpr bool operator<=(year_quarter const &lhs, year_quarter const &rhs) noexcept { return !(lhs > rhs); }
136 [[nodiscard]] friend constexpr bool operator>=(year_quarter const &lhs, year_quarter const &rhs) noexcept { return !(lhs < rhs); }
137
138 [[nodiscard]] friend std::string to_string(year_quarter const &rhs) noexcept {
139 return fmt::format("{}Q{}", rhs.y, rhs.q);
140 }
141
142 friend std::ostream &operator<<(std::ostream &lhs, year_quarter const &rhs) noexcept {
143 return lhs << to_string(rhs);
144 }
145};
146
147
148}
Definition date.hpp:18
Definition date.hpp:81
T to_string(T... args)