~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Moriyoshi Koizumi
  • Date: 2008-11-15 18:36:31 UTC
  • mto: (584.1.5 devel)
  • mto: This revision was merged to the branch mainline in revision 588.
  • Revision ID: mozo@mozo.jp-20081115183631-81d0clowyot42mk7
Incorporating changes proposed by mtaylor.

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