Drizzled Public API Documentation

dayofyear.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 #include <drizzled/temporal.h>
00022 #include <drizzled/error.h>
00023 #include <drizzled/calendar.h>
00024 #include <drizzled/function/time/dayofyear.h>
00025 #include <drizzled/internal/my_sys.h>
00026 
00027 namespace drizzled
00028 {
00029 
00030 int64_t Item_func_dayofyear::val_int()
00031 {
00032   assert(fixed);
00033 
00034   if (args[0]->is_null())
00035   {
00036     /* For NULL argument, we return a NULL result */
00037     null_value= true;
00038     return 0;
00039   }
00040 
00041   /* Grab the first argument as a DateTime object */
00042   DateTime temporal;
00043   Item_result arg0_result_type= args[0]->result_type();
00044   
00045   switch (arg0_result_type)
00046   {
00047     case DECIMAL_RESULT: 
00048       /* 
00049        * For doubles supplied, interpret the arg as a string, 
00050        * so intentionally fall-through here...
00051        * This allows us to accept double parameters like 
00052        * 19971231235959.01 and interpret it the way MySQL does:
00053        * as a TIMESTAMP-like thing with a microsecond component.
00054        * Ugh, but need to keep backwards-compat.
00055        */
00056     case STRING_RESULT:
00057       {
00058         char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00059         String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00060         String *res= args[0]->val_str(&tmp);
00061 
00062         if (res && (res != &tmp))
00063         {
00064           tmp.copy(*res);
00065         }
00066 
00067         if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
00068         {
00069           /* 
00070           * Could not interpret the function argument as a temporal value, 
00071           * so throw an error and return 0
00072           */
00073           my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00074           return 0;
00075         }
00076       }
00077       break;
00078     case INT_RESULT:
00079       if (temporal.from_int64_t(args[0]->val_int()))
00080         break;
00081       /* Intentionally fall-through on invalid conversion from integer */
00082     default:
00083       {
00084         /* 
00085         * Could not interpret the function argument as a temporal value, 
00086         * so throw an error and return 0
00087         */
00088         null_value= true;
00089         char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
00090         String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
00091         String *res;
00092 
00093         res= args[0]->val_str(&tmp);
00094 
00095         if (res && (res != &tmp))
00096         {
00097           tmp.copy(*res);
00098         }
00099 
00100         my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
00101         return 0;
00102       }
00103   }
00104   return (int64_t) julian_day_number_from_gregorian_date(temporal.years(), temporal.months(), temporal.days())
00105                  - julian_day_number_from_gregorian_date(temporal.years(), 1, 1) + 1;
00106 }
00107 
00108 } /* namespace drizzled */