1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 MySQL
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
21
#ifdef USE_PRAGMA_IMPLEMENTATION
22
#pragma implementation // gcc: Class implementation
27
/****************************************************************************
29
** Save in a byte the year 0, 1901->2155
30
** Can handle 2 byte or 4 byte years!
31
****************************************************************************/
33
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs)
37
int64_t nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error);
39
if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 ||
40
error == MY_ERRNO_ERANGE)
43
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
46
if (table->in_use->count_cuted_fields &&
47
(error= check_int(cs, from, len, end, error)))
49
if (error == 1) /* empty or incorrect string */
57
if (nr != 0 || len != 4)
59
if (nr < YY_PART_YEAR)
60
nr+=100; // 2000 - 2069
64
*ptr= (char) (uchar) nr;
69
int Field_year::store(double nr)
71
if (nr < 0.0 || nr >= 2155.0)
73
(void) Field_year::store((int64_t) -1, false);
76
return Field_year::store((int64_t) nr, false);
80
int Field_year::store(int64_t nr,
81
bool unsigned_val __attribute__((__unused__)))
83
if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155)
86
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1);
89
if (nr != 0 || field_length != 4) // 0000 -> 0; 00 -> 2000
91
if (nr < YY_PART_YEAR)
92
nr+=100; // 2000 - 2069
96
*ptr= (char) (uchar) nr;
101
bool Field_year::send_binary(Protocol *protocol)
103
uint64_t tmp= Field_year::val_int();
104
return protocol->store_short(tmp);
108
double Field_year::val_real(void)
110
return (double) Field_year::val_int();
114
int64_t Field_year::val_int(void)
116
int tmp= (int) ptr[0];
117
if (field_length != 4)
118
tmp%=100; // Return last 2 char
121
return (int64_t) tmp;
125
String *Field_year::val_str(String *val_buffer,
126
String *val_ptr __attribute__((unused)))
128
val_buffer->alloc(5);
129
val_buffer->length(field_length);
130
char *to=(char*) val_buffer->ptr();
131
sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int());
136
void Field_year::sql_type(String &res) const
138
CHARSET_INFO *cs=res.charset();
139
res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(),
140
"year(%d)",(int) field_length));