Skip to content

Commit 507d9b3

Browse files
committed
documentation and removed dead code from TimePoint
1 parent dfb4520 commit 507d9b3

4 files changed

Lines changed: 36 additions & 20 deletions

File tree

include/reflection.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#include <stddef.h>
3636
#endif
3737

38-
// todo: saving/retrieving dates (get/set local/utc time functions)
39-
// todo: cpp+20 for date type
4038
// todo: template specialization for int64_t and wchar_t*
4139

4240
// todo: bool member type

include/time_point.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,33 @@
3030
typedef std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> sys_seconds;
3131

3232
namespace sqlite_reflection {
33+
/// A wrapper for expressing points in time based on system time.
34+
/// The epoch is by convention the Unix epoch of 1 January 1970 at midnight (1970-01-01T00:00:00Z)
35+
/// System time is a very good approximation of UTC time, the only difference being the management of leap seconds
36+
/// (system time ignores leap seconds)
37+
/// https://en.wikipedia.org/wiki/Coordinated_Universal_Time
3338
class REFLECTION_EXPORT TimePoint
3439
{
3540
public:
41+
/// Defaults to start of system time (Unix epoch)
3642
TimePoint();
43+
44+
// Creates a time representation based on the elapsed seconds from the Unix epoch.
45+
// Negative valules represent time points before 1970-01-01T00:00:00Z,
46+
// whereas positive values after 1970-01-01 00:00:00 UTC
3747
explicit TimePoint(const int64_t& seconds_since_unix_epoch);
48+
49+
// Creates a time representation based on the elapsed seconds from the Unix epoch.
50+
// Negative valules represent time points before 1970-01-01T00:00:00Z,
51+
// whereas positive values after 1970-01-01 00:00:00 UTC
3852
explicit TimePoint(const sys_seconds& time_since_unix_epoch);
53+
54+
/// Creates a time point instance from its equivalent ISO 8601 UTC format
3955
static TimePoint FromSystemTime(const std::wstring& iso_8601_string);
40-
//static TimePoint FromLocalTime(const std::wstring& timestamp);
41-
56+
57+
/// Returns a string representation of this instance, expressed in ISO 8601 UTC format
58+
/// https://en.wikipedia.org/wiki/ISO_8601
4259
std::wstring SystemTime() const;
43-
//std::wstring LocalTimestamp() const;
4460

4561
private:
4662
sys_seconds time_stamp_;

src/time_point.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "time_point.h"
2424
#include "internal/string_utilities.h"
25+
26+
// This would not have been at all possible without this amazing library
2527
#include "internal/date.h"
2628

2729
#include <sstream>
@@ -30,20 +32,20 @@
3032
using namespace sqlite_reflection;
3133

3234
#if __cplusplus < 201907L
33-
#define LEGACY_CHRONO
35+
#define LEGACY_CHRONO
36+
#else
37+
#ifdef _WIN32
38+
#define MODERN_WIN_CHRONO
3439
#else
35-
#ifdef _WIN32
36-
#define MODERN_WIN_CHRONO
37-
#else
38-
#define MODERN_UNIX_CHRONO
39-
#endif
40+
#define MODERN_UNIX_CHRONO
41+
#endif
4042
#endif
4143

4244
#if defined(LEGACY_CHRONO) || !defined(MODERN_WIN_CHRONO)
4345
using namespace date;
4446
#endif
4547

46-
static std::wstring iso_format = L"%FT%T";
48+
static std::wstring iso_format = L"%FT%TZ";
4749

4850
TimePoint::TimePoint() {}
4951

@@ -68,9 +70,9 @@ std::wstring TimePoint::SystemTime() const {
6870
#endif
6971
std::stringstream ss;
7072
#if defined(LEGACY_CHRONO) || !defined(MODERN_WIN_CHRONO)
71-
ss << tp << "T" << make_time(time_stamp_ - tp) << " UTC";
73+
ss << tp << "T" << make_time(time_stamp_ - tp) << "Z";
7274
#else
73-
ss << tp << "T" << std::chrono::hh_mm_ss(time_stamp_ - tp) << " UTC";
75+
ss << tp << "T" << std::chrono::hh_mm_ss(time_stamp_ - tp) << "Z";
7476
#endif
7577
return StringUtilities::FromUtf8(ss.str().c_str());
7678
}

tests/date_time_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,25 @@ void ControlRoundTrip(const int64_t& time_point, const wchar_t* sys_time) {
6666
}
6767

6868
TEST_F(DateTimeTest, UnixEpoch) {
69-
ControlRoundTrip(0, L"1970-01-01T00:00:00 UTC");
69+
ControlRoundTrip(0, L"1970-01-01T00:00:00Z");
7070
}
7171

7272
TEST_F(DateTimeTest, RoundtripBefore1000) {
73-
ControlRoundTrip(-32350628573LL, L"944-11-06T10:17:07 UTC");
73+
ControlRoundTrip(-32350628573LL, L"944-11-06T10:17:07Z");
7474
}
7575

7676
TEST_F(DateTimeTest, RoundtripBefore1900) {
77-
ControlRoundTrip(-2359097130LL, L"1895-03-30T15:14:30 UTC");
77+
ControlRoundTrip(-2359097130LL, L"1895-03-30T15:14:30Z");
7878
}
7979

8080
TEST_F(DateTimeTest, RoundtripBefore1970) {
81-
ControlRoundTrip(-1508726294, L"1922-03-11T21:21:46 UTC");
81+
ControlRoundTrip(-1508726294, L"1922-03-11T21:21:46Z");
8282
}
8383

8484
TEST_F(DateTimeTest, RoundtripAfter2000) {
85-
ControlRoundTrip(1688842924, L"2023-07-08T19:02:04 UTC");
85+
ControlRoundTrip(1688842924, L"2023-07-08T19:02:04Z");
8686
}
8787

8888
TEST_F(DateTimeTest, RoundtripAfter2038) {
89-
ControlRoundTrip(2333089535, L"2043-12-07T08:25:35 UTC");
89+
ControlRoundTrip(2333089535, L"2043-12-07T08:25:35Z");
9090
}

0 commit comments

Comments
 (0)