~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: patrick crews
  • Date: 2011-06-08 03:02:27 UTC
  • mto: This revision was merged to the branch mainline in revision 2329.
  • Revision ID: gleebix@gmail.com-20110608030227-updkyv2652zvfajc
Initial voodoo worked to give us a crashme mode.  Need docs still

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
 
20
#include <config.h>
21
21
#include <boost/lexical_cast.hpp>
22
 
#include "drizzled/function/time/from_unixtime.h"
23
 
#include "drizzled/session.h"
24
 
#include "drizzled/temporal.h"
25
 
#include "drizzled/time_functions.h"
 
22
#include <drizzled/function/time/from_unixtime.h>
 
23
#include <drizzled/current_session.h>
 
24
#include <drizzled/session.h>
 
25
#include <drizzled/temporal.h>
 
26
#include <drizzled/time_functions.h>
 
27
#include <drizzled/field.h>
26
28
 
27
29
#include <sstream>
28
30
#include <string>
35
37
  session= current_session;
36
38
  collation.set(&my_charset_bin);
37
39
  decimals= DATETIME_DEC;
38
 
  max_length=DateTime::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
 
40
  max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
39
41
  maybe_null= 1;
40
42
}
41
43
 
42
44
String *Item_func_from_unixtime::val_str(String *str)
43
45
{
44
 
  DRIZZLE_TIME time_tmp;
 
46
  type::Time time_tmp;
45
47
 
46
48
  assert(fixed == 1);
47
49
 
48
 
  if (get_date(&time_tmp, 0))
49
 
    return 0;
50
 
 
51
 
  if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
52
 
  {
53
 
    null_value= 1;
54
 
    return 0;
55
 
  }
56
 
 
57
 
  make_datetime(&time_tmp, str);
 
50
  if (get_date(time_tmp, 0))
 
51
    return 0;
 
52
 
 
53
  str->alloc(type::Time::MAX_STRING_LENGTH);
 
54
 
 
55
  time_tmp.convert(*str);
58
56
 
59
57
  return str;
60
58
}
61
59
 
62
60
int64_t Item_func_from_unixtime::val_int()
63
61
{
64
 
  DRIZZLE_TIME time_tmp;
 
62
  type::Time time_tmp;
65
63
 
66
64
  assert(fixed == 1);
67
65
 
68
 
  if (get_date(&time_tmp, 0))
 
66
  if (get_date(time_tmp, 0))
69
67
    return 0;
70
68
 
71
 
  return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
 
69
  int64_t ret;
 
70
  time_tmp.convert(ret);
 
71
 
 
72
  return (int64_t) ret;
72
73
}
73
74
 
74
 
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime, uint32_t)
 
75
bool Item_func_from_unixtime::get_date(type::Time &ltime, uint32_t)
75
76
{
76
 
  uint64_t tmp= (uint64_t)(args[0]->val_int());
 
77
  uint64_t tmp= 0;
 
78
  type::Time::usec_t fractional_tmp= 0;
 
79
 
 
80
  switch (args[0]->result_type()) {
 
81
  case REAL_RESULT:
 
82
  case ROW_RESULT:
 
83
  case DECIMAL_RESULT:
 
84
  case STRING_RESULT:
 
85
    {
 
86
      double double_tmp= args[0]->val_real();
 
87
 
 
88
      tmp= (uint64_t)(double_tmp);
 
89
      fractional_tmp=  (type::Time::usec_t)((uint64_t)((double_tmp - tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS);
 
90
 
 
91
      break;
 
92
    }
 
93
 
 
94
  case INT_RESULT:
 
95
    tmp= (uint64_t)(args[0]->val_int());
 
96
    break;
 
97
  }
 
98
 
77
99
  /*
78
100
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
79
101
    from_unixtime() argument since tmp is unsigned.
81
103
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
82
104
    return 1;
83
105
 
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;
 
106
  ltime.reset();
 
107
  ltime.store(tmp, fractional_tmp);
102
108
 
103
109
  return 0;
104
110
}