~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Andy Lester
  • Date: 2008-08-09 05:23:39 UTC
  • mto: (266.1.29 use-replace-funcs)
  • mto: This revision was merged to the branch mainline in revision 287.
  • Revision ID: andy@petdance.com-20080809052339-iafoesszmesweq6b
use NULL, not 0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
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"
26
 
 
27
 
#include <sstream>
28
 
#include <string>
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
void Item_func_from_unixtime::fix_length_and_dec()
34
 
{
35
 
  session= current_session;
36
 
  collation.set(&my_charset_bin);
37
 
  decimals= DATETIME_DEC;
38
 
  max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
39
 
  maybe_null= 1;
40
 
}
41
 
 
42
 
String *Item_func_from_unixtime::val_str(String *str)
43
 
{
44
 
  type::Time time_tmp;
45
 
 
46
 
  assert(fixed == 1);
47
 
 
48
 
  if (get_date(time_tmp, 0))
49
 
    return 0;
50
 
 
51
 
  if (str->alloc(type::Time::MAX_STRING_LENGTH))
52
 
  {
53
 
    null_value= 1;
54
 
    return 0;
55
 
  }
56
 
 
57
 
  time_tmp.convert(*str);
58
 
 
59
 
  return str;
60
 
}
61
 
 
62
 
int64_t Item_func_from_unixtime::val_int()
63
 
{
64
 
  type::Time time_tmp;
65
 
 
66
 
  assert(fixed == 1);
67
 
 
68
 
  if (get_date(time_tmp, 0))
69
 
    return 0;
70
 
 
71
 
  int64_t ret;
72
 
  time_tmp.convert(ret);
73
 
 
74
 
  return (int64_t) ret;
75
 
}
76
 
 
77
 
bool Item_func_from_unixtime::get_date(type::Time &ltime, uint32_t)
78
 
{
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
 
 
101
 
  /*
102
 
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
103
 
    from_unixtime() argument since tmp is unsigned.
104
 
  */
105
 
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
106
 
    return 1;
107
 
 
108
 
  ltime.reset();
109
 
  ltime.store(tmp, fractional_tmp);
110
 
 
111
 
  return 0;
112
 
}
113
 
 
114
 
} /* namespace drizzled */