~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-11-22 08:48:11 UTC
  • mfrom: (574.3.19 drizzle-clean-code)
  • mto: This revision was merged to the branch mainline in revision 603.
  • Revision ID: monty@inaugust.com-20081122084811-6crylm9iqj4loyjk
MergedĀ fromĀ Lee.

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