~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Merged in latest plugin-slot-reorg.

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
 
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 <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/function/time/from_unixtime.h>
 
23
#include <drizzled/session.h>
 
24
 
 
25
#include "drizzled/temporal.h"
 
26
 
 
27
#include <sstream>
 
28
#include <string>
 
29
 
 
30
void Item_func_from_unixtime::fix_length_and_dec()
 
31
{
 
32
  session= current_session;
 
33
  collation.set(&my_charset_bin);
 
34
  decimals= DATETIME_DEC;
 
35
  max_length=drizzled::DateTime::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
 
36
  maybe_null= 1;
 
37
}
 
38
 
 
39
String *Item_func_from_unixtime::val_str(String *str)
 
40
{
 
41
  DRIZZLE_TIME time_tmp;
 
42
 
 
43
  assert(fixed == 1);
 
44
 
 
45
  if (get_date(&time_tmp, 0))
 
46
    return 0;
 
47
 
 
48
  if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
 
49
  {
 
50
    null_value= 1;
 
51
    return 0;
 
52
  }
 
53
 
 
54
  make_datetime(&time_tmp, str);
 
55
 
 
56
  return str;
 
57
}
 
58
 
 
59
int64_t Item_func_from_unixtime::val_int()
 
60
{
 
61
  DRIZZLE_TIME time_tmp;
 
62
 
 
63
  assert(fixed == 1);
 
64
 
 
65
  if (get_date(&time_tmp, 0))
 
66
    return 0;
 
67
 
 
68
  return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
 
69
}
 
70
 
 
71
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime, uint32_t)
 
72
{
 
73
  uint64_t tmp= (uint64_t)(args[0]->val_int());
 
74
  /*
 
75
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
 
76
    from_unixtime() argument since tmp is unsigned.
 
77
  */
 
78
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
 
79
    return 1;
 
80
 
 
81
  drizzled::Timestamp temporal;
 
82
  if (! temporal.from_time_t((time_t) tmp))
 
83
  {
 
84
    null_value= true;
 
85
    std::stringstream ss;
 
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());
 
89
    return 0;
 
90
  }
 
91
  
 
92
  memset(ltime, 0, sizeof(*ltime));
 
93
 
 
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;
 
101
 
 
102
  return 0;
 
103
}