00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00037 null_value= true;
00038 return 0;
00039 }
00040
00041
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
00050
00051
00052
00053
00054
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
00071
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
00082 default:
00083 {
00084
00085
00086
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 }