~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Monty Taylor
  • Date: 2008-12-06 22:41:03 UTC
  • mto: (656.1.7 devel)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206224103-jdouqwt9hb0f01y1
Moved non-working tests into broken suite for easier running of working tests.

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/timediff.h>
 
23
#include <drizzled/item/timefunc.h>
 
24
 
 
25
/**
 
26
  TIMEDIFF(t,s) is a time function that calculates the
 
27
  time value between a start and end time.
 
28
 
 
29
  t and s: time_or_datetime_expression
 
30
  Result: Time value
 
31
*/
 
32
 
 
33
String *Item_func_timediff::val_str(String *str)
 
34
{
 
35
  assert(fixed == 1);
 
36
  int64_t seconds;
 
37
  long microseconds;
 
38
  int l_sign= 1;
 
39
  DRIZZLE_TIME l_time1 ,l_time2, l_time3;
 
40
 
 
41
  null_value= 0;
 
42
  if (args[0]->get_time(&l_time1) ||
 
43
      args[1]->get_time(&l_time2) ||
 
44
      l_time1.time_type != l_time2.time_type)
 
45
    goto null_date;
 
46
 
 
47
  if (l_time1.neg != l_time2.neg)
 
48
    l_sign= -l_sign;
 
49
 
 
50
  memset(&l_time3, 0, sizeof(l_time3));
 
51
 
 
52
  l_time3.neg= calc_time_diff(&l_time1, &l_time2, l_sign,
 
53
                              &seconds, &microseconds);
 
54
 
 
55
  /*
 
56
    For DRIZZLE_TIMESTAMP_TIME only:
 
57
      If first argument was negative and diff between arguments
 
58
      is non-zero we need to swap sign to get proper result.
 
59
  */
 
60
  if (l_time1.neg && (seconds || microseconds))
 
61
    l_time3.neg= 1-l_time3.neg;         // Swap sign of result
 
62
 
 
63
  calc_time_from_sec(&l_time3, (long) seconds, microseconds);
 
64
 
 
65
  if (!make_datetime_with_warn(l_time1.second_part || l_time2.second_part ?
 
66
                               TIME_MICROSECOND : TIME_ONLY,
 
67
                               &l_time3, str))
 
68
    return str;
 
69
null_date:
 
70
  null_value=1;
 
71
  return 0;
 
72
}
 
73