~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/time/sec_to_time.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/sec_to_time.h>
 
23
 
 
24
/*
 
25
  Convert seconds to DRIZZLE_TIME value with overflow checking
 
26
 
 
27
  SYNOPSIS:
 
28
    sec_to_time()
 
29
    seconds          number of seconds
 
30
    unsigned_flag    1, if 'seconds' is unsigned, 0, otherwise
 
31
    ltime            output DRIZZLE_TIME value
 
32
 
 
33
  DESCRIPTION
 
34
    If the 'seconds' argument is inside DRIZZLE_TIME data range, convert it to a
 
35
    corresponding value.
 
36
    Otherwise, truncate the resulting value to the nearest endpoint, and
 
37
    produce a warning message.
 
38
 
 
39
  RETURN
 
40
    1                if the value was truncated during conversion
 
41
    0                otherwise
 
42
*/
 
43
 
 
44
static bool sec_to_time(int64_t seconds, bool unsigned_flag, DRIZZLE_TIME *ltime)
 
45
{
 
46
  uint32_t sec;
 
47
 
 
48
  memset(ltime, 0, sizeof(*ltime));
 
49
 
 
50
  if (seconds < 0)
 
51
  {
 
52
    ltime->neg= 1;
 
53
    if (seconds < -3020399)
 
54
      goto overflow;
 
55
    seconds= -seconds;
 
56
  }
 
57
  else if (seconds > 3020399)
 
58
    goto overflow;
 
59
 
 
60
  sec= (uint) ((uint64_t) seconds % 3600);
 
61
  ltime->hour= (uint) (seconds/3600);
 
62
  ltime->minute= sec/60;
 
63
  ltime->second= sec % 60;
 
64
 
 
65
  return 0;
 
66
 
 
67
overflow:
 
68
  ltime->hour= TIME_MAX_HOUR;
 
69
  ltime->minute= TIME_MAX_MINUTE;
 
70
  ltime->second= TIME_MAX_SECOND;
 
71
 
 
72
  char buf[22];
 
73
  int len= (int)(int64_t10_to_str(seconds, buf, unsigned_flag ? 10 : -10)
 
74
                 - buf);
 
75
  make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
76
                               buf, len, DRIZZLE_TIMESTAMP_TIME,
 
77
                               NULL);
 
78
 
 
79
  return 1;
 
80
}
 
81
 
 
82
String *Item_func_sec_to_time::val_str(String *str)
 
83
{
 
84
  assert(fixed == 1);
 
85
  DRIZZLE_TIME ltime;
 
86
  int64_t arg_val= args[0]->val_int();
 
87
 
 
88
  if ((null_value=args[0]->null_value) ||
 
89
      str->alloc(MAX_DATE_STRING_REP_LENGTH))
 
90
  {
 
91
    null_value= 1;
 
92
    return (String*) 0;
 
93
  }
 
94
 
 
95
  sec_to_time(arg_val, args[0]->unsigned_flag, &ltime);
 
96
 
 
97
  make_time((DATE_TIME_FORMAT *) 0, &ltime, str);
 
98
  return str;
 
99
}
 
100
 
 
101
int64_t Item_func_sec_to_time::val_int()
 
102
{
 
103
  assert(fixed == 1);
 
104
  DRIZZLE_TIME ltime;
 
105
  int64_t arg_val= args[0]->val_int();
 
106
 
 
107
  if ((null_value=args[0]->null_value))
 
108
    return 0;
 
109
 
 
110
  sec_to_time(arg_val, args[0]->unsigned_flag, &ltime);
 
111
 
 
112
  return (ltime.neg ? -1 : 1) *
 
113
    ((ltime.hour)*10000 + ltime.minute*100 + ltime.second);
 
114
}
 
115
 
 
116