1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include <gtest/gtest.h>
18 
19 #include "cassandra.h"
20 
21 #include <time.h>
22 
23 #define CASS_DATE_EPOCH 2147483648U
24 
TEST(DateTimeUnitTests,Simple)25 TEST(DateTimeUnitTests, Simple) {
26   time_t now = ::time(NULL);
27   cass_uint32_t date = cass_date_from_epoch(now);
28   cass_int64_t time = cass_time_from_epoch(now);
29   EXPECT_EQ(cass_date_time_to_epoch(date, time), now);
30 }
31 
TEST(DateTimeUnitTests,DateFromEpoch)32 TEST(DateTimeUnitTests, DateFromEpoch) {
33   EXPECT_EQ(cass_date_from_epoch(0), CASS_DATE_EPOCH);
34   EXPECT_EQ(cass_date_from_epoch(24 * 3600), CASS_DATE_EPOCH + 1);
35   EXPECT_EQ(cass_date_from_epoch(2 * 24 * 3600), CASS_DATE_EPOCH + 2);
36 }
37 
TEST(DateTimeUnitTests,TimeFromEpoch)38 TEST(DateTimeUnitTests, TimeFromEpoch) {
39   time_t now = ::time(NULL);
40   struct tm* tm = gmtime(&now);
41   cass_int64_t actual = cass_time_from_epoch(now);
42   cass_int64_t expected = 1000000000LL * (tm->tm_hour * 3600 + 60 * tm->tm_min + tm->tm_sec);
43   EXPECT_EQ(actual, expected);
44 }
45 
TEST(DateTimeUnitTests,DateTimeToEpoch)46 TEST(DateTimeUnitTests, DateTimeToEpoch) {
47   EXPECT_EQ(cass_date_time_to_epoch(CASS_DATE_EPOCH, 0), 0L);               // Epoch
48   EXPECT_EQ(cass_date_time_to_epoch(CASS_DATE_EPOCH - 1, 0), -24L * 3600L); // Epoch - 1 day
49   EXPECT_EQ(cass_date_time_to_epoch(CASS_DATE_EPOCH + 1, 0), 24L * 3600L);  // Epoch + 1 day
50 }
51