~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2009-03-08 23:45:12 UTC
  • mto: (923.2.1 mordred)
  • mto: This revision was merged to the branch mainline in revision 921.
  • Revision ID: mordred@inaugust.com-20090308234512-tqkygxtu1iaig23s
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!

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, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
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>
21
 
#include <boost/lexical_cast.hpp>
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
22
22
#include <drizzled/function/time/from_unixtime.h>
23
 
#include <drizzled/current_session.h>
24
23
#include <drizzled/session.h>
25
 
#include <drizzled/temporal.h>
26
 
#include <drizzled/time_functions.h>
 
24
 
 
25
#include "drizzled/temporal.h"
27
26
 
28
27
#include <sstream>
29
28
#include <string>
30
29
 
31
 
namespace drizzled
32
 
{
33
 
 
34
30
void Item_func_from_unixtime::fix_length_and_dec()
35
31
{
36
32
  session= current_session;
37
33
  collation.set(&my_charset_bin);
38
34
  decimals= DATETIME_DEC;
39
 
  max_length=type::Time::MAX_STRING_LENGTH*MY_CHARSET_BIN_MB_MAXLEN;
 
35
  max_length=MAX_DATETIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
40
36
  maybe_null= 1;
41
37
}
42
38
 
43
39
String *Item_func_from_unixtime::val_str(String *str)
44
40
{
45
 
  type::Time time_tmp;
 
41
  DRIZZLE_TIME time_tmp;
46
42
 
47
43
  assert(fixed == 1);
48
44
 
49
 
  if (get_date(time_tmp, 0))
 
45
  if (get_date(&time_tmp, 0))
50
46
    return 0;
51
47
 
52
 
  if (str->alloc(type::Time::MAX_STRING_LENGTH))
 
48
  if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
53
49
  {
54
50
    null_value= 1;
55
51
    return 0;
56
52
  }
57
53
 
58
 
  time_tmp.convert(*str);
 
54
  make_datetime(&time_tmp, str);
59
55
 
60
56
  return str;
61
57
}
62
58
 
63
59
int64_t Item_func_from_unixtime::val_int()
64
60
{
65
 
  type::Time time_tmp;
 
61
  DRIZZLE_TIME time_tmp;
66
62
 
67
63
  assert(fixed == 1);
68
64
 
69
 
  if (get_date(time_tmp, 0))
 
65
  if (get_date(&time_tmp, 0))
70
66
    return 0;
71
67
 
72
 
  int64_t ret;
73
 
  time_tmp.convert(ret);
74
 
 
75
 
  return (int64_t) ret;
 
68
  return (int64_t) TIME_to_uint64_t_datetime(&time_tmp);
76
69
}
77
70
 
78
 
bool Item_func_from_unixtime::get_date(type::Time &ltime, uint32_t)
 
71
bool Item_func_from_unixtime::get_date(DRIZZLE_TIME *ltime, uint32_t)
79
72
{
80
 
  uint64_t tmp= 0;
81
 
  type::Time::usec_t fractional_tmp= 0;
82
 
 
83
 
  switch (args[0]->result_type()) {
84
 
  case REAL_RESULT:
85
 
  case ROW_RESULT:
86
 
  case DECIMAL_RESULT:
87
 
  case STRING_RESULT:
88
 
    {
89
 
      double double_tmp= args[0]->val_real();
90
 
 
91
 
      tmp= (uint64_t)(double_tmp);
92
 
      fractional_tmp=  (type::Time::usec_t)((uint64_t)((double_tmp - tmp) * type::Time::FRACTIONAL_DIGITS) % type::Time::FRACTIONAL_DIGITS);
93
 
 
94
 
      break;
95
 
    }
96
 
 
97
 
  case INT_RESULT:
98
 
    tmp= (uint64_t)(args[0]->val_int());
99
 
    break;
100
 
  }
101
 
 
 
73
  uint64_t tmp= (uint64_t)(args[0]->val_int());
102
74
  /*
103
75
    "tmp > TIMESTAMP_MAX_VALUE" check also covers case of negative
104
76
    from_unixtime() argument since tmp is unsigned.
106
78
  if ((null_value= (args[0]->null_value || tmp > TIMESTAMP_MAX_VALUE)))
107
79
    return 1;
108
80
 
109
 
  ltime.reset();
110
 
  ltime.store(tmp, fractional_tmp);
 
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;
111
101
 
112
102
  return 0;
113
103
}
114
 
 
115
 
} /* namespace drizzled */