~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/datetime.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
 
21
#include "drizzled/server_includes.h"
22
22
#include "drizzled/field/datetime.h"
23
23
#include "drizzled/error.h"
24
24
#include "drizzled/table.h"
25
25
#include "drizzled/temporal.h"
26
26
#include "drizzled/session.h"
27
27
 
28
 
#include <math.h>
29
 
 
30
28
#include <sstream>
31
29
#include <string>
32
30
 
33
31
 
34
 
namespace drizzled
35
 
{
36
 
 
37
32
/****************************************************************************
38
33
** datetime type
39
34
** In string context: YYYY-MM-DD HH:MM:DD
45
40
                          uint32_t len,
46
41
                          const CHARSET_INFO * const )
47
42
{
48
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
49
43
  /* 
50
44
   * Try to create a DateTime from the supplied string.  Throw an error
51
45
   * if unable to create a valid DateTime.  
52
46
   */
53
 
  DateTime temporal;
 
47
  drizzled::DateTime temporal;
54
48
  if (! temporal.from_string(from, (size_t) len))
55
49
  {
56
50
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), from);
73
67
 
74
68
int Field_datetime::store(double from)
75
69
{
76
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
77
70
  if (from < 0.0 || from > 99991231235959.0)
78
71
  {
79
72
    /* Convert the double to a string using stringstream */
90
83
 
91
84
int Field_datetime::store(int64_t from, bool)
92
85
{
93
 
  ASSERT_COLUMN_MARKED_FOR_WRITE;
94
86
  /* 
95
87
   * Try to create a DateTime from the supplied integer.  Throw an error
96
88
   * if unable to create a valid DateTime.  
97
89
   */
98
 
  DateTime temporal;
 
90
  drizzled::DateTime temporal;
99
91
  if (! temporal.from_int64_t(from))
100
92
  {
101
93
    /* Convert the integer to a string using stringstream */
128
120
 
129
121
int Field_datetime::store_time(DRIZZLE_TIME *ltime, enum enum_drizzle_timestamp_type)
130
122
{
131
 
  DateTime temporal;
 
123
  drizzled::DateTime temporal;
132
124
 
133
125
  temporal.set_years(ltime->year);
134
126
  temporal.set_months(ltime->month);
142
134
    char tmp_string[MAX_DATE_STRING_REP_LENGTH];
143
135
    size_t tmp_string_len;
144
136
 
145
 
    tmp_string_len= temporal.to_string(tmp_string, MAX_DATE_STRING_REP_LENGTH);
146
 
    assert(tmp_string_len < MAX_DATE_STRING_REP_LENGTH);
 
137
    temporal.to_string(tmp_string, &tmp_string_len);
147
138
    my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), tmp_string);
148
139
    return 1;
149
140
  }
170
161
int64_t Field_datetime::val_int(void)
171
162
{
172
163
  int64_t j;
173
 
 
174
 
  ASSERT_COLUMN_MARKED_FOR_READ;
175
 
 
176
164
#ifdef WORDS_BIGENDIAN
177
165
  if (table && table->s->db_low_byte_first)
178
166
    j=sint8korr(ptr);
186
174
String *Field_datetime::val_str(String *val_buffer,
187
175
                                String *)
188
176
{
189
 
  val_buffer->alloc(DateTime::MAX_STRING_LENGTH);
190
 
  val_buffer->length(DateTime::MAX_STRING_LENGTH);
191
 
  int64_t tmp;
192
 
 
193
 
  ASSERT_COLUMN_MARKED_FOR_READ;
 
177
  val_buffer->alloc(field_length);
 
178
  val_buffer->length(field_length);
 
179
  uint64_t tmp;
 
180
  long part1,part2;
 
181
  char *pos;
 
182
  int part3;
194
183
 
195
184
#ifdef WORDS_BIGENDIAN
196
185
  if (table && table->s->db_low_byte_first)
199
188
#endif
200
189
    int64_tget(tmp,ptr);
201
190
 
202
 
  DateTime dt;
203
 
 
204
 
  /* TODO: add an assert that this succeeds
205
 
   * currently fails due to bug in allowing
206
 
   * ALTER TABLE to add a datetime column that's
207
 
   * not null without a default value.
208
 
   */
209
 
  dt.from_int64_t(tmp, false); /* NOTE: this does *NOT* attempt convertion
210
 
                                        from formats such as 20090101 as
211
 
                                        the stored value has already been
212
 
                                        converted.
213
 
                               */
214
 
 
215
 
  int rlen;
216
 
  rlen= dt.to_string((char*)val_buffer->ptr(), DateTime::MAX_STRING_LENGTH);
217
 
  assert((rlen+1) <  DateTime::MAX_STRING_LENGTH);
218
 
 
219
 
  val_buffer->length(rlen);
220
 
 
 
191
  /*
 
192
    Avoid problem with slow int64_t arithmetic and sprintf
 
193
  */
 
194
 
 
195
  part1=(long) (tmp/INT64_C(1000000));
 
196
  part2=(long) (tmp - (uint64_t) part1*INT64_C(1000000));
 
197
 
 
198
  pos=(char*) val_buffer->ptr() + MAX_DATETIME_WIDTH;
 
199
  *pos--=0;
 
200
  *pos--= (char) ('0'+(char) (part2%10)); part2/=10;
 
201
  *pos--= (char) ('0'+(char) (part2%10)); part3= (int) (part2 / 10);
 
202
  *pos--= ':';
 
203
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
204
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
205
  *pos--= ':';
 
206
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
207
  *pos--= (char) ('0'+(char) part3);
 
208
  *pos--= ' ';
 
209
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
210
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
211
  *pos--= '-';
 
212
  *pos--= (char) ('0'+(char) (part1%10)); part1/=10;
 
213
  *pos--= (char) ('0'+(char) (part1%10)); part3= (int) (part1/10);
 
214
  *pos--= '-';
 
215
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
216
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
217
  *pos--= (char) ('0'+(char) (part3%10)); part3/=10;
 
218
  *pos=(char) ('0'+(char) part3);
221
219
  return val_buffer;
222
220
}
223
221
 
298
296
  res.set_ascii(STRING_WITH_LEN("datetime"));
299
297
}
300
298
 
301
 
} /* namespace drizzled */