~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/time/makedate.cc

Renamed more stuff to drizzle.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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
 
 
28
 
/**
29
 
  MAKEDATE(a,b) is a date function that creates a date value
30
 
  from a year and day value.
31
 
 
32
 
  NOTES:
33
 
    As arguments are integers, we can't know if the year is a 2 digit or 4 digit year.
34
 
    In this case we treat all years < 100 as 2 digit years. Ie, this is not safe
35
 
    for dates between 0000-01-01 and 0099-12-31
36
 
*/
37
 
 
38
 
String *Item_func_makedate::val_str(String *str)
39
 
{
40
 
  assert(fixed == 1);
41
 
  DRIZZLE_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
 
    if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
60
 
      goto err;
61
 
    make_date(&l_time, str);
62
 
    return str;
63
 
  }
64
 
 
65
 
err:
66
 
  null_value=1;
67
 
  return 0;
68
 
}
69
 
 
70
 
 
71
 
/*
72
 
  MAKEDATE(a,b) is a date function that creates a date value
73
 
  from a year and day value.
74
 
 
75
 
  NOTES:
76
 
    As arguments are integers, we can't know if the year is a 2 digit or 4 digit year.
77
 
    In this case we treat all years < 100 as 2 digit years. Ie, this is not safe
78
 
    for dates between 0000-01-01 and 0099-12-31
79
 
*/
80
 
 
81
 
int64_t Item_func_makedate::val_int()
82
 
{
83
 
  assert(fixed == 1);
84
 
  DRIZZLE_TIME l_time;
85
 
  long daynr=  (long) args[1]->val_int();
86
 
  long year= (long) args[0]->val_int();
87
 
  long days;
88
 
 
89
 
  if (args[0]->null_value || args[1]->null_value ||
90
 
      year < 0 || daynr <= 0)
91
 
    goto err;
92
 
 
93
 
  if (year < 100)
94
 
    year= year_2000_handling(year);
95
 
 
96
 
  days= calc_daynr(year,1,1) + daynr - 1;
97
 
  /* Day number from year 0 to 9999-12-31 */
98
 
  if (days >= 0 && days < MAX_DAY_NUMBER)
99
 
  {
100
 
    null_value=0;
101
 
    get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
102
 
    return (int64_t) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
103
 
  }
104
 
 
105
 
err:
106
 
  null_value= 1;
107
 
  return 0;
108
 
}
109
 
 
110
 
} /* namespace drizzled */