Drizzled Public API Documentation

unix_timestamp.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <drizzled/function/time/unix_timestamp.h>
00023 #include <drizzled/field/epoch.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/temporal.h>
00026 #include <drizzled/item/field.h>
00027 
00028 namespace drizzled {
00029 
00030 int64_t Item_func_unix_timestamp::val_int()
00031 {
00032   type::Time ltime;
00033 
00034   assert(fixed == 1);
00035   if (arg_count == 0)
00036     return (int64_t) getSession().getCurrentTimestampEpoch();
00037 
00038   if (args[0]->type() == FIELD_ITEM)
00039   {                                             // Optimize timestamp field
00040     Field *field=((Item_field*) args[0])->field;
00041     if (field->is_timestamp())
00042       return ((field::Epoch::pointer) field)->get_timestamp(&null_value);
00043   }
00044 
00045   if (get_arg0_date(ltime, 0))
00046   {
00047     /*
00048       We have to set null_value again because get_arg0_date will also set it
00049       to true if we have wrong datetime parameter (and we should return 0 in
00050       this case).
00051     */
00052     null_value= args[0]->null_value;
00053     return 0;
00054   }
00055 
00056   Timestamp temporal;
00057 
00058   temporal.set_years(ltime.year);
00059   temporal.set_months(ltime.month);
00060   temporal.set_days(ltime.day);
00061   temporal.set_hours(ltime.hour);
00062   temporal.set_minutes(ltime.minute);
00063   temporal.set_seconds(ltime.second);
00064   temporal.set_epoch_seconds();
00065 
00066   if (! temporal.is_valid())
00067   {
00068     null_value= true;
00069     char buff[DateTime::MAX_STRING_LENGTH];
00070     int buff_len;
00071     buff_len= temporal.to_string(buff, DateTime::MAX_STRING_LENGTH);
00072     assert((buff_len+1) < DateTime::MAX_STRING_LENGTH);
00073     my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), buff);
00074     return 0;
00075   }
00076 
00077   time_t tmp;
00078   temporal.to_time_t(tmp);
00079 
00080   return (int64_t) tmp;
00081 }
00082 
00083 } /* namespace drizzled */