| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "plugin/x/client/mysqlxclient/xdatetime.h" |
| 28 | |
| 29 | namespace xpl { |
| 30 | |
| 31 | namespace test { |
| 32 | |
| 33 | TEST(xdatetime, date) { |
| 34 | EXPECT_TRUE(xcl::DateTime(2015, 12, 5)); |
| 35 | EXPECT_EQ("2015-12-05", xcl::DateTime(2015, 12, 5).to_string()); |
| 36 | EXPECT_EQ("2015-12-05", xcl::DateTime(2015, 12, 5).to_string()); |
| 37 | EXPECT_EQ("0001-01-01", xcl::DateTime(1, 1, 1).to_string()); |
| 38 | EXPECT_EQ("0000-00-00", xcl::DateTime(0, 0, 0).to_string()); |
| 39 | EXPECT_EQ("9999-12-31", xcl::DateTime(9999, 12, 31).to_string()); |
| 40 | EXPECT_FALSE(xcl::DateTime(0, 50, 60)); |
| 41 | } |
| 42 | |
| 43 | TEST(xdatetime, datetime) { |
| 44 | EXPECT_EQ("2015-12-05 00:00:00.123456", |
| 45 | xcl::DateTime(2015, 12, 5, 0, 0, 0, 123456).to_string()); |
| 46 | EXPECT_EQ("0001-01-01 23:45:59.99", |
| 47 | xcl::DateTime(1, 1, 1, 23, 45, 59, 990000).to_string()); |
| 48 | EXPECT_EQ("0000-00-00 00:00:00", |
| 49 | xcl::DateTime(0, 0, 0, 0, 0, 0, 0).to_string()); |
| 50 | EXPECT_EQ("9999-12-31 23:59:59.999999", |
| 51 | xcl::DateTime(9999, 12, 31, 23, 59, 59, 999999).to_string()); |
| 52 | |
| 53 | EXPECT_EQ("23:59:59.999999", |
| 54 | xcl::DateTime(9999, 12, 31, 23, 59, 59, 999999).time().to_string()); |
| 55 | |
| 56 | EXPECT_FALSE(xcl::DateTime(0, 50, 60, 24, 60, 60, 0)); |
| 57 | } |
| 58 | |
| 59 | TEST(xdatetime, time) { |
| 60 | EXPECT_EQ("00:00:00.123456", xcl::Time(false, 0, 0, 0, 123456).to_string()); |
| 61 | EXPECT_EQ("00:00:00.1234", xcl::Time(false, 0, 0, 0, 123400).to_string()); |
| 62 | EXPECT_EQ("23:45:59.99", xcl::Time(false, 23, 45, 59, 990000).to_string()); |
| 63 | EXPECT_EQ("00:00:00", xcl::Time(false, 0, 0, 0).to_string()); |
| 64 | EXPECT_EQ("23:59:59.999999", |
| 65 | xcl::Time(false, 23, 59, 59, 999999).to_string()); |
| 66 | EXPECT_EQ("00:00:00.33", xcl::Time(false, 0, 0, 0, 330000).to_string()); |
| 67 | EXPECT_EQ("-00:00:00.33", xcl::Time(true, 0, 0, 0, 330000).to_string()); |
| 68 | EXPECT_EQ("-821:00:00.33", xcl::Time(true, 821, 0, 0, 330000).to_string()); |
| 69 | |
| 70 | EXPECT_FALSE(xcl::Time(false, 24, 60, 60)); |
| 71 | } |
| 72 | |
| 73 | TEST(xdatetime, useconds_representation) { |
| 74 | EXPECT_EQ("00:00:00", xcl::Time(false, 0, 0, 0, 0).to_string()); |
| 75 | EXPECT_EQ("00:00:00.000001", xcl::Time(false, 0, 0, 0, 1).to_string()); |
| 76 | EXPECT_EQ("00:00:00.001234", xcl::Time(false, 0, 0, 0, 1234).to_string()); |
| 77 | EXPECT_EQ("00:00:00.999999", |
| 78 | xcl::Time(false, 00, 00, 00, 999999).to_string()); |
| 79 | |
| 80 | EXPECT_EQ("00:00:00", |
| 81 | xcl::DateTime(0000, 00, 00, 00, 00, 00, 0).time().to_string()); |
| 82 | EXPECT_EQ("00:00:00.000001", |
| 83 | xcl::DateTime(0000, 00, 00, 00, 00, 00, 1).time().to_string()); |
| 84 | EXPECT_EQ("00:00:00.001234", |
| 85 | xcl::DateTime(0000, 00, 00, 00, 00, 00, 1234).time().to_string()); |
| 86 | EXPECT_EQ("00:00:00.999999", |
| 87 | xcl::DateTime(0000, 00, 00, 00, 00, 00, 999999).time().to_string()); |
| 88 | } |
| 89 | |
| 90 | } // namespace test |
| 91 | |
| 92 | } // namespace xpl |