File: datetime.cpp

package info (click to toggle)
cpprest 2.10.10-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,812 kB
  • sloc: cpp: 69,785; sh: 254; makefile: 167
file content (183 lines) | stat: -rw-r--r-- 6,057 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***
 * Copyright (C) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 *
 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 *
 * Tests for datetime-related utility functions and classes.
 *
 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 ****/

#include "stdafx.h"

using namespace utility;

namespace tests
{
namespace functional
{
namespace utils_tests
{
SUITE(datetime)
{
    // This is by no means a comprehensive test suite for the datetime functionality.
    // It's a response to a particular bug and should be amended over time.

    TEST(parsing_dateandtime_basic)
    {
        // ISO 8601
        // RFC 1123

        auto dt1 = utility::datetime::from_string(_XPLATSTR("20130517T00:00:00Z"), utility::datetime::ISO_8601);
        VERIFY_ARE_NOT_EQUAL(0u, dt1.to_interval());

        auto dt2 =
            utility::datetime::from_string(_XPLATSTR("Fri, 17 May 2013 00:00:00 GMT"), utility::datetime::RFC_1123);
        VERIFY_ARE_NOT_EQUAL(0u, dt2.to_interval());

        VERIFY_ARE_EQUAL(dt1.to_interval(), dt2.to_interval());
    }

    TEST(parsing_dateandtime_extended)
    {
        // ISO 8601
        // RFC 1123

        auto dt1 = utility::datetime::from_string(_XPLATSTR("2013-05-17T00:00:00Z"), utility::datetime::ISO_8601);
        VERIFY_ARE_NOT_EQUAL(0u, dt1.to_interval());

        auto dt2 =
            utility::datetime::from_string(_XPLATSTR("Fri, 17 May 2013 00:00:00 GMT"), utility::datetime::RFC_1123);
        VERIFY_ARE_NOT_EQUAL(0u, dt2.to_interval());

        VERIFY_ARE_EQUAL(dt1.to_interval(), dt2.to_interval());
    }

    TEST(parsing_date_basic)
    {
        // ISO 8601
        {
            auto dt = utility::datetime::from_string(_XPLATSTR("20130517"), utility::datetime::ISO_8601);

            VERIFY_ARE_NOT_EQUAL(0u, dt.to_interval());
        }
    }

    TEST(parsing_date_extended)
    {
        // ISO 8601
        {
            auto dt = utility::datetime::from_string(_XPLATSTR("2013-05-17"), utility::datetime::ISO_8601);

            VERIFY_ARE_NOT_EQUAL(0u, dt.to_interval());
        }
    }

    TEST(parsing_time_extended)
    {
        // ISO 8601
        {
            auto dt = utility::datetime::from_string(_XPLATSTR("14:30:01Z"), utility::datetime::ISO_8601);

            VERIFY_ARE_NOT_EQUAL(0u, dt.to_interval());
        }
    }

    void TestDateTimeRoundtrip(utility::string_t str, utility::string_t strExpected)
    {
        auto dt = utility::datetime::from_string(str, utility::datetime::ISO_8601);
        utility::string_t str2 = dt.to_string(utility::datetime::ISO_8601);
        VERIFY_ARE_EQUAL(str2, strExpected);
    }

    void TestDateTimeRoundtrip(utility::string_t str) { TestDateTimeRoundtrip(str, str); }

    TEST(parsing_time_roundtrip_datetime1)
    {
        // Preserve all 7 digits after the comma:
        TestDateTimeRoundtrip(_XPLATSTR("2013-11-19T14:30:59.1234567Z"));
    }

    TEST(parsing_time_roundtrip_datetime2)
    {
        // lose the last '999' without rounding up
        TestDateTimeRoundtrip(_XPLATSTR("2013-11-19T14:30:59.1234567999Z"), _XPLATSTR("2013-11-19T14:30:59.1234567Z"));
    }

    TEST(parsing_time_roundtrip_datetime3)
    {
        // leading 0-s after the comma, tricky to parse correctly
        TestDateTimeRoundtrip(_XPLATSTR("2013-11-19T14:30:59.00123Z"));
    }

    TEST(parsing_time_roundtrip_datetime4)
    {
        // another leading 0 test
        TestDateTimeRoundtrip(_XPLATSTR("2013-11-19T14:30:59.0000001Z"));
    }

    TEST(parsing_time_roundtrip_datetime5)
    {
        // this is going to be truncated
        TestDateTimeRoundtrip(_XPLATSTR("2013-11-19T14:30:59.00000001Z"), _XPLATSTR("2013-11-19T14:30:59Z"));
    }

    TEST(parsing_time_roundtrip_datetime6)
    {
        // Only one digit after the dot
        TestDateTimeRoundtrip(_XPLATSTR("2013-11-19T14:30:59.5Z"));
    }

    TEST(parsing_time_roundtrip_datetime_invalid1,
         "Ignore:Linux",
         "Codeplex issue #115",
         "Ignore:Apple",
         "Codeplex issue #115")
    {
        // No digits after the dot, or non-digits. This is not a valid input, but we should not choke on it,
        // Simply ignore the bad fraction
        const utility::string_t bad_strings[] = {_XPLATSTR("2013-11-19T14:30:59.Z"),
                                                 _XPLATSTR("2013-11-19T14:30:59.1a2Z")};
        utility::string_t str_corrected = _XPLATSTR("2013-11-19T14:30:59Z");

        for (const auto& str : bad_strings)
        {
            auto dt = utility::datetime::from_string(str, utility::datetime::ISO_8601);
            utility::string_t str2 = dt.to_string(utility::datetime::ISO_8601);
            VERIFY_ARE_EQUAL(str2, str_corrected);
        }
    }

    TEST(parsing_time_roundtrip_datetime_invalid2)
    {
        // Variouls unsupported cases. In all cases, we have produce an empty date time
        const utility::string_t bad_strings[] = {
            _XPLATSTR(""),     // empty
            _XPLATSTR(".Z"),   // too short
            _XPLATSTR(".Zx"),  // no trailing Z
            _XPLATSTR("3.14Z") // not a valid date
        };

        for (const auto& str : bad_strings)
        {
            auto dt = utility::datetime::from_string(str, utility::datetime::ISO_8601);
            VERIFY_ARE_EQUAL(dt.to_interval(), 0);
        }
    }

    TEST(parsing_time_roundtrip_time)
    {
        // time only without date
        utility::string_t str = _XPLATSTR("14:30:59.1234567Z");
        auto dt = utility::datetime::from_string(str, utility::datetime::ISO_8601);
        utility::string_t str2 = dt.to_string(utility::datetime::ISO_8601);
        // Must look for a substring now, since the date part is filled with today's date
        VERIFY_IS_TRUE(str2.find(str) != std::string::npos);
    }

} // SUITE(datetime)

} // namespace utils_tests
} // namespace functional
} // namespace tests