11#pragma once
22
33#include " ParseDate.hh"
4- #include " date/date.h"
54#include < chrono>
65#include < cmath>
76#include < ratio>
87#include < cstdint>
98#include " SQLiteFleeceUtil.hh"
109#include " DateFormat.hh"
1110
12- namespace date {
11+ using namespace std ::chrono ;
1312
14- using namespace std ::chrono;
13+ using quarters = duration<int , std::ratio_multiply<std::ratio<3 >, months::period>>;
14+ using decades = duration<int , std::ratio_multiply<std::ratio<10 >, years::period>>;
15+ using centuries = duration<int , std::ratio_multiply<std::ratio<100 >, years::period>>;
16+ using millenniums = duration<int , std::ratio_multiply<std::ratio<1000 >, years::period>>;
1517
16- using quarters = duration<int , detail::ratio_multiply<std::ratio<3 >, months::period>>;
17- using decades = duration<int , detail::ratio_multiply<std::ratio<10 >, years::period>>;
18- using centuries = duration<int , detail::ratio_multiply<std::ratio<100 >, years::period>>;
19- using millenniums = duration<int , detail::ratio_multiply<std::ratio<1000 >, years::period>>;
18+ typedef struct {
19+ int64_t year;
20+ int64_t doy;
21+ int64_t hour;
22+ int64_t minute;
23+ int64_t second;
24+ int64_t millisecond;
25+ } DateDiff;
2026
21- typedef struct {
22- int64_t year;
23- int64_t doy;
24- int64_t hour;
25- int64_t minute;
26- int64_t second;
27- int64_t millisecond;
28- } DateDiff;
27+ constexpr year_month_day& operator +=(year_month_day& ymd, decades d) noexcept { return ymd += years (d); }
2928
30- } // namespace date
29+ constexpr year_month_day& operator +=(year_month_day& ymd, centuries c) noexcept { return ymd += years (c); }
30+
31+ constexpr year_month_day& operator +=(year_month_day& ymd, millenniums m) noexcept { return ymd += years (m); }
3132
3233namespace litecore {
3334 using namespace fleece ;
@@ -64,10 +65,10 @@ namespace litecore {
6465
6566 // The Day Of Year for the given time_point. This is the number of days since the start of the year.
6667 inline int64_t doy (const date_time_point& t) {
67- const auto daypoint = floor<date:: days>(t);
68- const auto ymd = date:: year_month_day{daypoint};
68+ const auto daypoint = floor<days>(t);
69+ const auto ymd = year_month_day{daypoint};
6970 const auto year = ymd.year ();
70- const auto year_day = daypoint - date:: sys_days{year / date ::January / 0 };
71+ const auto year_day = daypoint - sys_days{year / std::chrono ::January / 0 };
7172 return year_day.count ();
7273 }
7374
@@ -110,8 +111,7 @@ namespace litecore {
110111 setResultTextFromSlice (ctx, DateFormat::format (buf, millis, asUTC, format));
111112 }
112113
113- inline int64_t diffPart (const DateTime& t1, const DateTime& t2, const date::DateDiff& diff,
114- const DateComponent part) {
114+ inline int64_t diffPart (const DateTime& t1, const DateTime& t2, const DateDiff& diff, const DateComponent part) {
115115 switch ( part ) {
116116 case kDateComponentMillisecond :
117117 {
@@ -184,12 +184,12 @@ namespace litecore {
184184 sign = -1 ;
185185 }
186186
187- const date:: DateDiff diff{left.Y - right.Y ,
188- doy (tp_left) - doy (tp_right),
189- left.h - right.h ,
190- left.m - right.m ,
191- static_cast <int64_t >(left.s ) - static_cast <int64_t >(right.s ),
192- static_cast <int64_t >((frac (left.s ) - frac (right.s )) * 1000 )};
187+ const DateDiff diff{left.Y - right.Y ,
188+ doy (tp_left) - doy (tp_right),
189+ left.h - right.h ,
190+ left.m - right.m ,
191+ static_cast <int64_t >(left.s ) - static_cast <int64_t >(right.s ),
192+ static_cast <int64_t >((frac (left.s ) - frac (right.s )) * 1000 )};
193193
194194 auto result = diffPart (left, right, diff, date_component);
195195 result *= sign;
@@ -201,7 +201,7 @@ namespace litecore {
201201 DateComponent date_component;
202202 if ( !part || (date_component = ParseDateComponent (part)) == kDateComponentInvalid ) { return -1 ; }
203203
204- date:: year_month_day ymd = date:: year (start.Y ) / start.M / start.D ;
204+ year_month_day ymd = year (start.Y ) / start.M / start.D ;
205205 std::chrono::milliseconds tod = std::chrono::hours (start.h ) + std::chrono::minutes (start.m - start.tz )
206206 + std::chrono::milliseconds (static_cast <int64_t >(start.s * 1000 ));
207207
@@ -219,34 +219,34 @@ namespace litecore {
219219 tod += std::chrono::hours (amount);
220220 break ;
221221 case kDateComponentDay :
222- tod += date:: days (amount);
222+ tod += days (amount);
223223 break ;
224224 case kDateComponentWeek :
225- tod += date:: weeks (amount);
225+ tod += weeks (amount);
226226 break ;
227227 case kDateComponentMonth :
228- ymd += date:: months (amount);
228+ ymd += months (amount);
229229 break ;
230230 case kDateComponentQuarter :
231- ymd += date:: quarters (amount);
231+ ymd += quarters (amount);
232232 break ;
233233 case kDateComponentYear :
234- ymd += date:: years (amount);
234+ ymd += years (amount);
235235 break ;
236236 case kDateComponentDecade :
237- ymd += date:: decades (amount);
237+ ymd += decades (amount);
238238 break ;
239239 case kDateComponentCentury :
240- ymd += date:: centuries (amount);
240+ ymd += centuries (amount);
241241 break ;
242242 case kDateComponentMillennium :
243- ymd += date:: millenniums (amount);
243+ ymd += millenniums (amount);
244244 break ;
245245 case kDateComponentInvalid :
246246 return -1 ;
247247 }
248248
249- return (date:: sys_days (ymd) + tod).time_since_epoch ().count ();
249+ return (sys_days (ymd) + tod).time_since_epoch ().count ();
250250 }
251251
252252} // namespace litecore
0 commit comments