~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/time.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 02:08:04 UTC
  • mto: (2015.1.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2018.
  • Revision ID: brian@tangent.org-20101218020804-mh3lga9uv7jlajwy
Merge in first pass of TIME type (closer to EPOCH time).

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
 
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/time.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 "drizzled/temporal.h"
 
34
 
 
35
namespace drizzled
 
36
{
 
37
 
 
38
namespace field
 
39
{
 
40
 
 
41
/**
 
42
  time_t type
 
43
 
 
44
  ** In string context: HH:MM:SS
 
45
  ** In number context: HHMMSS
 
46
 
 
47
 */
 
48
  Time::Time(unsigned char *ptr_arg,
 
49
             uint32_t,
 
50
             unsigned char *null_ptr_arg,
 
51
             unsigned char null_bit_arg,
 
52
             const char *field_name_arg,
 
53
             const CHARSET_INFO * const cs) :
 
54
    Field_str(ptr_arg,
 
55
              DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
56
              null_ptr_arg,
 
57
              null_bit_arg,
 
58
              field_name_arg,
 
59
              cs)
 
60
{
 
61
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
62
  flags|= UNSIGNED_FLAG;
 
63
}
 
64
 
 
65
Time::Time(bool maybe_null_arg,
 
66
           const char *field_name_arg,
 
67
           const CHARSET_INFO * const cs) :
 
68
  Field_str((unsigned char*) NULL,
 
69
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
70
            maybe_null_arg ? (unsigned char*) "": 0,
 
71
            0,
 
72
            field_name_arg,
 
73
            cs)
 
74
{
 
75
  /* For 4.0 MYD and 4.0 InnoDB compatibility */
 
76
  flags|= UNSIGNED_FLAG;
 
77
}
 
78
 
 
79
int Time::store(const char *from,
 
80
                uint32_t len,
 
81
                const CHARSET_INFO * const )
 
82
{
 
83
  drizzled::Time temporal;
 
84
 
 
85
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
86
 
 
87
  if (not temporal.from_string(from, (size_t) len))
 
88
  {
 
89
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
 
90
    return 1;
 
91
  }
 
92
 
 
93
  uint64_t tmp;
 
94
  temporal.to_uint64_t(tmp);
 
95
 
 
96
  pack_num(tmp);
 
97
 
 
98
  return 0;
 
99
}
 
100
 
 
101
int Time::store(double from)
 
102
{
 
103
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
104
 
 
105
  if (from < 0 || from > 99991231235959.0)
 
106
  {
 
107
    /* Convert the double to a string using stringstream */
 
108
    std::stringstream ss;
 
109
    std::string tmp;
 
110
    ss.precision(18); /* 18 places should be fine for error display of double input. */
 
111
    ss << from; 
 
112
    ss >> tmp;
 
113
 
 
114
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
115
    return 2;
 
116
  }
 
117
  return Time::store((int64_t) rint(from), false);
 
118
}
 
119
 
 
120
int Time::store(int64_t from, bool)
 
121
{
 
122
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
123
 
 
124
  /* 
 
125
   * Try to create a DateTime from the supplied integer.  Throw an error
 
126
   * if unable to create a valid DateTime.  
 
127
   */
 
128
  Timestamp temporal;
 
129
  if (! temporal.from_int64_t(from))
 
130
  {
 
131
    /* Convert the integer to a string using boost::lexical_cast */
 
132
    std::string tmp(boost::lexical_cast<std::string>(from));
 
133
 
 
134
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
 
135
    return 2;
 
136
  }
 
137
 
 
138
  uint64_t tmp;
 
139
  temporal.to_time_t((time_t*)&tmp);
 
140
 
 
141
  pack_num(tmp);
 
142
 
 
143
  return 0;
 
144
}
 
145
 
 
146
double Time::val_real(void)
 
147
{
 
148
  return (double) Time::val_int();
 
149
}
 
150
 
 
151
int64_t Time::val_int(void)
 
152
{
 
153
  uint64_t temp;
 
154
 
 
155
  ASSERT_COLUMN_MARKED_FOR_READ;
 
156
 
 
157
  unpack_num(temp);
 
158
 
 
159
  Timestamp temporal;
 
160
  (void) temporal.from_time_t((time_t) temp);
 
161
 
 
162
  /* We must convert into a "timestamp-formatted integer" ... */
 
163
  int64_t result;
 
164
  temporal.to_int64_t(&result);
 
165
  return result;
 
166
}
 
167
 
 
168
String *Time::val_str(String *val_buffer, String *)
 
169
{
 
170
  uint64_t temp= 0;
 
171
  char *to;
 
172
  int to_len= field_length + 1;
 
173
 
 
174
  val_buffer->alloc(to_len);
 
175
  to= (char *) val_buffer->ptr();
 
176
 
 
177
  unpack_num(temp);
 
178
 
 
179
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
 
180
 
 
181
  Timestamp temporal;
 
182
  (void) temporal.from_time_t((time_t) temp);
 
183
 
 
184
  int rlen;
 
185
  rlen= temporal.to_string(to, to_len);
 
186
  assert(rlen < to_len);
 
187
 
 
188
  val_buffer->length(rlen);
 
189
  return val_buffer;
 
190
}
 
191
 
 
192
bool Time::get_date(DRIZZLE_TIME *ltime, uint32_t)
 
193
{
 
194
  uint64_t temp;
 
195
 
 
196
  unpack_num(temp);
 
197
  
 
198
  memset(ltime, 0, sizeof(*ltime));
 
199
 
 
200
  Timestamp temporal;
 
201
  (void) temporal.from_time_t((time_t) temp);
 
202
 
 
203
  /* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
 
204
 
 
205
  ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
 
206
  ltime->year= temporal.years();
 
207
  ltime->month= temporal.months();
 
208
  ltime->day= temporal.days();
 
209
  ltime->hour= temporal.hours();
 
210
  ltime->minute= temporal.minutes();
 
211
  ltime->second= temporal.seconds();
 
212
 
 
213
  return 0;
 
214
}
 
215
 
 
216
bool Time::get_time(DRIZZLE_TIME *ltime)
 
217
{
 
218
  return Time::get_date(ltime,0);
 
219
}
 
220
 
 
221
int Time::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
222
{
 
223
  uint64_t a,b;
 
224
 
 
225
  unpack_num(a, a_ptr);
 
226
  unpack_num(b, b_ptr);
 
227
 
 
228
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
229
}
 
230
 
 
231
 
 
232
void Time::sort_string(unsigned char *to,uint32_t )
 
233
{
 
234
#ifdef WORDS_BIGENDIAN
 
235
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
 
236
  {
 
237
    to[0] = ptr[0];
 
238
    to[1] = ptr[1];
 
239
    to[2] = ptr[2];
 
240
    to[3] = ptr[3];
 
241
    to[4] = ptr[4];
 
242
    to[5] = ptr[5];
 
243
    to[6] = ptr[6];
 
244
    to[7] = ptr[7];
 
245
  }
 
246
  else
 
247
#endif
 
248
  {
 
249
    to[0] = ptr[7];
 
250
    to[1] = ptr[6];
 
251
    to[2] = ptr[5];
 
252
    to[3] = ptr[4];
 
253
    to[4] = ptr[3];
 
254
    to[5] = ptr[2];
 
255
    to[6] = ptr[1];
 
256
    to[7] = ptr[0];
 
257
  }
 
258
}
 
259
 
 
260
void Time::sql_type(String &res) const
 
261
{
 
262
  res.set_ascii(STRING_WITH_LEN("timestamp"));
 
263
}
 
264
 
 
265
long Time::get_timestamp(bool *null_value)
 
266
{
 
267
  if ((*null_value= is_null()))
 
268
    return 0;
 
269
 
 
270
  uint64_t tmp;
 
271
  return unpack_num(tmp);
 
272
}
 
273
 
 
274
size_t Time::max_string_length()
 
275
{
 
276
  return sizeof(uint64_t);
 
277
}
 
278
 
 
279
} /* namespace field */
 
280
} /* namespace drizzled */