~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/microtime.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 */
 
20
 
 
21
#include "config.h"
 
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>
 
28
 
 
29
#include <math.h>
 
30
 
 
31
#include <sstream>
 
32
 
 
33
#include <boost/date_time/posix_time/posix_time.hpp>
 
34
 
 
35
#include "drizzled/temporal.h"
 
36
 
 
37
namespace drizzled
 
38
{
 
39
 
 
40
namespace field
 
41
{
 
42
 
 
43
static boost::posix_time::ptime _epoch(boost::gregorian::date(1970, 1, 1));
 
44
 
 
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) :
 
51
  Epoch(ptr_arg,
 
52
        null_ptr_arg,
 
53
        null_bit_arg,
 
54
        unireg_check_arg,
 
55
        field_name_arg,
 
56
        share)
 
57
{
 
58
}
 
59
 
 
60
Microtime::Microtime(bool maybe_null_arg,
 
61
                     const char *field_name_arg) :
 
62
  Epoch(maybe_null_arg,
 
63
        field_name_arg)
 
64
{
 
65
}
 
66
 
 
67
int Microtime::store(const char *from,
 
68
                     uint32_t len,
 
69
                     const CHARSET_INFO * const )
 
70
{
 
71
  MicroTimestamp temporal;
 
72
 
 
73
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
74
 
 
75
  if (not temporal.from_string(from, (size_t) len))
 
76
  {
 
77
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
 
78
    return 1;
 
79
  }
 
80
 
 
81
  struct timeval tmp;
 
82
  temporal.to_timeval(tmp);
 
83
 
 
84
  uint64_t tmp_seconds= tmp.tv_sec;
 
85
  uint32_t tmp_micro= tmp.tv_usec;
 
86
 
 
87
  pack_num(tmp_seconds);
 
88
  pack_num(tmp_micro, ptr +8);
 
89
 
 
90
  return 0;
 
91
}
 
92
 
 
93
int Microtime::store_time(type::Time &ltime, type::timestamp_t )
 
94
{
 
95
  long my_timezone;
 
96
  bool in_dst_time_gap;
 
97
 
 
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;
 
102
 
 
103
  pack_num(tmp_seconds);
 
104
  pack_num(tmp_micro, ptr +8);
 
105
 
 
106
  return 0;
 
107
}
 
108
 
 
109
int Microtime::store(double from)
 
110
{
 
111
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
112
 
 
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;
 
115
 
 
116
  MicroTimestamp temporal;
 
117
  if (not temporal.from_int64_t(from_tmp))
 
118
  {
 
119
    /* Convert the integer to a string using boost::lexical_cast */
 
120
    std::string tmp(boost::lexical_cast<std::string>(from));
 
121
 
 
122
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
123
    return 2;
 
124
  }
 
125
 
 
126
  time_t tmp;
 
127
  temporal.to_time_t(tmp);
 
128
 
 
129
  uint64_t tmp_micro= tmp;
 
130
  pack_num(tmp_micro);
 
131
  pack_num(fractional_seconds, ptr +8);
 
132
 
 
133
  return 0;
 
134
}
 
135
 
 
136
int Microtime::store(int64_t from, bool)
 
137
{
 
138
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
139
 
 
140
  MicroTimestamp temporal;
 
141
  if (not temporal.from_int64_t(from))
 
142
  {
 
143
    /* Convert the integer to a string using boost::lexical_cast */
 
144
    std::string tmp(boost::lexical_cast<std::string>(from));
 
145
 
 
146
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
147
    return 2;
 
148
  }
 
149
 
 
150
  time_t tmp;
 
151
  temporal.to_time_t(tmp);
 
152
 
 
153
  uint64_t tmp_micro= tmp;
 
154
  pack_num(tmp_micro);
 
155
  pack_num(static_cast<uint32_t>(0), ptr +8);
 
156
 
 
157
  return 0;
 
158
}
 
159
 
 
160
double Microtime::val_real(void)
 
161
{
 
162
  uint64_t temp;
 
163
  type::Time::usec_t micro_temp;
 
164
 
 
165
  ASSERT_COLUMN_MARKED_FOR_READ;
 
166
 
 
167
  unpack_num(temp);
 
168
  unpack_num(micro_temp, ptr +8);
 
169
 
 
170
  Timestamp temporal;
 
171
  (void) temporal.from_time_t((time_t) temp);
 
172
 
 
173
  /* We must convert into a "timestamp-formatted integer" ... */
 
174
  int64_t result;
 
175
  temporal.to_int64_t(&result);
 
176
 
 
177
  result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
 
178
 
 
179
  return result;
 
180
}
 
181
 
 
182
type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value)
 
