1
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <boost/lexical_cast.hpp>
23
#include <drizzled/field/microtime.h>
24
#include <drizzled/error.h>
25
#include <drizzled/tztime.h>
26
#include <drizzled/table.h>
27
#include <drizzled/session.h>
33
#include <boost/date_time/posix_time/posix_time.hpp>
35
#include "drizzled/temporal.h"
43
static boost::posix_time::ptime _epoch(boost::gregorian::date(1970, 1, 1));
45
Microtime::Microtime(unsigned char *ptr_arg,
46
unsigned char *null_ptr_arg,
47
unsigned char null_bit_arg,
48
enum utype unireg_check_arg,
49
const char *field_name_arg,
50
drizzled::TableShare *share) :
60
Microtime::Microtime(bool maybe_null_arg,
61
const char *field_name_arg) :
67
int Microtime::store(const char *from,
69
const CHARSET_INFO * const )
71
MicroTimestamp temporal;
73
ASSERT_COLUMN_MARKED_FOR_WRITE;
75
if (not temporal.from_string(from, (size_t) len))
77
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
82
temporal.to_timeval(tmp);
84
uint64_t tmp_seconds= tmp.tv_sec;
85
uint32_t tmp_micro= tmp.tv_usec;
87
pack_num(tmp_seconds);
88
pack_num(tmp_micro, ptr +8);
93
int Microtime::store_time(type::Time <ime, type::timestamp_t )
98
type::Time::epoch_t time_tmp;
99
ltime.convert(time_tmp, &my_timezone, &in_dst_time_gap, true);
100
uint64_t tmp_seconds= time_tmp;
101
uint32_t tmp_micro= ltime.second_part;
103
pack_num(tmp_seconds);
104
pack_num(tmp_micro, ptr +8);
109
int Microtime::store(double from)
111
ASSERT_COLUMN_MARKED_FOR_WRITE;
113
uint64_t from_tmp= (uint64_t)from;
114
type::Time::usec_t fractional_seconds= (type::Time::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS;
116
MicroTimestamp temporal;
117
if (not temporal.from_int64_t(from_tmp))
119
/* Convert the integer to a string using boost::lexical_cast */
120
std::string tmp(boost::lexical_cast<std::string>(from));
122
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
127
temporal.to_time_t(tmp);
129
uint64_t tmp_micro= tmp;
131
pack_num(fractional_seconds, ptr +8);
136
int Microtime::store(int64_t from, bool)
138
ASSERT_COLUMN_MARKED_FOR_WRITE;
140
MicroTimestamp temporal;
141
if (not temporal.from_int64_t(from))
143
/* Convert the integer to a string using boost::lexical_cast */
144
std::string tmp(boost::lexical_cast<std::string>(from));
146
my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
151
temporal.to_time_t(tmp);
153
uint64_t tmp_micro= tmp;
155
pack_num(static_cast<uint32_t>(0), ptr +8);
160
double Microtime::val_real(void)
163
type::Time::usec_t micro_temp;
165
ASSERT_COLUMN_MARKED_FOR_READ;
168
unpack_num(micro_temp, ptr +8);
171
(void) temporal.from_time_t((time_t) temp);
173
/* We must convert into a "timestamp-formatted integer" ... */
175
temporal.to_int64_t(&result);
177
result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
182
type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value)
188
return date2_class_decimal(<ime, decimal_value);
191
int64_t Microtime::val_int(void)
195
ASSERT_COLUMN_MARKED_FOR_READ;
200
(void) temporal.from_time_t((time_t) temp);
202
/* We must convert into a "timestamp-formatted integer" ... */
204
temporal.to_int64_t(&result);
209
String *Microtime::val_str(String *val_buffer, String *)
212
type::Time::usec_t micro_temp= 0;
215
unpack_num(micro_temp, ptr +8);
218
tmp_time.store(temp, micro_temp);
220
tmp_time.convert(*val_buffer);
226
bool Microtime::get_date(type::Time <ime, uint32_t)
229
uint32_t micro_temp= 0;
232
unpack_num(micro_temp, ptr +8);
236
ltime.store(temp, micro_temp);
241
bool Microtime::get_time(type::Time <ime)
243
return Microtime::get_date(ltime, 0);
246
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
249
uint32_t a_micro, b_micro;
251
unpack_num(a, a_ptr);
252
unpack_num(a_micro, a_ptr +8);
254
unpack_num(b, b_ptr);
255
unpack_num(b_micro, b_ptr +8);
258
return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;
260
return (a < b) ? -1 : (a > b) ? 1 : 0;
264
void Microtime::sort_string(unsigned char *to,uint32_t )
266
#ifdef WORDS_BIGENDIAN
267
if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
269
std::reverse_copy(to, to+pack_length(), ptr);
270
std::reverse_copy(to +8, to+pack_length(), ptr +8);
275
memcpy(to, ptr, pack_length());
279
void Microtime::sql_type(String &res) const
281
res.set_ascii(STRING_WITH_LEN("microsecond timestamp"));
284
void Microtime::set_time()
286
Session *session= getTable() ? getTable()->in_use : current_session;
288
type::Time::usec_t fractional_seconds= 0;
289
uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);
292
pack_num(epoch_seconds);
293
pack_num(fractional_seconds, ptr +8);
296
long Microtime::get_timestamp(bool *null_value)
298
if ((*null_value= is_null()))
302
return unpack_num(tmp);
305
} /* namespace field */
306
} /* namespace drizzled */