~drizzle-developers/drizzle/elliott-release

« back to all changes in this revision

Viewing changes to drizzled/field/microtime.cc

  • Committer: Patrick Crews
  • Date: 2011-02-01 20:33:06 UTC
  • mfrom: (1845.2.288 drizzle)
  • Revision ID: gleebix@gmail.com-20110201203306-mwq2rk0it81tlwxh
Merged Trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
        unireg_check_arg,
55
55
        field_name_arg,
56
56
        share)
57
 
  {
58
 
  }
 
57
{
 
58
}
59
59
 
60
60
Microtime::Microtime(bool maybe_null_arg,
61
61
                     const char *field_name_arg) :
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
 
90
90
  return 0;
91
91
}
92
92
 
93
 
int Microtime::store_time(type::Time *ltime, enum enum_drizzle_timestamp_type )
 
93
int Microtime::store_time(type::Time &ltime, type::timestamp_t )
94
94
{
95
95
  long my_timezone;
96
96
  bool in_dst_time_gap;
97
97
 
98
 
  time_t time_tmp= my_system_gmt_sec(ltime, &my_timezone, &in_dst_time_gap, true);
 
98
  type::Time::epoch_t time_tmp;
 
99
  ltime.convert(time_tmp, &my_timezone, &in_dst_time_gap, true);
99
100
  uint64_t tmp_seconds= time_tmp;
100
 
  uint32_t tmp_micro= ltime->second_part;
 
101
  uint32_t tmp_micro= ltime.second_part;
101
102
 
102
103
  pack_num(tmp_seconds);
103
104
  pack_num(tmp_micro, ptr +8);
109
110
{
110
111
  ASSERT_COLUMN_MARKED_FOR_WRITE;
111
112
 
112
 
  if (from < 0 || from > 99991231235959.0)
 
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))
113
118
  {
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;
 
119
    /* Convert the integer to a string using boost::lexical_cast */
 
120
    std::string tmp(boost::lexical_cast<std::string>(from));
120
121
 
121
 
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
122
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
122
123
    return 2;
123
124
  }
124
 
  return Microtime::store((int64_t) rint(from), false);
 
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;
125
134
}
126
135
 
127
136
int Microtime::store(int64_t from, bool)
134
143
    /* Convert the integer to a string using boost::lexical_cast */
135
144
    std::string tmp(boost::lexical_cast<std::string>(from));
136
145
 
137
 
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
146
    my_error(ER_INVALID_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
138
147
    return 2;
139
148
  }
140
149
 
150
159
 
151
160
double Microtime::val_real(void)
152
161
{
153
 
  return (double) Microtime::val_int();
 
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);
154
189
}
155
190
 
156
191
int64_t Microtime::val_int(void)
174
209
String *Microtime::val_str(String *val_buffer, String *)
175
210
{
176
211
  uint64_t temp= 0;
177
 
  uint32_t micro_temp= 0;
178
 
  char *to;
179
 
  int to_len= field_length + 1 + 8;
180
 
 
181
 
  val_buffer->alloc(to_len);
182
 
  to= (char *) val_buffer->ptr();
 
212
  type::Time::usec_t micro_temp= 0;
183
213
 
184
214
  unpack_num(temp);
185
215
  unpack_num(micro_temp, ptr +8);
186
216
 
187
 
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
188
 
 
189
 
  struct timeval buffer;
190
 
  buffer.tv_sec= temp;
191
 
  buffer.tv_usec= micro_temp;
192
 
 
193
 
  MicroTimestamp temporal;
194
 
  (void) temporal.from_timeval(buffer);
195
 
 
196
 
  int rlen= temporal.to_string(to, to_len);
197
 
  assert(rlen <= to_len);
198
 
 
199
 
  val_buffer->length(rlen);
 
217
  type::Time tmp_time;
 
218
  tmp_time.store(temp, micro_temp);
 
219
 
 
220
  tmp_time.convert(*val_buffer);
 
221
 
200
222
 
201
223
  return val_buffer;
202
224
}
203
225
 
204
 
bool Microtime::get_date(type::Time *ltime, uint32_t)
 
226
bool Microtime::get_date(type::Time &ltime, uint32_t)
205
227
{
206
228
  uint64_t temp;
207
229
  uint32_t micro_temp= 0;
209
231
  unpack_num(temp);
210
232
  unpack_num(micro_temp, ptr +8);
211
233
  
212
 
  memset(ltime, 0, sizeof(*ltime));
213
 
 
214
 
  Timestamp temporal;
215
 
  (void) temporal.from_time_t((time_t) temp);
216
 
 
217
 
  /* @TODO Goodbye the below code when type::Time is finally gone.. */
218
 
 
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();
227
 
 
228
 
  return 0;
 
234
  ltime.reset();
 
235
 
 
236
  ltime.store(temp, micro_temp);
 
237
 
 
238
  return false;
229
239
}
230
240
 
231
 
bool Microtime::get_time(type::Time *ltime)
 
241
bool Microtime::get_time(type::Time &ltime)
232
242
{
233
 
  return Microtime::get_date(ltime,0);
 
243
  return Microtime::get_date(ltime, 0);
234
244
}
235
245
 
236
246
int Microtime::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
275
285
{
276
286
  Session *session= getTable() ? getTable()->in_use : current_session;
277
287
 
278
 
  uint32_t fractional_seconds= 0;
 
288
  type::Time::usec_t fractional_seconds= 0;
279
289
  uint64_t epoch_seconds= session->getCurrentTimestampEpoch(fractional_seconds);
280
290
 
281
291
  set_notnull();