Line data Source code
1 : /*
2 : * Present - Date/Time Library
3 : *
4 : * Implementations of utility functions for Present tests
5 : *
6 : * Licensed under the MIT License.
7 : * For details, see LICENSE.
8 : */
9 :
10 : #include <assert.h>
11 : #include <time.h>
12 :
13 : #include "test-utils.hpp"
14 :
15 : // If we're using GCC, disable the annoying warning about missing initializers
16 : #ifdef __GNUC__
17 : # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
18 : #endif
19 :
20 : static const time_t JAN_1_1970_UNIX_TIMESTAMP = 0;
21 : static const time_t JUL_1_1970_UNIX_TIMESTAMP = 15638400;
22 :
23 2 : time_t get_local_time_zone_offset_for_january() {
24 2 : struct tm tm = {};
25 2 : tm.tm_year = 1970;
26 2 : tm.tm_mon = 1;
27 2 : tm.tm_mday = 1;
28 2 : tm.tm_hour = 0;
29 2 : tm.tm_min = 0;
30 2 : tm.tm_sec = 0;
31 2 : tm.tm_isdst = -1;
32 :
33 2 : time_t local_timestamp = mktime(&tm);
34 2 : assert(local_timestamp != -1);
35 :
36 2 : return JAN_1_1970_UNIX_TIMESTAMP - local_timestamp;
37 : }
38 :
39 2 : time_t get_local_time_zone_offset_for_july() {
40 2 : struct tm tm = {};
41 2 : tm.tm_year = 1970;
42 2 : tm.tm_mon = 7;
43 2 : tm.tm_mday = 1;
44 2 : tm.tm_hour = 0;
45 2 : tm.tm_min = 0;
46 2 : tm.tm_sec = 0;
47 2 : tm.tm_isdst = -1;
48 :
49 2 : time_t local_timestamp = mktime(&tm);
50 2 : assert(local_timestamp != -1);
51 :
52 2 : return JUL_1_1970_UNIX_TIMESTAMP - local_timestamp;
53 : }
54 :
55 58 : std::ostream & operator<<(std::ostream & os, ClockTime const & v) {
56 58 : os << "ClockTime{" << v.hour() << ":" << v.minute() << ":"
57 116 : << v.second_decimal() << "}";
58 :
59 58 : return os;
60 : }
61 :
62 2530 : std::ostream & operator<<(std::ostream & os, Date const & v) {
63 2530 : os << "Date{" << v.year() << "-" << v.month() << "-" << v.day() << "}";
64 :
65 2530 : return os;
66 : }
67 :
68 72 : std::ostream & operator<<(std::ostream & os, DayDelta const & v) {
69 72 : os << "DayDelta{" << v.days() << " days}";
70 :
71 72 : return os;
72 : }
73 :
74 48 : std::ostream & operator<<(std::ostream & os, MonthDelta const & v) {
75 48 : os << "MonthDelta{" << v.months() << " months}";
76 :
77 48 : return os;
78 : }
79 :
80 98 : std::ostream & operator<<(std::ostream & os, TimeDelta const & v) {
81 98 : os << "TimeDelta{";
82 98 : if (v.weeks() > 1) {
83 16 : os << v.weeks_decimal() << " weeks";
84 82 : } else if (v.days() > 1) {
85 4 : os << v.days_decimal() << " days";
86 78 : } else if (v.hours() > 1) {
87 14 : os << v.hours_decimal() << " hours";
88 64 : } else if (v.minutes() > 1) {
89 10 : os << v.minutes_decimal() << " minutes";
90 54 : } else if (v.seconds() > 1) {
91 10 : os << v.seconds_decimal() << " seconds";
92 44 : } else if (v.milliseconds() > 1) {
93 6 : os << v.milliseconds_decimal() << " ms";
94 38 : } else if (v.microseconds() > 1) {
95 6 : os << v.microseconds_decimal() << " us";
96 : } else {
97 32 : os << v.nanoseconds() << " ns";
98 : }
99 98 : os << "}";
100 :
101 98 : return os;
102 : }
103 :
104 0 : std::ostream & operator<<(std::ostream & os, Timestamp const & v) {
105 0 : const Date &d = v.get_date_utc();
106 0 : const ClockTime &c = v.get_clock_time_utc();
107 0 : os << "Timestamp{UTC="
108 0 : << d.year() << "-" << d.month() << "-" << d.day()
109 0 : << "_"
110 0 : << c.hour() << ":" << c.minute() << ":" << c.second_decimal()
111 0 : << "}";
112 :
113 0 : return os;
114 : }
115 :
|