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