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