Drizzled Public API Documentation

from_unixtime.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <boost/lexical_cast.hpp>
00022 #include <drizzled/function/time/from_unixtime.h>
00023 #include <drizzled/current_session.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/temporal.h>
00026 #include <drizzled/time_functions.h>
00027 #include <drizzled/field.h>
00028 
00029 #include <sstream>
00030 #include <string>
00031 
00032 namespace drizzled
00033 {
00034 
00035 void Item_func_from_unixtime::fix_length_and_dec()
00036 {
00037   session= current_session;
00038   collation.set(&my_charset_bin);
00039   decimals= DATETIME_DEC;
00040   max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
00041   maybe_null= 1;
00042 }
00043 
00044 String *Item_func_from_unixtime::val_str(String *str)
00045 {
00046   type::Time time_tmp;
00047 
00048   assert(fixed == 1);
00049 
00050   if (get_date(time_tmp, 0))
00051     return 0;
00052 
00053   str->alloc(type::Time::MAX_STRING_LENGTH);
00054 
00055   time_tmp.convert(*str);
00056 
00057   return str;
00058 }
00059 
00060 int64_t Item_func_from_unixtime::val_int()
00061 {
00062   type::Time time_tmp;
00063 
00064   assert(fixed == 1);
00065 
00066   if (get_date(time_tmp, 0))
00067     return 0;
00068 
00069   int64_t ret;
00070   time_tmp.convert(ret);
00071 
00072   return (int64_t) ret;
00073 }
00074 
00075 bool Item_func_from_unixtime::get_date(type::Time &ltime, uint32_t)
00076 {
00077   uint64_t tmp= 0;
00078   type::usec_t fractional_tmp= 0;
00079 
00080   switch (args[0]->result_type()) {
00081   case REAL_RESULT:
00082   case ROW_RESULT:
00083   case DECIMAL_RESULT:
00084   case STRING_RESULT:
00085     {
00086       double double_tmp= args[0]->val_real();
00087 
00088       tmp= (uint64_t)(double_tmp);
00089       fractional_tmp=  (type::usec_t)((uint64_t)((double_tmp - tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS);
00090 
00091       break;
00092     }
00093 
00094   case INT_RESULT:
00095     tmp= (uint64_t)(args[0]->val_int());
00096     break;
00097   }
00098 
00099   /*
00100     "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
00101     from_unixtime() argument since tmp is unsigned.
00102   */
00103   if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
00104     return 1;
00105 
00106   ltime.reset();
00107   ltime.store(tmp, fractional_tmp);
00108 
00109   return 0;
00110 }
00111 
00112 } /* namespace drizzled */