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   if (str->alloc(type::Time::MAX_STRING_LENGTH))
00054   {
00055     null_value= 1;
00056     return 0;
00057   }
00058 
00059   time_tmp.convert(*str);
00060 
00061   return str;
00062 }
00063 
00064 int64_t Item_func_from_unixtime::val_int()
00065 {
00066   type::Time time_tmp;
00067 
00068   assert(fixed == 1);
00069 
00070   if (get_date(time_tmp, 0))
00071     return 0;
00072 
00073   int64_t ret;
00074   time_tmp.convert(ret);
00075 
00076   return (int64_t) ret;
00077 }
00078 
00079 bool Item_func_from_unixtime::get_date(type::Time &ltime, uint32_t)
00080 {
00081   uint64_t tmp= 0;
00082   type::Time::usec_t fractional_tmp= 0;
00083 
00084   switch (args[0]->result_type()) {
00085   case REAL_RESULT:
00086   case ROW_RESULT:
00087   case DECIMAL_RESULT:
00088   case STRING_RESULT:
00089     {
00090       double double_tmp= args[0]->val_real();
00091 
00092       tmp= (uint64_t)(double_tmp);
00093       fractional_tmp=  (type::Time::usec_t)((uint64_t)((double_tmp - tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS);
00094 
00095       break;
00096     }
00097 
00098   case INT_RESULT:
00099     tmp= (uint64_t)(args[0]->val_int());
00100     break;
00101   }
00102 
00103   /*
00104     "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
00105     from_unixtime() argument since tmp is unsigned.
00106   */
00107   if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
00108     return 1;
00109 
00110   ltime.reset();
00111   ltime.store(tmp, fractional_tmp);
00112 
00113   return 0;
00114 }
00115 
00116 } /* namespace drizzled */