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 #ifndef __TEST_DATE_HPP__
18 #define __TEST_DATE_HPP__
19 #include "nullable_value.hpp"
20 
21 #ifdef min
22 #undef min
23 #endif
24 #ifdef max
25 #undef max
26 #endif
27 
28 namespace test { namespace driver { namespace values {
29 
30 /**
31  * Date wrapped value
32  */
33 class Date {
34 public:
35   typedef cass_uint32_t ConvenienceType;
36   typedef cass_uint32_t ValueType;
37 
Date()38   Date()
39       : date_(0) {}
40 
Date(const ConvenienceType date)41   Date(const ConvenienceType date)
42       : date_(date) {}
43 
append(Collection collection)44   void append(Collection collection) {
45     ASSERT_EQ(CASS_OK, cass_collection_append_uint32(collection.get(), date_));
46   }
47 
cql_type() const48   std::string cql_type() const { return "date"; }
49 
cql_value() const50   std::string cql_value() const { return "'" + str() + "'"; }
51 
52   /**
53    * Comparison operation for driver unsigned integers (e.g. date)
54    *
55    * @param rhs Right hand side to compare
56    * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
57    */
compare(const cass_uint32_t & rhs) const58   int compare(const cass_uint32_t& rhs) const {
59     if (date_ < rhs) return -1;
60     if (date_ > rhs) return 1;
61 
62     return 0;
63   }
64 
65   /**
66    * Comparison operation for driver date
67    *
68    * @param rhs Right hand side to compare
69    * @return -1 if LHS < RHS, 1 if LHS > RHS, and 0 if equal
70    */
compare(const Date & rhs) const71   int compare(const Date& rhs) const { return compare(rhs.date_); }
72 
initialize(const CassValue * value)73   void initialize(const CassValue* value) {
74     ASSERT_EQ(CASS_OK, cass_value_get_uint32(value, &date_))
75         << "Unable to Get Date: Invalid error code returned";
76   }
77 
max()78   static Date max() {
79     return Date(2147533357u); // Maximum value supported by strftime()
80   }
81 
min()82   static Date min() {
83     return Date(2147483648u); // Minimum value supported by strftime()
84   }
85 
set(Tuple tuple,size_t index)86   void set(Tuple tuple, size_t index) {
87     ASSERT_EQ(CASS_OK, cass_tuple_set_uint32(tuple.get(), index, date_));
88   }
89 
set(UserType user_type,const std::string & name)90   void set(UserType user_type, const std::string& name) {
91     ASSERT_EQ(CASS_OK, cass_user_type_set_uint32_by_name(user_type.get(), name.c_str(), date_));
92   }
93 
statement_bind(Statement statement,size_t index)94   void statement_bind(Statement statement, size_t index) {
95     ASSERT_EQ(CASS_OK, cass_statement_bind_uint32(statement.get(), index, date_));
96   }
97 
statement_bind(Statement statement,const std::string & name)98   void statement_bind(Statement statement, const std::string& name) {
99     ASSERT_EQ(CASS_OK, cass_statement_bind_uint32_by_name(statement.get(), name.c_str(), date_));
100   }
101 
str() const102   std::string str() const {
103     // Convert the date to a human readable format
104     char date_string[32];
105     time_t epoch_secs = static_cast<time_t>(cass_date_time_to_epoch(date_, 0));
106     strftime(date_string, sizeof(date_string), "%Y-%m-%d", gmtime(&epoch_secs));
107     return date_string;
108   }
109 
supported_server_version()110   static std::string supported_server_version() { return "2.2.3"; }
111 
value() const112   cass_uint32_t value() const { return date_; }
113 
value_type() const114   CassValueType value_type() const { return CASS_VALUE_TYPE_DATE; }
115 
116 protected:
117   /**
118    * Native driver value
119    */
120   cass_uint32_t date_;
121 };
122 
operator <<(std::ostream & output_stream,const Date & value)123 inline std::ostream& operator<<(std::ostream& output_stream, const Date& value) {
124   // Output both values
125   output_stream << value.cql_value() << " [ = " << value.value() << "]";
126   return output_stream;
127 }
128 
129 }}} // namespace test::driver::values
130 
131 #endif // __TEST_DATE_HPP__
132