Drizzled Public API Documentation

makedate.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/makedate.h>
00023 #include <drizzled/time_functions.h>
00024 
00025 namespace drizzled
00026 {
00027 
00038 String *Item_func_makedate::val_str(String *str)
00039 {
00040   assert(fixed == 1);
00041   type::Time l_time;
00042   long daynr=  (long) args[1]->val_int();
00043   long year= (long) args[0]->val_int();
00044   long days;
00045 
00046   if (args[0]->null_value || args[1]->null_value ||
00047       year < 0 || daynr <= 0)
00048     goto err;
00049 
00050   if (year < 100)
00051     year= year_2000_handling(year);
00052 
00053   days= calc_daynr(year,1,1) + daynr - 1;
00054   /* Day number from year 0 to 9999-12-31 */
00055   if (days >= 0 && days <= MAX_DAY_NUMBER)
00056   {
00057     null_value=0;
00058     get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
00059     if (str->alloc(type::Time::MAX_STRING_LENGTH))
00060       goto err;
00061 
00062     l_time.convert(*str, type::DRIZZLE_TIMESTAMP_DATE);
00063 
00064     return str;
00065   }
00066 
00067 err:
00068   null_value=1;
00069   return 0;
00070 }
00071 
00072 
00073 /*
00074   MAKEDATE(a,b) is a date function that creates a date value
00075   from a year and day value.
00076 
00077   NOTES:
00078     As arguments are integers, we can't know if the year is a 2 digit or 4 digit year.
00079     In this case we treat all years < 100 as 2 digit years. Ie, this is not safe
00080     for dates between 0000-01-01 and 0099-12-31
00081 */
00082 
00083 int64_t Item_func_makedate::val_int()
00084 {
00085   assert(fixed == 1);
00086   type::Time l_time;
00087   long daynr=  (long) args[1]->val_int();
00088   long year= (long) args[0]->val_int();
00089   long days;
00090 
00091   if (args[0]->null_value || args[1]->null_value ||
00092       year < 0 || daynr <= 0)
00093     goto err;
00094 
00095   if (year < 100)
00096     year= year_2000_handling(year);
00097 
00098   days= calc_daynr(year,1,1) + daynr - 1;
00099   /* Day number from year 0 to 9999-12-31 */
00100   if (days >= 0 && days < MAX_DAY_NUMBER)
00101   {
00102     null_value=0;
00103     get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
00104     return (int64_t) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
00105   }
00106 
00107 err:
00108   null_value= 1;
00109   return 0;
00110 }
00111 
00112 } /* namespace drizzled */