~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/time.cc

  • Committer: Olaf van der Spek
  • Date: 2011-02-12 18:24:24 UTC
  • mto: (2167.1.2 build) (2172.1.4 build)
  • mto: This revision was merged to the branch mainline in revision 2168.
  • Revision ID: olafvdspek@gmail.com-20110212182424-kgnm9osi7qo97at2
casts

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
}
 
62
 
 
63
Time::Time(bool maybe_null_arg,
 
64
           const char *field_name_arg,
 
65
           const CHARSET_INFO * const cs) :
 
66
  Field_str((unsigned char*) NULL,
 
67
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
 
68
            maybe_null_arg ? (unsigned char*) "": 0,
 
69
            0,
 
70
            field_name_arg,
 
71
            cs)
 
72
{
 
73
}
 
74
 
 
75
int Time::store(const char *from,
 
76
                uint32_t len,
 
77
                const CHARSET_INFO * const )
 
78
{
 
79
  drizzled::Time temporal;
 
80
 
 
81
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
82
 
 
83
  if (not temporal.from_string(from, (size_t) len))
 
84
  {
 
85
    std::string tmp(boost::lexical_cast<std::string>(from));
 
86
    my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
 
87
    return 1;
 
88
  }
 
89
 
 
90
  pack_time(temporal);
 
91
 
 
92
  return 0;
 
93
}
 
94
 
 
95
int Time::store(double from)
 
96
 
97
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
98
 
 
99
  do
 
100
  {
 
101
    int64_t tmp;
 
102
 
 
103
    if (from > (double)TIME_MAX_VALUE)
 
104
    { 
 
105
      tmp= TIME_MAX_VALUE;
 
106
      break;
 
107
    }
 
108
    else if (from < (double) - TIME_MAX_VALUE)
 
109
    { 
 
110
      tmp= -TIME_MAX_VALUE;
 
111
      break;
 
112
    }
 
113
    else
 
114
    { 
 
115
      tmp=(long) floor(fabs(from));                 // Remove fractions
 
116
 
 
117
      if (from < 0)
 
118
        tmp= -tmp;
 
119
 
 
120
      if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
121
      { 
 
122
        break;
 
123
      }
 
124
    }
 
125
 
 
126
    return store(tmp, false);
 
127
 
 
128
  } while (0);
 
129
 
 
130
  std::string tmp(boost::lexical_cast<std::string>(from));
 
131
  my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
 
132
 
 
133
  return 1;
 
134
}
 
135
 
 
136
int Time::store(int64_t from, bool)
 
137
{
 
138
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
139
 
 
140
  /* 
 
141
   * Try to create a DateTime from the supplied integer.  Throw an error
 
142
   * if unable to create a valid DateTime.  
 
143
   */
 
144
  drizzled::Time temporal;
 
145
  if (not temporal.from_time_t(from))
 
146
  {
 
147
    /* Convert the integer to a string using boost::lexical_cast */
 
148
    std::string tmp(boost::lexical_cast<std::string>(from));
 
149
    my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
 
150
    return 2;
 
151
  }
 
152
 
 
153
  pack_time(temporal);
 
154
 
 
155
  return 0;
 
156
}
 
157
 
 
158
void Time::pack_time(drizzled::Time &temporal)
 
159
{
 
160
  int32_t tmp;
 
161
  temporal.to_int32_t(&tmp);
 
162
  tmp= htonl(tmp);
 
163
  memcpy(ptr, &tmp, sizeof(int32_t));
 
164
}
 
165
 
 
166
void Time::unpack_time(drizzled::Time &temporal)
 
167
{
 
168
  int32_t tmp;
 
169
 
 
170
  memcpy(&tmp, ptr, sizeof(int32_t));
 
171
  tmp= htonl(tmp);
 
172
 
 
173
  temporal.from_int32_t(tmp);
 
174
}
 
175
 
 
176
void Time::unpack_time(int32_t &destination, const unsigned char *source)
 
