18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
22
#include <boost/lexical_cast.hpp>
23
23
#include <drizzled/field/microtime.h>
24
24
#include <drizzled/error.h>
25
25
#include <drizzled/tztime.h>
26
26
#include <drizzled/table.h>
27
27
#include <drizzled/session.h>
28
#include <drizzled/current_session.h>
76
75
if (not temporal.from_string(from, (size_t) len))
78
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
77
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
94
int Microtime::store_time(type::Time <ime, type::timestamp_t )
93
int Microtime::store_time(type::Time *ltime, enum enum_drizzle_timestamp_type )
97
96
bool in_dst_time_gap;
99
type::Time::epoch_t time_tmp;
100
ltime.convert(time_tmp, &my_timezone, &in_dst_time_gap, true);
98
time_t time_tmp= my_system_gmt_sec(ltime, &my_timezone, &in_dst_time_gap, true);
101
99
uint64_t tmp_seconds= time_tmp;
102
uint32_t tmp_micro= ltime.second_part;
100
uint32_t tmp_micro= ltime->second_part;
104
102
pack_num(tmp_seconds);
105
103
pack_num(tmp_micro, ptr +8);
112
110
ASSERT_COLUMN_MARKED_FOR_WRITE;
114
uint64_t from_tmp= (uint64_t)from;
115
type::Time::usec_t fractional_seconds= (type::Time::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS;
117
MicroTimestamp temporal;
118
if (not temporal.from_int64_t(from_tmp))
112
if (from < 0 || from > 99991231235959.0)
120
/* Convert the integer to a string using boost::lexical_cast */
121
std::string tmp(boost::lexical_cast<std::string>(from));
114
/* Convert the double to a string using stringstream */
115
std::stringstream ss;
117
ss.precision(18); /* 18 places should be fine for error display of double input. */
123
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
121
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
128
temporal.to_time_t(tmp);
130
uint64_t tmp_micro= tmp;
132
pack_num(fractional_seconds, ptr +8);
124
return Microtime::store((int64_t) rint(from), false);
137
127
int Microtime::store(int64_t from, bool)
144
134
/* Convert the integer to a string using boost::lexical_cast */
145
135
std::string tmp(boost::lexical_cast<std::string>(from));
147
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
137
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
161
double Microtime::val_real(void) const
164
type::Time::usec_t micro_temp;
166
ASSERT_COLUMN_MARKED_FOR_READ;
169
unpack_num(micro_temp, ptr +8);
172
(void) temporal.from_time_t((time_t) temp);
174
/* We must convert into a "timestamp-formatted integer" ... */
176
temporal.to_int64_t(&result);
178
result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
183
type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value) const
189
return date2_class_decimal(<ime, decimal_value);
192
int64_t Microtime::val_int(void) const
196
ASSERT_COLUMN_MARKED_FOR_READ;
201
(void) temporal.from_time_t((time_t) temp);
203
/* We must convert into a "timestamp-formatted integer" ... */
205
temporal.to_int64_t(&result);
210
String *Microtime::val_str(String *val_buffer, String *) const
151
double Microtime::val_real(void)
153
return (double) Microtime::val_int();
156
int64_t Microtime::val_int(void)
160
ASSERT_COLUMN_MARKED_FOR_READ;
165
(void) temporal.from_time_t((time_t) temp);
167
/* We must convert into a "timestamp-formatted integer" ... */
169
temporal.to_int64_t(&result);
174
String *Microtime::val_str(String *val_buffer, String *)
212
176
uint64_t temp= 0;
213
type::Time::usec_t micro_temp= 0;
177
uint32_t micro_temp= 0;
179
int to_len= field_length + 1 + 8;
181
val_buffer->alloc(to_len);
182
to= (char *) val_buffer->ptr();
215
184
unpack_num(temp);
216
185
unpack_num(micro_temp, ptr +8);
219
tmp_time.store(temp, micro_temp);
221
tmp_time.convert(*val_buffer);
187
val_buffer->set_charset(&my_charset_bin); /* Safety */
189
struct timeval buffer;
191
buffer.tv_usec= micro_temp;
193
MicroTimestamp temporal;
194
(void) temporal.from_timeval(buffer);
196
int rlen= temporal.to_string(to, to_len);
197
assert(rlen <= to_len);
199
val_buffer->length(rlen);
224
201
return val_buffer;
227
bool Microtime::get_date(type::Time <ime, uint32_t) const
204
bool Microtime::get_date(type::Time *ltime, uint32_t)
230
207
uint32_t micro_temp= 0;
232
209
unpack_num(temp);
233
210
unpack_num(micro_temp, ptr +8);
237
ltime.store(temp, micro_temp);
212
memset(ltime, 0, sizeof(*ltime));
215
(void) temporal.from_time_t((time_t) temp);
217
/* @TODO Goodbye the below code when type::Time is finally gone.. */
219
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
220
ltime->year= temporal.years();
221
ltime->month= temporal.months();
222
ltime->day= temporal.days();
223
ltime->hour= temporal.hours();
224
ltime->minute= temporal.minutes();
225
ltime->second= temporal.seconds();
226
ltime->second_part= temporal.useconds();
242
bool Microtime::get_time(type::Time <ime) const
231
bool Microtime::get_time(type::Time *ltime)
244
return Microtime::get_date(ltime, 0);
233
return Microtime::get_date(ltime,0);
247
236
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)