00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 <ime, 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
00101
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 }