177
{
 
178
  memcpy(&destination, source, sizeof(int32_t));
 
179
  destination= htonl(destination);
 
180
}
 
181
 
 
182
double Time::val_real(void)
 
183
{
 
184
  return (double) Time::val_int();
 
185
}
 
186
 
 
187
int64_t Time::val_int(void)
 
188
{
 
189
  ASSERT_COLUMN_MARKED_FOR_READ;
 
190
 
 
191
  drizzled::Time temporal;
 
192
  unpack_time(temporal);
 
193
 
 
194
  /* We must convert into a "timestamp-formatted integer" ... */
 
195
  uint64_t result;
 
196
  temporal.to_uint64_t(result);
 
197
  return result;
 
198
}
 
199
 
 
200
String *Time::val_str(String *val_buffer, String *)
 
201
{
 
202
  char *to;
 
203
  int to_len= field_length + 1;
 
204
 
 
205
  val_buffer->alloc(to_len);
 
206
  to= (char *) val_buffer->ptr();
 
207
 
 
208
  val_buffer->set_charset(&my_charset_bin);     /* Safety */
 
209
 
 
210
  drizzled::Time temporal;
 
211
  unpack_time(temporal);
 
212
 
 
213
  int rlen;
 
214
  rlen= temporal.to_string(to, to_len);
 
215
  assert(rlen < to_len);
 
216
 
 
217
  val_buffer->length(rlen);
 
218
  return val_buffer;
 
219
}
 
220
 
 
221
bool Time::get_date(type::Time &ltime, uint32_t)
 
222
{
 
223
  ltime.reset();
 
224
 
 
225
  drizzled::Time temporal;
 
226
  unpack_time(temporal);
 
227
 
 
228
  ltime.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
 
229
  ltime.year= temporal.years();
 
230
  ltime.month= temporal.months();
 
231
  ltime.day= temporal.days();
 
232
  ltime.hour= temporal.hours();
 
233
  ltime.minute= temporal.minutes();
 
234
  ltime.second= temporal.seconds();
 
235
 
 
236
  return 0;
 
237
}
 
238
 
 
239
bool Time::get_time(type::Time &ltime)
 
240
{
 
241
  return Time::get_date(ltime, 0);
 
242
}
 
243
 
 
244
int Time::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
 
245
{
 
246
  int32_t a,b;
 
247
 
 
248
  unpack_time(a, a_ptr);
 
249
  unpack_time(b, b_ptr);
 
250
 
 
251
  return (a < b) ? -1 : (a > b) ? 1 : 0;
 
252
}
 
253
 
 
254
 
 
255
void Time::sort_string(unsigned char *to,uint32_t )
 
256
{
 
257
#ifdef WORDS_BIGENDIAN
 
258
  if (!getTable() || !getTable()->getShare()->db_low_byte_first)
 
259
  {
 
260
    to[0] = ptr[0];
 
261
    to[1] = ptr[1];
 
262
    to[2] = ptr[2];
 
263
    to[3] = ptr[3];
 
264
  }
 
265
  else
 
266
#endif
 
267
  {
 
268
    to[0] = ptr[3];
 
269
    to[1] = ptr[2];
 
270
    to[2] = ptr[1];
 
271
    to[3] = ptr[0];
 
272
  }
 
273
}
 
274
 
 
275
void Time::sql_type(String &res) const
 
276
{
 
277
  res.set_ascii(STRING_WITH_LEN("timestamp"));
 
278
}
 
279
 
 
280
long Time::get_timestamp(bool *null_value)
 
281
{
 
282
  if ((*null_value= is_null()))
 
283
    return 0;
 
284
 
 
285
  uint64_t tmp;
 
286
  return unpack_num(tmp);
 
287
}
 
288
 
 
289
size_t Time::max_string_length()
 
290
{
 
291
  return sizeof(int64_t);
 
292
}
 
293
 
 
294
} /* namespace field */
 
295
} /* namespace drizzled */