Skip to content

Commit f2b4c87

Browse files
committed
CBL-7773: Remove date.h and use std::chrono
1 parent 745e651 commit f2b4c87

8 files changed

Lines changed: 91 additions & 92 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Platform logic is largely separated into the cmake/platform_*.cmake files. Plat
4040
cmake_minimum_required (VERSION 3.21...4.0)
4141

4242
# Mac/apple setup -- must appear before the first "project()" line"
43-
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0")
43+
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.3")
4444
if(NOT DEFINED CMAKE_OSX_SYSROOT)
4545
# Tells Mac builds to use the current SDK's headers & libs, not what's in the OS.
4646
set(CMAKE_OSX_SYSROOT macosx)

Crypto/Certificate.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <algorithm>
3838
#include <cstdlib>
3939
#include <chrono>
40-
#include "date/date.h"
4140

4241
namespace litecore::crypto {
4342
using namespace std;
@@ -262,8 +261,8 @@ namespace litecore::crypto {
262261
auto now = floor<seconds>(system_clock::now()) - 60s;
263262
auto exp = now + seconds(issuerParams.validity_secs);
264263
stringstream notBefore, notAfter;
265-
notBefore << date::format("%Y%m%d%H%M%S", now);
266-
notAfter << date::format("%Y%m%d%H%M%S", exp);
264+
notBefore << std::format("{:%Y%m%d%H%M%S}", now);
265+
notAfter << std::format("{:%Y%m%d%H%M%S}", exp);
267266

268267
// Set certificate attributes:
269268
mbedtls_x509write_crt_set_subject_key(&crt, subjectKey->context());
@@ -345,8 +344,8 @@ namespace litecore::crypto {
345344
}
346345

347346
static time_t x509_to_time_t(const mbedtls_x509_time& xtime) {
348-
date::sys_days date = date::year{xtime.year} / xtime.mon / xtime.day;
349-
date::sys_seconds datetime = date + (hours(xtime.hour) + minutes(xtime.min) + seconds(xtime.sec));
347+
sys_days date = year{xtime.year} / xtime.mon / xtime.day;
348+
sys_seconds datetime = date + (hours(xtime.hour) + minutes(xtime.min) + seconds(xtime.sec));
350349

351350
// The limit of 32-bit time_t is approaching...
352351
auto result = datetime.time_since_epoch().count();

LiteCore/Logging/LogDecoder.cc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <cstring>
1919
#include <chrono>
2020
#include <algorithm>
21-
#include "date/date.h"
2221
#include "ParseDate.hh"
2322
#include "NumConversion.hh"
2423

@@ -51,27 +50,29 @@ namespace litecore {
5150
}
5251

5352
void LogIterator::writeTimestamp(Timestamp t, ostream& out, bool inUtcTime) {
54-
date::local_time<microseconds> tp{seconds(t.secs) + microseconds(t.microsecs)};
55-
const char* fmt = "%FT%TZ ";
56-
if ( !inUtcTime ) {
57-
struct tm tmpTime = FromTimestamp(duration_cast<seconds>(tp.time_since_epoch()));
53+
if ( inUtcTime ) {
54+
writeISO8601DateTime(t, out);
55+
out << " ";
56+
} else {
57+
local_time<microseconds> tp{seconds(t.secs) + microseconds(t.microsecs)};
58+
struct tm tmpTime = FromTimestamp(duration_cast<seconds>(tp.time_since_epoch()));
59+
// Updates tp to the local time.
5860
tp += GetLocalTZOffset(&tmpTime, true);
59-
fmt = "%FT%T ";
61+
out << std::format("{:%FT%T }", tp);
6062
}
61-
out << date::format(fmt, tp);
6263
}
6364

6465
void LogIterator::writeISO8601DateTime(Timestamp t, std::ostream& out) {
65-
date::sys_time<microseconds> tp(seconds(t.secs) + microseconds(t.microsecs));
66-
out << date::format("%FT%TZ", tp);
66+
sys_time<microseconds> tp(seconds(t.secs) + microseconds(t.microsecs));
67+
out << std::format("{:%FT%TZ}", tp);
6768
}
6869

6970
string LogIterator::formatDate(Timestamp t) {
70-
date::local_time<microseconds> tp(seconds(t.secs) + microseconds(t.microsecs));
71-
struct tm tmpTime = FromTimestamp(duration_cast<seconds>(tp.time_since_epoch()));
71+
local_time<microseconds> tp(seconds(t.secs) + microseconds(t.microsecs));
72+
struct tm tmpTime = FromTimestamp(duration_cast<seconds>(tp.time_since_epoch()));
7273
tp += GetLocalTZOffset(&tmpTime, true);
7374
stringstream out;
74-
out << date::format("%c", tp);
75+
out << std::format("%c", tp);
7576
return out.str();
7677
}
7778

@@ -148,8 +149,8 @@ namespace litecore {
148149
std::optional<Timestamp> startingAt) {
149150
if ( !startingAt || *startingAt < Timestamp{_startTime, 0} ) {
150151
writeTimestamp({_startTime, 0}, out, true);
151-
date::local_time<seconds> tp{seconds(_startTime)};
152-
out << "---- Logging begins on " << date::format("%A %FT%TZ", tp) << " ----" << endl;
152+
local_time<seconds> tp{seconds(_startTime)};
153+
out << "---- Logging begins on " << std::format("{:%A %FT%TZ}", tp) << " ----" << endl;
153154
}
154155

155156
LogIterator::decodeTo(out, levelNames, startingAt);

LiteCore/Query/DateFormat.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "DateFormat.hh"
22
#include "ParseDate.hh"
3-
#include "date/date.h"
43
#include "fleece/slice.hh"
54
#include <optional>
65
#include <slice_stream.hh>
@@ -19,8 +18,7 @@ namespace fleece {
1918

2019
const DateFormat DateFormat::kISO8601 = DateFormat{YMD::kISO8601, Separator::T, HMS::kISO8601, {Timezone::NoColon}};
2120

22-
/** This parses a subset of the formatting tokens from "date.h", found
23-
* here: https://howardhinnant.github.io/date/date.html#to_stream_formatting.
21+
/** This parses a subset of the formatting tokens of std::format
2422
* The valid tokens are:
2523
* %Y: Year (YYYY), %m: Month (MM), %d: Day (DD).
2624
* %F == %Y-%m-%d
@@ -326,31 +324,31 @@ namespace fleece {
326324
std::ostringstream stream;
327325

328326
const milliseconds millis{milliseconds{timestamp} + duration_cast<milliseconds>(tzoffset)};
329-
const auto tm = date::local_time<milliseconds>{millis};
330-
331-
const seconds offset_seconds{tzoffset};
327+
const auto tm = local_time<milliseconds>{millis};
332328

333329
const DateFormat f = fmt.has_value() ? fmt.value() : kISO8601;
334330

335-
if ( f.ymd.has_value() ) { stream << date::format("%F", tm); }
331+
if ( f.ymd.has_value() ) { stream << std::format("{:%F}", tm); }
336332

337333
if ( f.hms.has_value() ) {
338334
if ( f.ymd.has_value() ) { stream << (char)f.separator.value(); }
339335

340336
if ( f.hms.value().millis && timestamp % 1000 ) {
341-
stream << date::format("%T", tm);
337+
stream << std::format("{:%T}", tm);
342338
} else {
343339
const auto secs = duration_cast<seconds>(millis);
344-
stream << date::format("%T", date::local_seconds(secs));
340+
stream << std::format("{:%T}", local_seconds(secs));
345341
}
346342

347343
if ( f.tz.has_value() ) {
348-
if ( offset_seconds.count() == 0 ) {
344+
if ( tzoffset.count() == 0 ) {
349345
stream << 'Z';
350346
} else {
351-
if ( f.tz.value() == Timezone::Colon ) to_stream(stream, "%Ez", tm, nullptr, &offset_seconds);
352-
else
353-
to_stream(stream, "%z", tm, nullptr, &offset_seconds);
347+
char sign = tzoffset.count() < 0 ? '-' : '+';
348+
hh_mm_ss hms{sign == '-' ? -tzoffset : tzoffset};
349+
stream << std::format("{}{:02}", sign, hms.hours().count());
350+
if ( f.tz.value() == Timezone::Colon ) stream << ":";
351+
stream << std::format("{:02}", hms.minutes().count());
354352
}
355353
}
356354
}

LiteCore/Query/SQLiteDateTimeHelpers.hh

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
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

3233
namespace 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

LiteCore/tests/QueryTest.cc

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <chrono>
2222
#include <limits>
2323
#include <numeric>
24-
#include "date/date.h"
2524
#include "ParseDate.hh"
2625
#include "SecureDigest.hh"
2726
#include <functional>
@@ -1818,40 +1817,42 @@ N_WAY_TEST_CASE_METHOD(QueryTest, "Query Distance Metrics", "[Query]") {
18181817

18191818

18201819
N_WAY_TEST_CASE_METHOD(QueryTest, "Query Date Functions", "[Query][CBL-59]") {
1821-
constexpr date::local_seconds localtime = date::local_days{date::year(2018) / 10 / 23};
1822-
tm tmpTime = FromTimestamp(localtime.time_since_epoch());
1823-
const seconds offset_seconds = GetLocalTZOffset(&tmpTime, false);
1824-
date::local_seconds utc_time = localtime - offset_seconds;
1820+
constexpr local_seconds localtime = local_days{year(2018) / 10 / 23};
1821+
tm tmpTime = FromTimestamp(localtime.time_since_epoch());
1822+
const seconds offset_seconds = GetLocalTZOffset(&tmpTime, false);
1823+
local_seconds utc_time = localtime - offset_seconds;
18251824

18261825
// MILLIS_TO_STR() result should be in localtime.
1827-
stringstream mil_to_str;
1828-
constexpr date::local_seconds mil_to_str_time = localtime + 18h + 33min + 1s;
1829-
mil_to_str << date::format("%FT%T", mil_to_str_time + offset_seconds);
1826+
stringstream mil_to_str;
1827+
constexpr local_seconds mil_to_str_time = localtime + 18h + 33min + 1s;
1828+
mil_to_str << std::format("{:%FT%T}", mil_to_str_time + offset_seconds);
18301829
if ( offset_seconds.count() == 0 ) {
18311830
mil_to_str << "Z";
18321831
} else {
1833-
to_stream(mil_to_str, "%z", mil_to_str_time, nullptr, &offset_seconds);
1832+
char sign = offset_seconds.count() < 0 ? '-' : '+';
1833+
hh_mm_ss hms{sign == '-' ? -offset_seconds : offset_seconds};
1834+
mil_to_str << std::format("{}{:02}{:02}", sign, hms.hours().count(), hms.minutes().count());
18341835
}
18351836
const auto mil_to_str_expected = mil_to_str.str();
18361837

18371838
// These are all for STR_TO_UTC
18381839
stringstream s1, s2, s3, s5;
18391840
stringstream s1iso, s2iso, s3iso;
1840-
s1 << date::format("%F", utc_time);
1841-
s1iso << date::format("%FT%TZ", utc_time);
1841+
s1 << std::format("{:%F}", utc_time);
1842+
s1iso << std::format("{:%FT%TZ}", utc_time);
18421843
utc_time += 18h + 33min;
1843-
s2 << date::format("%FT%TZ", utc_time);
1844-
s2iso << date::format("%FT%TZ", utc_time);
1844+
s2 << std::format("{:%FT%TZ}", utc_time);
1845+
s2iso << std::format("{:%FT%TZ}", utc_time);
18451846
utc_time += 1s;
1846-
s3 << date::format("%FT%T", utc_time);
1847-
s3iso << date::format("%FT%TZ", utc_time);
1848-
s5 << date::format("%FT%TZ", utc_time);
1847+
s3 << std::format("{:%FT%T}", utc_time);
1848+
s3iso << std::format("{:%FT%TZ}", utc_time);
1849+
s5 << std::format("{:%FT%TZ}", utc_time);
18491850

1850-
constexpr date::local_seconds localtime2 = date::local_days{date::year(1944) / 6 / 6} + 6h + 30min;
1851-
tmpTime = FromTimestamp(localtime2.time_since_epoch());
1852-
utc_time = localtime2 - GetLocalTZOffset(&tmpTime, false);
1851+
constexpr local_seconds localtime2 = local_days{year(1944) / 6 / 6} + 6h + 30min;
1852+
tmpTime = FromTimestamp(localtime2.time_since_epoch());
1853+
utc_time = localtime2 - GetLocalTZOffset(&tmpTime, false);
18531854
stringstream s4;
1854-
s4 << date::format("%FT%TZ", utc_time);
1855+
s4 << std::format("{:%FT%TZ}", utc_time);
18551856

18561857
auto expected1 = s1.str();
18571858
auto expected2 = s2.str();

Xcode/xcconfigs/Project.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LITECORE_VERSION_STRING = 0.0.0
2525
LITECORE_BUILD_NUMBER = 0
2626

2727
IPHONEOS_DEPLOYMENT_TARGET = 15.0
28-
MACOSX_DEPLOYMENT_TARGET = 13.0
28+
MACOSX_DEPLOYMENT_TARGET = 13.3
2929
TVOS_DEPLOYMENT_TARGET = 12.0
3030
ONLY_ACTIVE_ARCH = YES
3131
SKIP_INSTALL = YES

0 commit comments

Comments
 (0)