1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
4
* Copyright (C) 2008 Sun Microsystems
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
21
#include <boost/lexical_cast.hpp>
22
#include "drizzled/function/time/from_unixtime.h"
23
#include "drizzled/session.h"
20
#include <drizzled/server_includes.h>
22
#include <drizzled/function/time/from_unixtime.h>
23
#include <drizzled/session.h>
24
25
#include "drizzled/temporal.h"
25
#include "drizzled/time_functions.h"
33
30
void Item_func_from_unixtime::fix_length_and_dec()
35
32
session= current_session;
36
33
collation.set(&my_charset_bin);
37
34
decimals= DATETIME_DEC;
38
max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
35
max_length=drizzled::DateTime::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
42
39
String *Item_func_from_unixtime::val_str(String *str)
41
DRIZZLE_TIME time_tmp;
46
43
assert(fixed == 1);
48
if (get_date(time_tmp, 0))
45
if (get_date(&time_tmp, 0))
51
if (str->alloc(type::Time::MAX_STRING_LENGTH))
48
if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
57
time_tmp.convert(*str);
54
make_datetime(&time_tmp, str);
62
59
int64_t Item_func_from_unixtime::val_int()
61
DRIZZLE_TIME time_tmp;
66
63
assert(fixed == 1);
68
if (get_date(time_tmp, 0))
65
if (get_date(&time_tmp, 0))
72
time_tmp.convert(ret);
68
return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
77
bool Item_func_from_unixtime::get_date(type::Time <ime, uint32_t)
71
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime, uint32_t)
80
type::Time::usec_t fractional_tmp= 0;
82
switch (args[0]->result_type()) {
88
double double_tmp= args[0]->val_real();
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);
97
tmp= (uint64_t)(args[0]->val_int());
73
uint64_t tmp= (uint64_t)(args[0]->val_int());
102
75
"tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
103
76
from_unixtime() argument since tmp is unsigned.
105
78
if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
109
ltime.store(tmp, fractional_tmp);
81
drizzled::Timestamp temporal;
82
if (! temporal.from_time_t((time_t) tmp))
86
std::string tmp_string;
87
ss << tmp; ss >> tmp_string;
88
my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), tmp_string.c_str());
92
memset(ltime, 0, sizeof(*ltime));
94
ltime->year= temporal.years();
95
ltime->month= temporal.months();
96
ltime->day= temporal.days();
97
ltime->hour= temporal.hours();
98
ltime->minute= temporal.minutes();
99
ltime->second= temporal.seconds();
100
ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
114
} /* namespace drizzled */