~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/time/from_unixtime.cc

  • Committer: Monty Taylor
  • Date: 2010-12-26 03:15:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2038.
  • Revision ID: mordred@inaugust.com-20101226031544-1cf3raipu53fnmyj
Through page.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
  session= current_session;
36
36
  collation.set(&my_charset_bin);
37
37
  decimals= DATETIME_DEC;
38
 
  max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
 
38
  max_length=DateTime::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
39
39
  maybe_null= 1;
40
40
}
41
41
 
42
42
String *Item_func_from_unixtime::val_str(String *str)
43
43
{
44
 
  type::Time time_tmp;
 
44
  DRIZZLE_TIME time_tmp;
45
45
 
46
46
  assert(fixed == 1);
47
47
 
48
 
  if (get_date(time_tmp, 0))
 
48
  if (get_date(&time_tmp, 0))
49
49
    return 0;
50
50
 
51
 
  if (str->alloc(type::Time::MAX_STRING_LENGTH))
 
51
  if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
52
52
  {
53
53
    null_value= 1;
54
54
    return 0;
55
55
  }
56
56
 
57
 
  time_tmp.convert(*str);
 
57
  make_datetime(&time_tmp, str);
58
58
 
59
59
  return str;
60
60
}
61
61
 
62
62
int64_t Item_func_from_unixtime::val_int()
63
63
{
64
 
  type::Time time_tmp;
 
64
  DRIZZLE_TIME time_tmp;
65
65
 
66
66
  assert(fixed == 1);
67
67
 
68
 
  if (get_date(time_tmp, 0))
 
68
  if (get_date(&time_tmp, 0))
69
69
    return 0;
70
70
 
71
 
  int64_t ret;
72
 
  time_tmp.convert(ret);
73
 
 
74
 
  return (int64_t) ret;
 
71
  return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
75
72
}
76
73
 
77
 
bool Item_func_from_unixtime::get_date(type::Time &ltime, uint32_t)
 
74
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime, uint32_t)
78
75
{
79
 
  uint64_t tmp= 0;
80
 
  type::Time::usec_t fractional_tmp= 0;
81
 
 
82
 
  switch (args[0]->result_type()) {
83
 
  case REAL_RESULT:
84
 
  case ROW_RESULT:
85
 
  case DECIMAL_RESULT:
86
 
  case STRING_RESULT:
87
 
    {
88
 
      double double_tmp= args[0]->val_real();
89
 
 
90
 
      tmp= (uint64_t)(double_tmp);
91
 
      fractional_tmp=  (type::Time::usec_t)((uint64_t)((double_tmp - tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS);
92
 
 
93
 
      break;
94
 
    }
95
 
 
96
 
  case INT_RESULT:
97
 
    tmp= (uint64_t)(args[0]->val_int());
98
 
    break;
99
 
  }
100
 
 
 
76
  uint64_t tmp= (uint64_t)(args[0]->val_int());
101
77
  /*
102
78
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
103
79
    from_unixtime() argument since tmp is unsigned.
105
81
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
106
82
    return 1;
107
83
 
108
 
  ltime.reset();
109
 
  ltime.store(tmp, fractional_tmp);
 
84
  Timestamp temporal;
 
85
  if (! temporal.from_time_t((time_t) tmp))
 
86
  {
 
87
    null_value= true;
 
88
    std::string tmp_string(boost::lexical_cast<std::string>(tmp));
 
89
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), tmp_string.c_str());
 
90
    return 0;
 
91
  }
 
92
  
 
93
  memset(ltime, 0, sizeof(*ltime));
 
94
 
 
95
  ltime->year= temporal.years();
 
96
  ltime->month= temporal.months();
 
97
  ltime->day= temporal.days();
 
98
  ltime->hour= temporal.hours();
 
99
  ltime->minute= temporal.minutes();
 
100
  ltime->second= temporal.seconds();
 
101
  ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
110
102
 
111
103
  return 0;
112
104
}