183
{
 
184
  type::Time ltime;
 
185
 
 
186
  get_date(ltime, 0);
 
187
 
 
188
  return date2_class_decimal(&ltime, decimal_value);
 
189
}
 
190
 
 
191
int64_t Microtime::val_int(void)
 
192
{
 
193
  uint64_t temp;
 
194
 
 
195
  ASSERT_COLUMN_MARKED_FOR_READ;
 
196
 
 
197
  unpack_num(temp);
 
198
 
 
199
  Timestamp temporal;
 
200
  (void) temporal.from_time_t((time_t) temp);
 
201
 
 
202
  /* We must convert into a "timestamp-formatted integer" ... */
 
203
  int64_t result;
 
204
  temporal.to_int64_t(&result);
 
205
 
 
206
  return result;
 
207
}
 
208
 
 
209
String *Microtime::val_str(String *val_buffer, String *)
 
210
{
 
211
  uint64_t temp= 0;
 
212
  type::Time::usec_t micro_temp= 0;
 
213
 
 
214
  unpack_num(temp);
 
215
  unpack_num(micro_temp, ptr +8);
 
216
 
 
217
  type::Time tmp_time;
 
218
  tmp_time.store(temp, micro_temp);
 
219
 
 
220
  tmp_time.convert(*val_buffer);
 
221
 
 
222
 
 
223
  return val_buffer;
 
224
}
 
225
 
 
226
bool Microtime::get_date(type::Time &ltime, uint32_t)
 
227
{
 
228
  uint64_t temp;
 
229
  uint32_t micro_temp= 0;
 
230
 
 
231
  unpack_num(temp);
 
232
  unpack_num(micro_temp, ptr +8);
 
233
  
 
234
  ltime.reset();
 
235
 
 
236
  ltime.store(temp, micro_temp);
 
237
 
 
238
  return false;
 
239
}
 
240
 
 
241
bool Microtime::get_time(type::Time &ltime)
 
242
{
 
243
  return Microtime::get_date(ltime, 0);
 
244
}
 
245
 
 
246
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
247
{
 
248
  uint64_t a,b;
 
249
  uint32_t a_micro, b_micro;
 
250
 
 
251
  unpack_num(a, a_ptr);
 
252
  unpack_num(a_micro, a_ptr +8);
 
253
 
 
254
  unpack_num(b, b_ptr);
 
255
  unpack_num(b_micro, b_ptr +8);
 
256
 
 
257
  if (a == b)
 
258
    return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;
 
259
 
 
260
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
261
}
 
262
 
 
263
 
 
264
void Microtime::sort_string(unsigned char *to,uint32_t )
 
265
{
 
266
#ifdef WORDS_BIGENDIAN
 
267
  if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
 
268
  {
 
269
    std::reverse_copy(to, to+pack_length(), ptr);
 
270
    std::reverse_copy(to +8, to+pack_length(), ptr +8);
 
271
  }
 
272
  else
 
273
#endif
 
274
  {
 
275
    memcpy(to, ptr, pack_length());
 
276
  }
 
277
}
 
278
 
 
279
void Microtime::sql_type(String &res) const
 
280
{
 
281
  res.set_ascii(STRING_WITH_LEN("microsecond timestamp"));
 
282
}
 
283
 
 
284
void Microtime::set_time()
 
285
{
 
286
  Session *session= getTable() ? getTable()->in_use : current_session;
 
287
 
 
288
  type::Time::usec_t fractional_seconds= 0;
 
289
  uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);
 
290
 
 
291
  set_notnull();
 
292
  pack_num(epoch_seconds);
 
293
  pack_num(fractional_seconds, ptr +8);
 
294
}
 
295
 
 
296
long Microtime::get_timestamp(bool *null_value)
 
297
{
 
298
  if ((*null_value= is_null()))
 
299
    return 0;
 
300
 
 
301
  uint64_t tmp;
 
302
  return unpack_num(tmp);
 
303
}
 
304
 
 
305
} /* namespace field */
 
306
} /* namespace drizzled */