~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/time.cc

  • Committer: Brian Aker
  • Date: 2010-12-27 20:04:50 UTC
  • mto: (2060.2.1 clean)
  • mto: This revision was merged to the branch mainline in revision 2063.
  • Revision ID: brian@tangent.org-20101227200450-dmxpemwyfmlinlnm
Merge in first pass.

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 "config.h"
22
22
#include <boost/lexical_cast.hpp>
23
23
#include <drizzled/field/time.h>
24
24
#include <drizzled/error.h>
30
30
 
31
31
#include <sstream>
32
32
 
33
 
#include <drizzled/temporal.h>
 
33
#include "drizzled/temporal.h"
34
34
 
35
35
namespace drizzled
36
36
{
49
49
             uint32_t,
50
50
             unsigned char *null_ptr_arg,
51
51
             unsigned char null_bit_arg,
52
 
             const char *field_name_arg) :
 
52
             const char *field_name_arg,
 
53
             const CHARSET_INFO * const cs) :
53
54
    Field_str(ptr_arg,
54
55
              DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
55
56
              null_ptr_arg,
56
57
              null_bit_arg,
57
58
              field_name_arg,
58
 
              &my_charset_bin)
 
59
              cs)
59
60
{
60
61
}
61
62
 
62
63
Time::Time(bool maybe_null_arg,
63
 
           const char *field_name_arg) :
 
64
           const char *field_name_arg,
 
65
           const CHARSET_INFO * const cs) :
64
66
  Field_str((unsigned char*) NULL,
65
67
            DateTime::MAX_STRING_LENGTH - 1 /* no \0 */,
66
68
            maybe_null_arg ? (unsigned char*) "": 0,
67
69
            0,
68
70
            field_name_arg,
69
 
            &my_charset_bin)
 
71
            cs)
70
72
{
71
73
}
72
74
 
80
82
 
81
83
  if (not temporal.from_string(from, (size_t) len))
82
84
  {
83
 
    std::string tmp(boost::lexical_cast<std::string>(from));
84
 
    my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
 
85
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), from);
85
86
    return 1;
86
87
  }
87
88
 
93
94
int Time::store(double from)
94
95
95
96
  ASSERT_COLUMN_MARKED_FOR_WRITE;
96
 
 
97
 
  do
98
 
  {
99
 
    int64_t tmp;
100
 
 
101
 
    if (from > (double)TIME_MAX_VALUE)
102
 
    { 
103
 
      tmp= TIME_MAX_VALUE;
104
 
      break;
105
 
    }
106
 
    else if (from < (double) - TIME_MAX_VALUE)
107
 
    { 
108
 
      tmp= -TIME_MAX_VALUE;
109
 
      break;
110
 
    }
111
 
    else
112
 
    { 
113
 
      tmp=(long) floor(fabs(from));                 // Remove fractions
114
 
 
115
 
      if (from < 0)
116
 
        tmp= -tmp;
117
 
 
118
 
      if (tmp % 100 > 59 || tmp/100 % 100 > 59)
119
 
      { 
120
 
        break;
121
 
      }
122
 
    }
123
 
 
 
97
  int64_t tmp;
 
98
  int error= 0;
 
99
 
 
100
  if (from > (double)TIME_MAX_VALUE)
 
101
  { 
 
102
    tmp= TIME_MAX_VALUE;
 
103
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
104
                         ER_WARN_DATA_OUT_OF_RANGE, from, DRIZZLE_TIMESTAMP_TIME);
 
105
    error= 1;
 
106
  }
 
107
  else if (from < (double) - TIME_MAX_VALUE)
 
108
  { 
 
109
    tmp= -TIME_MAX_VALUE;
 
110
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
111
                         ER_WARN_DATA_OUT_OF_RANGE, from, DRIZZLE_TIMESTAMP_TIME);
 
112
    error= 1;
 
113
  }
 
114
  else
 
115
  { 
 
116
    tmp=(long) floor(fabs(from));                 // Remove fractions
 
117
 
 
118
    if (from < 0)
 
119
      tmp= -tmp;
 
120
 
 
121
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
 
122
    { 
 
123
      tmp=0;
 
124
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
125
                           ER_WARN_DATA_OUT_OF_RANGE, from,
 
126
                           DRIZZLE_TIMESTAMP_TIME);
 
127
      error= 1;
 
128
    }
 
