~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/time/maketime.cc

  • Committer: Eric Herman
  • Date: 2008-12-07 15:29:44 UTC
  • mto: (656.1.14 devel)
  • mto: This revision was merged to the branch mainline in revision 670.
  • Revision ID: eric@mysql.com-20081207152944-cq1nx1cyi0huqj0f
Added pointer to online version of the FAQ

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/functions/time/maketime.h>
 
23
#include <drizzled/functions/time/make_time_with_warn.h>
 
24
 
 
25
/**
 
26
  MAKETIME(h,m,s) is a time function that calculates a time value
 
27
  from the total number of hours, minutes, and seconds.
 
28
  Result: Time value
 
29
*/
 
30
 
 
31
String *Item_func_maketime::val_str(String *str)
 
32
{
 
33
  assert(fixed == 1);
 
34
  DRIZZLE_TIME ltime;
 
35
  bool overflow= 0;
 
36
 
 
37
  int64_t hour=   args[0]->val_int();
 
38
  int64_t minute= args[1]->val_int();
 
39
  int64_t second= args[2]->val_int();
 
40
 
 
41
  if ((null_value=(args[0]->null_value ||
 
42
                   args[1]->null_value ||
 
43
                   args[2]->null_value ||
 
44
                   minute < 0 || minute > 59 ||
 
45
                   second < 0 || second > 59 ||
 
46
                   str->alloc(MAX_DATE_STRING_REP_LENGTH))))
 
47
    return 0;
 
48
 
 
49
  memset(&ltime, 0, sizeof(ltime));
 
50
  ltime.neg= 0;
 
51
 
 
52
  /* Check for integer overflows */
 
53
  if (hour < 0)
 
54
    ltime.neg= 1;
 
55
 
 
56
  if (-hour > UINT_MAX || hour > UINT_MAX)
 
57
    overflow= 1;
 
58
 
 
59
  if (!overflow)
 
60
  {
 
61
    ltime.hour=   (uint) ((hour < 0 ? -hour : hour));
 
62
    ltime.minute= (uint) minute;
 
63
    ltime.second= (uint) second;
 
64
  }
 
65
  else
 
66
  {
 
67
    ltime.hour= TIME_MAX_HOUR;
 
68
    ltime.minute= TIME_MAX_MINUTE;
 
69
    ltime.second= TIME_MAX_SECOND;
 
70
    char buf[28];
 
71
    char *ptr= int64_t10_to_str(hour, buf, args[0]->unsigned_flag ? 10 : -10);
 
72
    int len = (int)(ptr - buf) +
 
73
      sprintf(ptr, ":%02u:%02u", (uint)minute, (uint)second);
 
74
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
75
                                 buf, len, DRIZZLE_TIMESTAMP_TIME,
 
76
                                 NULL);
 
77
  }
 
78
 
 
79
  if (make_time_with_warn((DATE_TIME_FORMAT *) 0, &ltime, str))
 
80
  {
 
81
    null_value= 1;
 
82
    return 0;
 
83
  }
 
84
  return str;
 
85
}
 
86