drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
unix_timestamp.cc
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, Inc.
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 <config.h>
21
22#include <drizzled/function/time/unix_timestamp.h>
23#include <drizzled/field/epoch.h>
24#include <drizzled/session.h>
25#include <drizzled/session/times.h>
26#include <drizzled/temporal.h>
27#include <drizzled/item/field.h>
28
29namespace drizzled {
30
32{
33 type::Time ltime;
34
35 assert(fixed == 1);
36 if (arg_count == 0)
37 return (int64_t) getSession().times.getCurrentTimestampEpoch();
38
39 if (args[0]->type() == FIELD_ITEM)
40 { // Optimize timestamp field
41 Field *field=((Item_field*) args[0])->field;
42 if (field->is_timestamp())
43 return ((field::Epoch::pointer) field)->get_timestamp(&null_value);
44 }
45
46 if (get_arg0_date(ltime, 0))
47 {
48 /*
49 We have to set null_value again because get_arg0_date will also set it
50 to true if we have wrong datetime parameter (and we should return 0 in
51 this case).
52 */
53 null_value= args[0]->null_value;
54 return 0;
55 }
56
57 Timestamp temporal;
58
59 temporal.set_years(ltime.year);
60 temporal.set_months(ltime.month);
61 temporal.set_days(ltime.day);
62 temporal.set_hours(ltime.hour);
63 temporal.set_minutes(ltime.minute);
64 temporal.set_seconds(ltime.second);
65 temporal.set_epoch_seconds();
66
67 if (! temporal.is_valid())
68 {
69 null_value= true;
71 int buff_len;
72 buff_len= temporal.to_string(buff, DateTime::MAX_STRING_LENGTH);
73 (void)(buff_len);
74 assert((buff_len+1) < DateTime::MAX_STRING_LENGTH);
75 my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), buff);
76 return 0;
77 }
78
79 time_t tmp;
80 temporal.to_time_t(tmp);
81
82 return (int64_t) tmp;
83}
84
85} /* namespace drizzled */
virtual int to_string(char *to, size_t to_len) const
Definition temporal.cc:961
static const int MAX_STRING_LENGTH
Definition temporal.h:608
bool fixed
Definition item.h:122
bool null_value
Definition item.h:124
void set_seconds(const uint32_t second)
Definition temporal.h:128
void set_hours(const uint32_t hour)
Definition temporal.h:136
void set_days(const uint32_t day)
Definition temporal.h:140
void set_minutes(const uint32_t minute)
Definition temporal.h:132
void set_epoch_seconds()
Definition temporal.cc:85
void set_months(const uint32_t month)
Definition temporal.h:144
void set_years(const uint32_t year)
Definition temporal.h:148
virtual bool is_valid() const
Definition temporal.cc:1355
void to_time_t(time_t &to) const
Definition temporal.cc:1298
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.