File: time.cpp

package info (click to toggle)
libfilezilla 0.52.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,492 kB
  • sloc: cpp: 30,965; sh: 4,241; makefile: 375; xml: 37
file content (145 lines) | stat: -rw-r--r-- 3,673 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "../lib/libfilezilla/time.hpp"
#include "../lib/libfilezilla/util.hpp"

#include <cppunit/extensions/HelperMacros.h>

#ifndef _MSC_VER
#include <unistd.h>
#endif

class TimeTest final : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE(TimeTest);
	CPPUNIT_TEST(testNow);
	CPPUNIT_TEST(testPreEpoch);
	CPPUNIT_TEST(testAlternateMidnight);
	CPPUNIT_TEST(testRFC822);
	CPPUNIT_TEST(testRFC3339);
	CPPUNIT_TEST_SUITE_END();

public:
	void setUp() {}
	void tearDown() {}

	void testNow();
	void testPreEpoch();

	void testAlternateMidnight();

	void testRFC822();
	void testRFC3339();
};

CPPUNIT_TEST_SUITE_REGISTRATION(TimeTest);

void TimeTest::testNow()
{
	fz::datetime const t1 = fz::datetime::now();

	fz::sleep(fz::duration::from_seconds(2));

	fz::datetime const t2 = fz::datetime::now();

	CPPUNIT_ASSERT(!t1.empty());
	CPPUNIT_ASSERT(!t2.empty());
	CPPUNIT_ASSERT(t2 > t1);

	auto const diff = t2 - t1;

	CPPUNIT_ASSERT(diff.get_seconds() >= 2);
	CPPUNIT_ASSERT(diff.get_seconds() < 4); // May fail if running on a computer for ants

	CPPUNIT_ASSERT(t1.get_time_t() > 1431333788); // The time this test was written
}

void TimeTest::testPreEpoch()
{
	fz::datetime const now = fz::datetime::now();

	fz::datetime const t1(fz::datetime::utc, 1957, 10, 4, 19, 28, 34);

	CPPUNIT_ASSERT(!t1.empty());
	CPPUNIT_ASSERT(t1 < now);

	CPPUNIT_ASSERT(t1.get_time_t() < -1);

	auto const tm1 = t1.get_tm(fz::datetime::utc);
	CPPUNIT_ASSERT_EQUAL(57, tm1.tm_year);
	CPPUNIT_ASSERT_EQUAL(9,  tm1.tm_mon);
	CPPUNIT_ASSERT_EQUAL(4,  tm1.tm_mday);
	CPPUNIT_ASSERT_EQUAL(19, tm1.tm_hour);
	CPPUNIT_ASSERT_EQUAL(28, tm1.tm_min);
	CPPUNIT_ASSERT_EQUAL(34, tm1.tm_sec);


	fz::datetime const t2(fz::datetime::utc, 1969, 12, 31, 23, 59, 59);

	CPPUNIT_ASSERT(!t2.empty());
	CPPUNIT_ASSERT(t2 > t1);
	CPPUNIT_ASSERT(t2 < now);

	auto const tm2 = t2.get_tm(fz::datetime::utc);
	CPPUNIT_ASSERT_EQUAL(69, tm2.tm_year);
	CPPUNIT_ASSERT_EQUAL(11, tm2.tm_mon);
	CPPUNIT_ASSERT_EQUAL(31, tm2.tm_mday);
	CPPUNIT_ASSERT_EQUAL(23, tm2.tm_hour);
	CPPUNIT_ASSERT_EQUAL(59, tm2.tm_min);
	CPPUNIT_ASSERT_EQUAL(59, tm2.tm_sec);
}

void TimeTest::testAlternateMidnight()
{
	fz::datetime const t1(fz::datetime::utc, 2016, 4, 13, 0, 0, 0);
	fz::datetime const t2(fz::datetime::utc, 2016, 4, 12, 24, 0, 0);
	fz::datetime const t3("2016-04-13 00:00:00", fz::datetime::utc);
	fz::datetime const t4("2016-04-12 24:00:00", fz::datetime::utc);

	CPPUNIT_ASSERT(!t1.empty());
	CPPUNIT_ASSERT(!t2.empty());
	CPPUNIT_ASSERT(!t3.empty());
	CPPUNIT_ASSERT(!t4.empty());

	CPPUNIT_ASSERT(t1 == t2);
	CPPUNIT_ASSERT(t1 == t3);
	CPPUNIT_ASSERT(t1 == t4);

	fz::datetime imbue("2016-04-12", fz::datetime::utc);
	CPPUNIT_ASSERT(imbue.imbue_time(24, 0, 0));
	CPPUNIT_ASSERT(t1 == imbue);

}

void TimeTest::testRFC822()
{
	fz::datetime const t1(fz::datetime::utc, 2020, 3, 2, 12, 35, 0);

	std::string s = t1.get_rfc822();
	CPPUNIT_ASSERT(!s.empty());

	fz::datetime t;
	CPPUNIT_ASSERT(t.set_rfc822(s));
	CPPUNIT_ASSERT(t == t1);

	std::string const offset1 = "Mon, 02 Mar 2020 13:35:00 +0100";
	std::string const offset2 = "Mon, 02 Mar 2020 07:35:00 -0500";

	CPPUNIT_ASSERT(t.set_rfc822(offset1));
	CPPUNIT_ASSERT(t == t1);
	CPPUNIT_ASSERT(t.set_rfc822(offset2));
	CPPUNIT_ASSERT(t == t1);
}

void TimeTest::testRFC3339()
{
	fz::datetime const t1(fz::datetime::utc, 1985, 4, 12, 23, 20, 50, 520);
	fz::datetime const t2(fz::datetime::utc, 1996, 12, 20, 0, 39, 57);

	std::string const s1 = "1985-04-12T23:20:50.52Z";
	std::string const s2 = "1996-12-19T16:39:57-08:00";

	fz::datetime t;
	CPPUNIT_ASSERT(t.set_rfc3339(s1));
	CPPUNIT_ASSERT(t == t1);
	CPPUNIT_ASSERT(t.set_rfc3339(s2));
	CPPUNIT_ASSERT(t == t2);
}