~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/microtime.cc

  • Committer: Brian Aker
  • Date: 2011-01-08 10:35:13 UTC
  • mfrom: (2057.2.9 timestamp)
  • Revision ID: brian@tangent.org-20110108103513-3wuo8tsyajjcxjrg
Merge in fractional seconds to timestamp.

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_UNIX_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(double from)
 
94
{
 
95
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
96
 
 
97
  if (from < 0 || from > 99991231235959.0)
 
98
  {
 
99
    /* Convert the double to a string using stringstream */
 
100
    std::stringstream ss;
 
101
    std::string tmp;
 
102
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
103
    ss << from; 
 
104
    ss >> tmp;
 
105
 
 
106
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
107
    return 2;
 
108
  }
 
109
  return Microtime::store((int64_t) rint(from), false);
 
110
}
 
111
 
 
112
int Microtime::store(int64_t from, bool)
 
113
{
 
114
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
115
 
 
116
  MicroTimestamp temporal;
 
117
  if (not temporal.from_int64_t(from))
 
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_UNIX_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(static_cast<uint32_t>(0), ptr +8);
 
132
 
 
133
  return 0;
 
134
}
 
135
 
 
136
double Microtime::val_real(void)
 
137
{
 
138
  return (double) Microtime::val_int();
 
139
}
 
140
 
 
141
int64_t Microtime::val_int(void)
 
142
{
 
143
  uint64_t temp;
 
144
 
 
145
  ASSERT_COLUMN_MARKED_FOR_READ;
 
146
 
 
147
  unpack_num(temp);
 
148
 
 
149
  Timestamp temporal;
 
150
  (void) temporal.from_time_t((time_t) temp);
 
151
 
 
152
  /* We must convert into a "timestamp-formatted integer" ... */
 
153
  int64_t result;
 
154
  temporal.to_int64_t(&result);
 
155
 
 
156
  return result;
 
157
}
 
158
 
 
159
String *Microtime::val_str(String *val_buffer, String *)
 
160
{
 
161
  uint64_t temp= 0;
 
162
  uint32_t micro_temp= 0;
 
163
  char *to;
 
164
  int to_len= field_length + 1 + 8;
 
165
 
 
166
  val_buffer->alloc(to_len);
 
167
  to= (char *) val_buffer->ptr();
 
168
 
 
169
  unpack_num(temp);
 
170
  unpack_num(micro_temp, ptr +8);
 
171
 
 
172
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
 
173
 
 
174
  struct timeval buffer;
 
175
  buffer.tv_sec= temp;
 
176
  buffer.tv_usec= micro_temp;
 
177
 
 
178
  MicroTimestamp temporal;
 
179
  (void) temporal.from_timeval(buffer);
 
180
 
 
181
  int rlen= temporal.to_string(to, to_len);
 
182
  assert(rlen <= to_len);
 
183
 
 
184
  val_buffer->length(rlen);
 
185
 
 
186
  return val_buffer;
 
187
}
 
188
 
 
189
bool Microtime::get_date(type::Time *ltime, uint32_t)
 
190
{
 
191
  uint64_t temp;
 
192
  uint32_t micro_temp= 0;
 
193
 
 
194
  unpack_num(temp);
 
195
  unpack_num(micro_temp, ptr +8);
 
196
  
 
197
  memset(ltime, 0, sizeof(*ltime));
 
198
 
 
199
  Timestamp temporal;
 
200
  (void) temporal.from_time_t((time_t) temp);
 
201
 
 
202
  /* @TODO Goodbye the below code when type::Time is finally gone.. */
 
203
 
 
204
  ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
 
205
  ltime->year= temporal.years();
 
206
  ltime->month= temporal.months();
 
207
  ltime->day= temporal.days();
 
208
  ltime->hour= temporal.hours();
 
209
  ltime->minute= temporal.minutes();
 
210
  ltime->second= temporal.seconds();
 
211
  ltime->second_part= temporal.useconds();
 
212
 
 
213
  return 0;
 
214
}
 
215
 
 
216
bool Microtime::get_time(type::Time *ltime)
 
217
{
 
218
  return Microtime::get_date(ltime,0);
 
219
}
 
220
 
 
221
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
222
{
 
223
  uint64_t a,b;
 
224
  uint32_t a_micro, b_micro;
 
225
 
 
226
  unpack_num(a, a_ptr);
 
227
  unpack_num(a_micro, a_ptr +8);
 
228
 
 
229
  unpack_num(b, b_ptr);
 
230
  unpack_num(b_micro, b_ptr +8);
 
231
 
 
232
  if (a == b)
 
233
    return (a_micro < b_micro) ? -1 : (a_micro > b_micro) ? 1 : 0;
 
234
 
 
235
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
236
}
 
237
 
 
238
 
 
239
void Microtime::sort_string(unsigned char *to,uint32_t )
 
240
{
 
241
#ifdef WORDS_BIGENDIAN
 
242
  if ((not getTable()) or (not getTable()->getShare()->db_low_byte_first))
 
243
  {
 
244
    std::reverse_copy(to, to+pack_length(), ptr);
 
245
    std::reverse_copy(to +8, to+pack_length(), ptr +8);
 
246
  }
 
247
  else
 
248
#endif
 
249
  {
 
250
    memcpy(to, ptr, pack_length());
 
251
  }
 
252
}
 
253
 
 
254
void Microtime::sql_type(String &res) const
 
255
{
 
256
  res.set_ascii(STRING_WITH_LEN("microsecond timestamp"));
 
257
}
 
258
 
 
259
void Microtime::set_time()
 
260
{
 
261
  Session *session= getTable() ? getTable()->in_use : current_session;
 
262
 
 
263
  uint32_t fractional_seconds= 0;
 
264
  uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);
 
265
 
 
266
  set_notnull();
 
267
  pack_num(epoch_seconds);
 
268
  pack_num(fractional_seconds, ptr +8);
 
269
}
 
270
 
 
271
long Microtime::get_timestamp(bool *null_value)
 
272
{
 
273
  if ((*null_value= is_null()))
 
274
    return 0;
 
275
 
 
276
  uint64_t tmp;
 
277
  return unpack_num(tmp);
 
278
}
 
279
 
 
280
} /* namespace field */
 
281
} /* namespace drizzled */