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 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 <ime, 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
00105
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 }