~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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/function/time/unix_timestamp.h>
23
 
#include <drizzled/field/timestamp.h>
24
 
#include <drizzled/session.h>
25
 
 
26
 
#include "drizzled/temporal.h"
27
 
 
28
 
int64_t Item_func_unix_timestamp::val_int()
29
 
{
30
 
  DRIZZLE_TIME ltime;
31
 
 
32
 
  assert(fixed == 1);
33
 
  if (arg_count == 0)
34
 
    return (int64_t) current_session->query_start();
35
 
  if (args[0]->type() == FIELD_ITEM)
36
 
  {                                             // Optimize timestamp field
37
 
    Field *field=((Item_field*) args[0])->field;
38
 
    if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
39
 
      return ((Field_timestamp*) field)->get_timestamp(&null_value);
40
 
  }
41
 
 
42
 
  if (get_arg0_date(&ltime, 0))
43
 
  {
44
 
    /*
45
 
      We have to set null_value again because get_arg0_date will also set it
46
 
      to true if we have wrong datetime parameter (and we should return 0 in
47
 
      this case).
48
 
    */
49
 
    null_value= args[0]->null_value;
50
 
    return 0;
51
 
  }
52
 
 
53
 
  drizzled::Timestamp temporal;
54
 
 
55
 
  temporal.set_years(ltime.year);
56
 
  temporal.set_months(ltime.month);
57
 
  temporal.set_days(ltime.day);
58
 
  temporal.set_hours(ltime.hour);
59
 
  temporal.set_minutes(ltime.minute);
60
 
  temporal.set_seconds(ltime.second);
61
 
  temporal.set_epoch_seconds();
62
 
 
63
 
  if (! temporal.is_valid())
64
 
  {
65
 
    null_value= true;
66
 
    char buff[drizzled::DateTime::MAX_STRING_LENGTH];
67
 
    int buff_len;
68
 
    buff_len= temporal.to_string(buff, drizzled::DateTime::MAX_STRING_LENGTH);
69
 
    assert((buff_len+1) < drizzled::DateTime::MAX_STRING_LENGTH);
70
 
    my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), buff);
71
 
    return 0;
72
 
  }
73
 
 
74
 
  time_t tmp;
75
 
  temporal.to_time_t(&tmp);
76
 
 
77
 
  return (int64_t) tmp;
78
 
}