00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022 #include <boost/lexical_cast.hpp>
00023 #include <drizzled/field/microtime.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/table.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/session/times.h>
00028 #include <drizzled/current_session.h>
00029 #include <drizzled/temporal.h>
00030 #include <cmath>
00031 #include <sstream>
00032 #include <boost/date_time/posix_time/posix_time.hpp>
00033
00034
00035 namespace drizzled {
00036 namespace field {
00037
00038 static boost::posix_time::ptime _epoch(boost::gregorian::date(1970, 1, 1));
00039
00040 Microtime::Microtime(unsigned char *ptr_arg,
00041 unsigned char *null_ptr_arg,
00042 unsigned char null_bit_arg,
00043 enum utype unireg_check_arg,
00044 const char *field_name_arg,
00045 drizzled::TableShare *share) :
00046 Epoch(ptr_arg,
00047 null_ptr_arg,
00048 null_bit_arg,
00049 unireg_check_arg,
00050 field_name_arg,
00051 share)
00052 {
00053 }
00054
00055 Microtime::Microtime(bool maybe_null_arg,
00056 const char *field_name_arg) :
00057 Epoch(maybe_null_arg,
00058 field_name_arg)
00059 {
00060 }
00061
00062 int Microtime::store(const char *from,
00063 uint32_t len,
00064 const charset_info_st * const )
00065 {
00066 MicroTimestamp temporal;
00067
00068 ASSERT_COLUMN_MARKED_FOR_WRITE;
00069
00070 if (not temporal.from_string(from, (size_t) len))
00071 {
00072 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
00073 return 1;
00074 }
00075
00076 struct timeval tmp;
00077 temporal.to_timeval(tmp);
00078
00079 uint64_t tmp_seconds= tmp.tv_sec;
00080 uint32_t tmp_micro= tmp.tv_usec;
00081
00082 pack_num(tmp_seconds);
00083 pack_num(tmp_micro, ptr +8);
00084
00085 return 0;
00086 }
00087
00088 int Microtime::store_time(type::Time <ime, type::timestamp_t)
00089 {
00090 long my_timezone;
00091
00092 type::epoch_t time_tmp;
00093 ltime.convert(time_tmp, &my_timezone);
00094 uint64_t tmp_seconds= time_tmp;
00095 uint32_t tmp_micro= ltime.second_part;
00096
00097 pack_num(tmp_seconds);
00098 pack_num(tmp_micro, ptr +8);
00099
00100 return 0;
00101 }
00102
00103 int Microtime::store(double from)
00104 {
00105 ASSERT_COLUMN_MARKED_FOR_WRITE;
00106
00107 uint64_t from_tmp= (uint64_t)from;
00108 type::usec_t fractional_seconds= (type::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS;
00109
00110 MicroTimestamp temporal;
00111 if (not temporal.from_int64_t(from_tmp))
00112 {
00113
00114 std::string tmp(boost::lexical_cast<std::string>(from));
00115
00116 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
00117 return 2;
00118 }
00119
00120 time_t tmp;
00121 temporal.to_time_t(tmp);
00122
00123 uint64_t tmp_micro= tmp;
00124 pack_num(tmp_micro);
00125 pack_num(fractional_seconds, ptr +8);
00126
00127 return 0;
00128 }
00129
00130 int Microtime::store(int64_t from, bool)
00131 {
00132 ASSERT_COLUMN_MARKED_FOR_WRITE;
00133
00134 MicroTimestamp temporal;
00135 if (not temporal.from_int64_t(from))
00136 {
00137
00138 std::string tmp(boost::lexical_cast<std::string>(from));
00139
00140 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
00141 return 2;
00142 }
00143
00144 time_t tmp;
00145 temporal.to_time_t(tmp);
00146
00147 uint64_t tmp_micro= tmp;
00148 pack_num(tmp_micro);
00149 pack_num(static_cast<uint32_t>(0), ptr +8);
00150
00151 return 0;
00152 }
00153
00154 double Microtime::val_real(void) const
00155 {
00156 uint64_t temp;
00157 type::usec_t micro_temp;
00158
00159 ASSERT_COLUMN_MARKED_FOR_READ;
00160
00161 unpack_num(temp);
00162 unpack_num(micro_temp, ptr +8);
00163
00164 Timestamp temporal;
00165 (void) temporal.from_time_t((time_t) temp);
00166
00167
00168 int64_t result;
00169 temporal.to_int64_t(&result);
00170
00171 result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
00172
00173 return result;
00174 }
00175
00176 type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value) const
00177 {
00178 type::Time ltime;
00179
00180 get_date(ltime, 0);
00181
00182 return date2_class_decimal(<ime, decimal_value);
00183 }
00184
00185 int64_t Microtime::val_int(void) const
00186 {
00187 uint64_t temp;
00188
00189 ASSERT_COLUMN_MARKED_FOR_READ;
00190
00191 unpack_num(temp);
00192
00193 Timestamp temporal;
00194 (void) temporal.from_time_t((time_t) temp);
00195
00196
00197 int64_t result;
00198 temporal.to_int64_t(&result);
00199
00200 return result;
00201 }
00202
00203 String *Microtime::val_str(String *val_buffer, String *) const
00204 {
00205 uint64_t temp= 0;
00206 type::usec_t micro_temp= 0;
00207
00208 unpack_num(temp);
00209 unpack_num(micro_temp, ptr +8);
00210
00211 type::Time tmp_time;
00212 tmp_time.store(temp, micro_temp);
00213
00214 tmp_time.convert(*val_buffer);
00215
00216
00217 return val_buffer;
00218 }
00219
00220 bool Microtime::get_date(type::Time <ime, uint32_t) const
00221 {
00222 uint64_t temp;
00223 uint32_t micro_temp= 0;
00224
00225 unpack_num(temp);
00226 unpack_num(micro_temp, ptr +8);
00227
00228 ltime.reset();
00229
00230 ltime.store(temp, micro_temp);
00231
00232 return false;
00233 }
00234
00235 bool Microtime::get_time(type::Time <ime) const
00236 {
00237 return Microtime::get_date(ltime, 0);
00238 }
00239
00240 int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
00241 {
00242 uint64_t a,b;
00243 uint32_t a_micro, b_micro;
00244
00245 unpack_num(a, a_ptr);
00246 unpack_num(a_micro, a_ptr +8);
00247
00248 unpack_num(b, b_ptr);
00249 unpack_num(b_micro, b_ptr +8);
00250
00251 if (a == b)
00252 return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;
00253
00254 return (a < b) ? -1 : (a > b) ? 1 : 0;
00255 }
00256
00257
00258 void Microtime::sort_string(unsigned char *to,uint32_t )
00259 {
00260 #ifdef WORDS_BIGENDIAN
00261 if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
00262 {
00263 std::reverse_copy(to, to+pack_length(), ptr);
00264 std::reverse_copy(to +8, to+pack_length(), ptr +8);
00265 }
00266 else
00267 #endif
00268 {
00269 memcpy(to, ptr, pack_length());
00270 }
00271 }
00272
00273 void Microtime::set_time()
00274 {
00275 Session *session= getTable() ? getTable()->in_use : current_session;
00276
00277 type::usec_t fractional_seconds= 0;
00278 uint64_t epoch_seconds= session->times.getCurrentTimestampEpoch(fractional_seconds);
00279
00280 set_notnull();
00281 pack_num(epoch_seconds);
00282 pack_num(fractional_seconds, ptr +8);
00283 }
00284
00285 long Microtime::get_timestamp(bool *null_value) const
00286 {
00287 if ((*null_value= is_null()))
00288 return 0;
00289
00290 uint64_t tmp;
00291 return unpack_num(tmp);
00292 }
00293
00294 }
00295 }