~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/microtime.cc

  • Committer: Brian Aker
  • Date: 2011-01-13 00:08:59 UTC
  • mto: (2082.4.1 timestamp)
  • mto: This revision was merged to the branch mainline in revision 2098.
  • Revision ID: brian@tangent.org-20110113000859-8dw4ybnp8id56u50
Fix issue with return value from unix_timestamp(). Also clean up error
messages, and remove limit on value for timestamp.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
 
75
75
  if (not temporal.from_string(from, (size_t) len))
76
76
  {
77
 
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
 
77
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
78
78
    return 1;
79
79
  }
80
80
 
109
109
{
110
110
  ASSERT_COLUMN_MARKED_FOR_WRITE;
111
111
 
112
 
  if (from < 0 || from > 99991231235959.0)
 
112
  uint64_t from_tmp= (uint64_t)from;
 
113
  type::Time::usec_t fractional_seconds= (type::Time::usec_t)((from - from_tmp) * type::Time::FRACTIONAL_DIGITS);
 
114
 
 
115
  MicroTimestamp temporal;
 
116
  if (not temporal.from_int64_t(from_tmp))
113
117
  {
114
 
    /* Convert the double to a string using stringstream */
115
 
    std::stringstream ss;
116
 
    std::string tmp;
117
 
    ss.precision(18); /* 18 places should be fine for error display of double input. */
118
 
    ss << from; 
119
 
    ss >> tmp;
 
118
    /* Convert the integer to a string using boost::lexical_cast */
 
119
    std::string tmp(boost::lexical_cast<std::string>(from));
120
120
 
121
 
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
121
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
122
122
    return 2;
123
123
  }
124
 
  return Microtime::store((int64_t) rint(from), false);
 
124
 
 
125
  time_t tmp;
 
126
  temporal.to_time_t(tmp);
 
127
 
 
128
  uint64_t tmp_micro= tmp;
 
129
  pack_num(tmp_micro);
 
130
  pack_num(fractional_seconds, ptr +8);
 
131
 
 
132
  return 0;
 
133
}
 
134
 
 
135
int Microtime::store_decimal(const type::Decimal *value)
 
136
{
 
137
  char buff[DECIMAL_MAX_STR_LENGTH+1];
 
138
 
 
139
  String str(buff, sizeof(buff), &my_charset_bin);
 
140
  class_decimal2string(E_DEC_FATAL_ERROR, value, 0, 0, 0, &str);
 
141
 
 
142
  return store(str.ptr(), str.length(), str.charset());
125
143
}
126
144
 
127
145
int Microtime::store(int64_t from, bool)
134
152
    /* Convert the integer to a string using boost::lexical_cast */
135
153
    std::string tmp(boost::lexical_cast<std::string>(from));
136
154
 
137
 
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
155
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
138
156
    return 2;
139
157
  }
140
158
 
150
168
 
151
169
double Microtime::val_real(void)
152
170
{
153
 
  return (double) Microtime::val_int();
 
171
  uint64_t temp;
 
172
  type::Time::usec_t micro_temp;
 
173
 
 
174
  ASSERT_COLUMN_MARKED_FOR_READ;
 
175
 
 
176
  unpack_num(temp);
 
177
  unpack_num(micro_temp, ptr +8);
 
178
 
 
179
  Timestamp temporal;
 
180
  (void) temporal.from_time_t((time_t) temp);
 
181
 
 
182
  /* We must convert into a "timestamp-formatted integer" ... */
 
183
  int64_t result;
 
184
  temporal.to_int64_t(&result);
 
185
 
 
186
  result+= micro_temp % type::Time::FRACTIONAL_DIGITS;
 
187
 
 
188
  return result;
 
189
}
 
190
 
 
191
type::Decimal *Microtime::val_decimal(type::Decimal *decimal_value)
 
192
{
 
193
  type::Time ltime;
 
194
 
 
195
  get_date(&ltime, 0);
 
196
 
 
197
  return date2_class_decimal(&ltime, decimal_value);
154
198
}
155
199
 
156
200
int64_t Microtime::val_int(void)
174
218
String *Microtime::val_str(String *val_buffer, String *)
175
219
{
176
220
  uint64_t temp= 0;
177
 
  uint32_t micro_temp= 0;
 
221
  type::Time::usec_t micro_temp= 0;
178
222
  char *to;
179
223
  int to_len= field_length + 1 + 8;
180
224
 
209
253
  unpack_num(temp);
210
254
  unpack_num(micro_temp, ptr +8);
211
255
  
212
 
  memset(ltime, 0, sizeof(*ltime));
 
256
  ltime->reset();
213
257
 
214
258
  Timestamp temporal;
215
259
  (void) temporal.from_time_t((time_t) temp);
225
269
  ltime->second= temporal.seconds();
226
270
  ltime->second_part= temporal.useconds();
227
271
 
228
 
  return 0;
 
272
  return false;
229
273
}
230
274
 
231
275
bool Microtime::get_time(type::Time *ltime)
232
276
{
233
 
  return Microtime::get_date(ltime,0);
 
277
  return Microtime::get_date(ltime, 0);
234
278
}
235
279
 
236
280
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
275
319
{
276
320
  Session *session= getTable() ? getTable()->in_use : current_session;
277
321
 
278
 
  uint32_t fractional_seconds= 0;
 
322
  type::Time::usec_t fractional_seconds= 0;
279
323
  uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);
280
324
 
281
325
  set_notnull();