~drizzle-trunk/drizzle/development

574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
21
#include <boost/lexical_cast.hpp>
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
22
#include "drizzled/function/time/from_unixtime.h"
23
#include "drizzled/session.h"
907.1.7 by Jay Pipes
Merged in remove-timezone work
24
#include "drizzled/temporal.h"
1237.9.8 by Monty Taylor
Fixed solaris build.
25
#include "drizzled/time_functions.h"
907.1.7 by Jay Pipes
Merged in remove-timezone work
26
27
#include <sstream>
28
#include <string>
29
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
30
namespace drizzled
31
{
32
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
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;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
38
  max_length=DateTime::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
39
  maybe_null= 1;
40
}
41
42
String *Item_func_from_unixtime::val_str(String *str)
43
{
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
44
  type::Time time_tmp;
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
45
46
  assert(fixed == 1);
47
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
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
57
  make_datetime(&time_tmp, str);
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
58
59
  return str;
60
}
61
62
int64_t Item_func_from_unixtime::val_int()
63
{
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
64
  type::Time time_tmp;
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
65
66
  assert(fixed == 1);
67
68
  if (get_date(&time_tmp, 0))
69
    return 0;
70
71
  return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
72
}
73
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
74
bool Item_func_from_unixtime::get_date(type::Time *ltime, uint32_t)
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
75
{
76
  uint64_t tmp= (uint64_t)(args[0]->val_int());
77
  /*
78
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
79
    from_unixtime() argument since tmp is unsigned.
80
  */
81
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
82
    return 1;
83
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
84
  Timestamp temporal;
907.1.7 by Jay Pipes
Merged in remove-timezone work
85
  if (! temporal.from_time_t((time_t) tmp))
86
  {
87
    null_value= true;
1775.5.1 by earney
modified files containing stringstream to use boost:lexical_cast instead as
88
    std::string tmp_string(boost::lexical_cast<std::string>(tmp));
907.1.7 by Jay Pipes
Merged in remove-timezone work
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;
574.3.18 by Lee
moving functions from item_timefunc to functions/time directory
102
103
  return 0;
104
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
105
106
} /* namespace drizzled */