Drizzled Public API Documentation

makedate.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <drizzled/function/time/makedate.h>
23 #include <drizzled/time_functions.h>
24 
25 namespace drizzled
26 {
27 
39 {
40  assert(fixed == 1);
41  type::Time l_time;
42  long daynr= (long) args[1]->val_int();
43  long year= (long) args[0]->val_int();
44  long days;
45 
46  if (args[0]->null_value || args[1]->null_value ||
47  year < 0 || daynr <= 0)
48  goto err;
49 
50  if (year < 100)
51  year= year_2000_handling(year);
52 
53  days= calc_daynr(year,1,1) + daynr - 1;
54  /* Day number from year 0 to 9999-12-31 */
55  if (days >= 0 && days <= MAX_DAY_NUMBER)
56  {
57  null_value=0;
58  get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
59  str->alloc(type::Time::MAX_STRING_LENGTH);
60 
61  l_time.convert(*str, type::DRIZZLE_TIMESTAMP_DATE);
62 
63  return str;
64  }
65 
66 err:
67  null_value=1;
68  return 0;
69 }
70 
71 
72 /*
73  MAKEDATE(a,b) is a date function that creates a date value
74  from a year and day value.
75 
76  NOTES:
77  As arguments are integers, we can't know if the year is a 2 digit or 4 digit year.
78  In this case we treat all years < 100 as 2 digit years. Ie, this is not safe
79  for dates between 0000-01-01 and 0099-12-31
80 */
81 
83 {
84  assert(fixed == 1);
85  type::Time l_time;
86  long daynr= (long) args[1]->val_int();
87  long year= (long) args[0]->val_int();
88  long days;
89 
90  if (args[0]->null_value || args[1]->null_value ||
91  year < 0 || daynr <= 0)
92  goto err;
93 
94  if (year < 100)
95  year= year_2000_handling(year);
96 
97  days= calc_daynr(year,1,1) + daynr - 1;
98  /* Day number from year 0 to 9999-12-31 */
99  if (days >= 0 && days < MAX_DAY_NUMBER)
100  {
101  null_value=0;
102  get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
103  return (int64_t) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
104  }
105 
106 err:
107  null_value= 1;
108  return 0;
109 }
110 
111 } /* namespace drizzled */
bool fixed
Definition: item.h:120
TODO: Rename this file - func.h is stupid.
bool null_value
Definition: item.h:122
String * val_str(String *str)
Definition: makedate.cc:38