129
  }
 
130
 
 
131
  if (not error)
124
132
    return store(tmp, false);
125
133
 
126
 
  } while (0);
127
 
 
128
 
  std::string tmp(boost::lexical_cast<std::string>(from));
129
 
  my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
130
 
 
131
 
  return 1;
 
134
  return error;
132
135
}
133
136
 
134
137
int Time::store(int64_t from, bool)
140
143
   * if unable to create a valid DateTime.  
141
144
   */
142
145
  drizzled::Time temporal;
143
 
  if (not temporal.from_time_t(from))
 
146
  if (! temporal.from_time_t(from))
144
147
  {
145
148
    /* Convert the integer to a string using boost::lexical_cast */
146
149
    std::string tmp(boost::lexical_cast<std::string>(from));
147
 
    my_error(ER_INVALID_TIME_VALUE, MYF(0), tmp.c_str());
 
150
 
 
151
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(ME_FATALERROR), tmp.c_str());
148
152
    return 2;
149
153
  }
150
154
 
161
165
  memcpy(ptr, &tmp, sizeof(int32_t));
162
166
}
163
167
 
164
 
void Time::unpack_time(drizzled::Time &temporal) const
 
168
void Time::unpack_time(drizzled::Time &temporal)
165
169
{
166
170
  int32_t tmp;
167
171
 
171
175
  temporal.from_int32_t(tmp);
172
176
}
173
177
 
174
 
void Time::unpack_time(int32_t &destination, const unsigned char *source) const
 
178
void Time::unpack_time(int32_t &destination, const unsigned char *source)
175
179
{
176
180
  memcpy(&destination, source, sizeof(int32_t));
177
181
  destination= htonl(destination);
178
182
}
179
183
 
180
 
double Time::val_real(void) const
 
184
double Time::val_real(void)
181
185
{
182
186
  return (double) Time::val_int();
183
187
}
184
188
 
185
 
int64_t Time::val_int(void) const
 
189
int64_t Time::val_int(void)
186
190
{
187
191
  ASSERT_COLUMN_MARKED_FOR_READ;
188
192
 
195
199
  return result;
196
200
}
197
201
 
198
 
String *Time::val_str(String *val_buffer, String *) const
 
202
String *Time::val_str(String *val_buffer, String *)
199
203
{
200
204
  char *to;
201
205
  int to_len= field_length + 1;
216
220
  return val_buffer;
217
221
}
218
222
 
219
 
bool Time::get_date(type::Time &ltime, uint32_t) const
 
223
bool Time::get_date(DRIZZLE_TIME *ltime, uint32_t)
220
224
{
221
 
  ltime.reset();
 
225
  memset(ltime, 0, sizeof(*ltime));
222
226
 
223
227
  drizzled::Time temporal;
224
228
  unpack_time(temporal);
225
229
 
226
 
  ltime.time_type= type::DRIZZLE_TIMESTAMP_DATETIME;
227
 
  ltime.year= temporal.years();
228
 
  ltime.month= temporal.months();
229
 
  ltime.day= temporal.days();
230
 
  ltime.hour= temporal.hours();
231
 
  ltime.minute= temporal.minutes();
232
 
  ltime.second= temporal.seconds();
 
230
  ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
 
231
  ltime->year= temporal.years();
 
232
  ltime->month= temporal.months();
 
233
  ltime->day= temporal.days();
 
234
  ltime->hour= temporal.hours();
 
235
  ltime->minute= temporal.minutes();
 
236
  ltime->second= temporal.seconds();
233
237
 
234
238
  return 0;
235
239
}
236
240
 
237
 
bool Time::get_time(type::Time &ltime) const
 
241
bool Time::get_time(DRIZZLE_TIME *ltime)
238
242
{
239
243
  return Time::get_date(ltime, 0);
240
244
}
275
279
  res.set_ascii(STRING_WITH_LEN("timestamp"));
276
280
}
277
281
 
278
 
long Time::get_timestamp(bool *null_value) const
 
282
long Time::get_timestamp(bool *null_value)
279
283
{
280
284
  if ((*null_value= is_null()))
281
285
    return 0;