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 */,
61
/* For 4.0 MYD and 4.0 InnoDB compatibility */
62
flags|= UNSIGNED_FLAG;
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,
75
/* For 4.0 MYD and 4.0 InnoDB compatibility */
76
flags|= UNSIGNED_FLAG;
79
int Time::store(const char *from,
81
const CHARSET_INFO * const )
83
drizzled::Time temporal;
85
ASSERT_COLUMN_MARKED_FOR_WRITE;
87
if (not temporal.from_string(from, (size_t) len))
89
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
94
temporal.to_uint64_t(tmp);
101
int Time::store(double from)
103
ASSERT_COLUMN_MARKED_FOR_WRITE;
105
if (from < 0 || from > 99991231235959.0)
107
/* Convert the double to a string using stringstream */
108
std::stringstream ss;
110
ss.precision(18); /* 18 places should be fine for error display of double input. */
114
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
117
return Time::store((int64_t) rint(from), false);
120
int Time::store(int64_t from, bool)
122
ASSERT_COLUMN_MARKED_FOR_WRITE;
125
* Try to create a DateTime from the supplied integer. Throw an error
126
* if unable to create a valid DateTime.
129
if (! temporal.from_int64_t(from))
131
/* Convert the integer to a string using boost::lexical_cast */
132
std::string tmp(boost::lexical_cast<std::string>(from));
134
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
139
temporal.to_time_t((time_t*)&tmp);
146
double Time::val_real(void)
148
return (double) Time::val_int();
151
int64_t Time::val_int(void)
155
ASSERT_COLUMN_MARKED_FOR_READ;
160
(void) temporal.from_time_t((time_t) temp);
162
/* We must convert into a "timestamp-formatted integer" ... */
164
temporal.to_int64_t(&result);
168
String *Time::val_str(String *val_buffer, String *)
172
int to_len= field_length + 1;
174
val_buffer->alloc(to_len);
175
to= (char *) val_buffer->ptr();
179
val_buffer->set_charset(&my_charset_bin); /* Safety */
182
(void) temporal.from_time_t((time_t) temp);
185
rlen= temporal.to_string(to, to_len);
186
assert(rlen < to_len);
188
val_buffer->length(rlen);
192
bool Time::get_date(DRIZZLE_TIME *ltime, uint32_t)
198
memset(ltime, 0, sizeof(*ltime));
201
(void) temporal.from_time_t((time_t) temp);
203
/* @TODO Goodbye the below code when DRIZZLE_TIME is finally gone.. */
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();
216
bool Time::get_time(DRIZZLE_TIME *ltime)
218
return Time::get_date(ltime,0);
221
int Time::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
225
unpack_num(a, a_ptr);
226
unpack_num(b, b_ptr);
228
return (a < b) ? -1 : (a > b) ? 1 : 0;
232
void Time::sort_string(unsigned char *to,uint32_t )
234
#ifdef WORDS_BIGENDIAN
235
if (!getTable() || !getTable()->getShare()->db_low_byte_first)
260
void Time::sql_type(String &res) const
262
res.set_ascii(STRING_WITH_LEN("timestamp"));
265
long Time::get_timestamp(bool *null_value)
267
if ((*null_value= is_null()))
271
return unpack_num(tmp);
274
size_t Time::max_string_length()
276
return sizeof(uint64_t);
279
} /* namespace field */
280
} /* namespace drizzled */