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