22 #include <boost/lexical_cast.hpp>
23 #include <drizzled/field/microtime.h>
24 #include <drizzled/error.h>
25 #include <drizzled/table.h>
26 #include <drizzled/session.h>
27 #include <drizzled/session/times.h>
28 #include <drizzled/current_session.h>
32 #include <boost/date_time/posix_time/posix_time.hpp>
38 static boost::posix_time::ptime _epoch(boost::gregorian::date(1970, 1, 1));
40 Microtime::Microtime(
unsigned char *ptr_arg,
41 unsigned char *null_ptr_arg,
42 unsigned char null_bit_arg,
43 enum utype unireg_check_arg,
44 const char *field_name_arg,
55 Microtime::Microtime(
bool maybe_null_arg,
56 const char *field_name_arg) :
62 int Microtime::store(
const char *from,
64 const charset_info_st *
const )
66 MicroTimestamp temporal;
68 ASSERT_COLUMN_MARKED_FOR_WRITE;
70 if (not temporal.from_string(from, (
size_t) len))
72 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
77 temporal.to_timeval(tmp);
79 uint64_t tmp_seconds= tmp.tv_sec;
80 uint32_t tmp_micro= tmp.tv_usec;
82 pack_num(tmp_seconds);
83 pack_num(tmp_micro, ptr +8);
88 int Microtime::store_time(
type::Time <ime, type::timestamp_t)
92 type::epoch_t time_tmp;
93 ltime.convert(time_tmp, &my_timezone);
94 uint64_t tmp_seconds= time_tmp;
95 uint32_t tmp_micro= ltime.second_part;
97 pack_num(tmp_seconds);
98 pack_num(tmp_micro, ptr +8);
103 int Microtime::store(
double from)
105 ASSERT_COLUMN_MARKED_FOR_WRITE;
107 uint64_t from_tmp= (uint64_t)from;
108 type::usec_t fractional_seconds= (type::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS;
114 std::string tmp(boost::lexical_cast<std::string>(from));
116 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
123 uint64_t tmp_micro= tmp;
125 pack_num(fractional_seconds, ptr +8);
130 int Microtime::store(int64_t from,
bool)
132 ASSERT_COLUMN_MARKED_FOR_WRITE;
134 MicroTimestamp temporal;
135 if (not temporal.from_int64_t(from))
138 std::string tmp(boost::lexical_cast<std::string>(from));
140 my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
145 temporal.to_time_t(tmp);
147 uint64_t tmp_micro= tmp;
149 pack_num(static_cast<uint32_t>(0), ptr +8);
154 double Microtime::val_real(
void)
const
157 type::usec_t micro_temp;
159 ASSERT_COLUMN_MARKED_FOR_READ;
162 unpack_num(micro_temp, ptr +8);
165 (void) temporal.from_time_t((time_t) temp);
169 temporal.to_int64_t(&result);
171 result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
176 type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value)
const
182 return date2_class_decimal(<ime, decimal_value);
185 int64_t Microtime::val_int(
void)
const
189 ASSERT_COLUMN_MARKED_FOR_READ;
194 (void) temporal.from_time_t((time_t) temp);
198 temporal.to_int64_t(&result);
203 String *Microtime::val_str(String *val_buffer, String *)
const
206 type::usec_t micro_temp= 0;
209 unpack_num(micro_temp, ptr +8);
212 tmp_time.store(temp, micro_temp);
214 tmp_time.convert(*val_buffer);
220 bool Microtime::get_date(type::Time <ime, uint32_t)
const
223 uint32_t micro_temp= 0;
226 unpack_num(micro_temp, ptr +8);
230 ltime.store(temp, micro_temp);
235 bool Microtime::get_time(type::Time <ime)
const
237 return Microtime::get_date(ltime, 0);
240 int Microtime::cmp(
const unsigned char *a_ptr,
const unsigned char *b_ptr)
243 uint32_t a_micro, b_micro;
245 unpack_num(a, a_ptr);
246 unpack_num(a_micro, a_ptr +8);
248 unpack_num(b, b_ptr);
249 unpack_num(b_micro, b_ptr +8);
252 return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;
254 return (a < b) ? -1 : (a > b) ? 1 : 0;
258 void Microtime::sort_string(
unsigned char *to,uint32_t )
260 #ifdef WORDS_BIGENDIAN
261 if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
263 std::reverse_copy(to, to+pack_length(), ptr);
264 std::reverse_copy(to +8, to+pack_length(), ptr +8);
269 memcpy(to, ptr, pack_length());
273 void Microtime::set_time()
275 Session *session= getTable() ? getTable()->in_use : current_session;
277 type::usec_t fractional_seconds= 0;
278 uint64_t epoch_seconds= session->times.getCurrentTimestampEpoch(fractional_seconds);
281 pack_num(epoch_seconds);
282 pack_num(fractional_seconds, ptr +8);
285 long Microtime::get_timestamp(
bool *null_value)
const
287 if ((*null_value= is_null()))
291 return unpack_num(tmp);
TODO: Rename this file - func.h is stupid.
bool from_int64_t(const int64_t from, bool convert)
void to_time_t(time_t &to) const