~drizzle-trunk/drizzle/development

574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
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 <drizzled/server_includes.h>
21
#include CSTDINT_H
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/time/makedate.h>
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
23
24
/**
25
  MAKEDATE(a,b) is a date function that creates a date value
26
  from a year and day value.
27
28
  NOTES:
29
    As arguments are integers, we can't know if the year is a 2 digit or 4 digit year.
30
    In this case we treat all years < 100 as 2 digit years. Ie, this is not safe
31
    for dates between 0000-01-01 and 0099-12-31
32
*/
33
34
String *Item_func_makedate::val_str(String *str)
35
{
36
  assert(fixed == 1);
37
  DRIZZLE_TIME l_time;
38
  long daynr=  (long) args[1]->val_int();
39
  long year= (long) args[0]->val_int();
40
  long days;
41
42
  if (args[0]->null_value || args[1]->null_value ||
43
      year < 0 || daynr <= 0)
44
    goto err;
45
46
  if (year < 100)
47
    year= year_2000_handling(year);
48
49
  days= calc_daynr(year,1,1) + daynr - 1;
50
  /* Day number from year 0 to 9999-12-31 */
51
  if (days >= 0 && days <= MAX_DAY_NUMBER)
52
  {
53
    null_value=0;
54
    get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
55
    if (str->alloc(MAX_DATE_STRING_REP_LENGTH))
56
      goto err;
57
    make_date((DATE_TIME_FORMAT *) 0, &l_time, str);
58
    return str;
59
  }
60
61
err:
62
  null_value=1;
63
  return 0;
64
}
65
66
67
/*
68
  MAKEDATE(a,b) is a date function that creates a date value
69
  from a year and day value.
70
71
  NOTES:
72
    As arguments are integers, we can't know if the year is a 2 digit or 4 digit year.
73
    In this case we treat all years < 100 as 2 digit years. Ie, this is not safe
74
    for dates between 0000-01-01 and 0099-12-31
75
*/
76
77
int64_t Item_func_makedate::val_int()
78
{
79
  assert(fixed == 1);
80
  DRIZZLE_TIME l_time;
81
  long daynr=  (long) args[1]->val_int();
82
  long year= (long) args[0]->val_int();
83
  long days;
84
85
  if (args[0]->null_value || args[1]->null_value ||
86
      year < 0 || daynr <= 0)
87
    goto err;
88
89
  if (year < 100)
90
    year= year_2000_handling(year);
91
92
  days= calc_daynr(year,1,1) + daynr - 1;
93
  /* Day number from year 0 to 9999-12-31 */
94
  if (days >= 0 && days < MAX_DAY_NUMBER)
95
  {
96
    null_value=0;
97
    get_date_from_daynr(days,&l_time.year,&l_time.month,&l_time.day);
98
    return (int64_t) (l_time.year * 10000L + l_time.month * 100 + l_time.day);
99
  }
100
101
err:
102
  null_value= 1;
103
  return 0;
104
}
105