1
/* -*- mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
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>
33
#include "drizzled/temporal.h"
44
** In string context: HH:MM:SS
45
** In number context: HHMMSS
48
Time::Time(unsigned char *ptr_arg,
50
unsigned char *null_ptr_arg,
51
unsigned char null_bit_arg,
52
const char *field_name_arg,
53
const CHARSET_INFO * const cs) :
55
DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
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,
75
int Time::store(const char *from,
77
const CHARSET_INFO * const )
79
drizzled::Time temporal;
81
ASSERT_COLUMN_MARKED_FOR_WRITE;
83
if (not temporal.from_string(from, (size_t) len))
85
std::string tmp(boost::lexical_cast<std::string>(from));
86
my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
95
int Time::store(double from)
97
ASSERT_COLUMN_MARKED_FOR_WRITE;
103
if (from > (double)TIME_MAX_VALUE)
108
else if (from < (double) - TIME_MAX_VALUE)
110
tmp= -TIME_MAX_VALUE;
115
tmp=(long) floor(fabs(from)); // Remove fractions
120
if (tmp % 100 > 59 || tmp/100 % 100 > 59)
126
return store(tmp, false);
130
std::string tmp(boost::lexical_cast<std::string>(from));
131
my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
136
int Time::store(int64_t from, bool)
138
ASSERT_COLUMN_MARKED_FOR_WRITE;
141
* Try to create a DateTime from the supplied integer. Throw an error
142
* if unable to create a valid DateTime.
144
drizzled::Time temporal;
145
if (not temporal.from_time_t(from))
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());
158
void Time::pack_time(drizzled::Time &temporal)
161
temporal.to_int32_t(&tmp);
163
memcpy(ptr, &tmp, sizeof(int32_t));
166
void Time::unpack_time(drizzled::Time &temporal)
170
memcpy(&tmp, ptr, sizeof(int32_t));
173
temporal.from_int32_t(tmp);
176
void Time::unpack_time(int32_t &destination, const unsigned char *source)
178
memcpy(&destination, source, sizeof(int32_t));
179
destination= htonl(destination);
182
double Time::val_real(void)
184
return (double) Time::val_int();
187
int64_t Time::val_int(void)
189
ASSERT_COLUMN_MARKED_FOR_READ;
191
drizzled::Time temporal;
192
unpack_time(temporal);
194
/* We must convert into a "timestamp-formatted integer" ... */
196
temporal.to_uint64_t(result);
200
String *Time::val_str(String *val_buffer, String *)
203
int to_len= field_length + 1;
205
val_buffer->alloc(to_len);
206
to= (char *) val_buffer->ptr();
208
val_buffer->set_charset(&my_charset_bin); /* Safety */
210
drizzled::Time temporal;
211
unpack_time(temporal);
214
rlen= temporal.to_string(to, to_len);
215
assert(rlen < to_len);
217
val_buffer->length(rlen);
221
bool Time::get_date(type::Time <ime, uint32_t)
225
drizzled::Time temporal;
226
unpack_time(temporal);
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();
239
bool Time::get_time(type::Time <ime)
241
return Time::get_date(ltime, 0);
244
int Time::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
248
unpack_time(a, a_ptr);
249
unpack_time(b, b_ptr);
251
return (a < b) ? -1 : (a > b) ? 1 : 0;
255
void Time::sort_string(unsigned char *to,uint32_t )
257
#ifdef WORDS_BIGENDIAN
258
if (!getTable() || !getTable()->getShare()->db_low_byte_first)
275
void Time::sql_type(String &res) const
277
res.set_ascii(STRING_WITH_LEN("timestamp"));
280
long Time::get_timestamp(bool *null_value)
282
if ((*null_value= is_null()))
286
return unpack_num(tmp);
289
size_t Time::max_string_length()
291
return sizeof(int64_t);
294
} /* namespace field */
295
} /* namespace drizzled */