~drizzle-trunk/drizzle/development

574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/time/unix_timestamp.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
23
#include <drizzled/field/timestamp.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
24
#include <drizzled/session.h>
574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
25
907.1.7 by Jay Pipes
Merged in remove-timezone work
26
#include "drizzled/temporal.h"
27
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
28
namespace drizzled
29
{
30
574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
31
int64_t Item_func_unix_timestamp::val_int()
32
{
33
  DRIZZLE_TIME ltime;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
34
574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
35
  assert(fixed == 1);
36
  if (arg_count == 0)
37
    return (int64_t) current_session->query_start();
38
  if (args[0]->type() == FIELD_ITEM)
39
  {                                             // Optimize timestamp field
40
    Field *field=((Item_field*) args[0])->field;
41
    if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
42
      return ((Field_timestamp*) field)->get_timestamp(&null_value);
43
  }
44
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
45
  if (get_arg0_date(&ltime, 0))
574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
46
  {
47
    /*
48
      We have to set null_value again because get_arg0_date will also set it
49
      to true if we have wrong datetime parameter (and we should return 0 in
50
      this case).
51
    */
52
    null_value= args[0]->null_value;
53
    return 0;
54
  }
55
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
56
  Timestamp temporal;
907.1.7 by Jay Pipes
Merged in remove-timezone work
57
58
  temporal.set_years(ltime.year);
59
  temporal.set_months(ltime.month);
60
  temporal.set_days(ltime.day);
61
  temporal.set_hours(ltime.hour);
62
  temporal.set_minutes(ltime.minute);
63
  temporal.set_seconds(ltime.second);
64
  temporal.set_epoch_seconds();
65
66
  if (! temporal.is_valid())
67
  {
68
    null_value= true;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
69
    char buff[DateTime::MAX_STRING_LENGTH];
1079.3.1 by Stewart Smith
Change temporal to_string routines to use snprintf instead of sprintf.
70
    int buff_len;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
71
    buff_len= temporal.to_string(buff, DateTime::MAX_STRING_LENGTH);
72
    assert((buff_len+1) < DateTime::MAX_STRING_LENGTH);
907.1.7 by Jay Pipes
Merged in remove-timezone work
73
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), buff);
74
    return 0;
75
  }
76
77
  time_t tmp;
78
  temporal.to_time_t(&tmp);
79
80
  return (int64_t) tmp;
574.3.15 by Lee
moving functions from item_timefunc to functions/time directory
81
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
82
83
} /* namespace drizzled */