~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Phase 1 - temporal changes

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