~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
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"
1237.9.8 by Monty Taylor
Fixed solaris build.
23
#include "drizzled/time_functions.h"
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
24
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
25
namespace drizzled
26
{
27
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
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);
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
41
  type::Time l_time;
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
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);
2088.8.11 by Brian Aker
Fix additional output, swaps for the valus.
59
    if (str->alloc(type::Time::MAX_STRING_LENGTH))
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
60
      goto err;
2069.2.3 by Brian Aker
Encapsulate a bit more of the logic around making strings from type::Time
61
2088.8.9 by Brian Aker
Merge in namespace of enum.
62
    l_time.convert(*str, type::DRIZZLE_TIMESTAMP_DATE);
2069.2.3 by Brian Aker
Encapsulate a bit more of the logic around making strings from type::Time
63
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
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);
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
86
  type::Time l_time;
574.3.17 by Lee
moving functions from item_timefunc to functions/time directory
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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
112
} /* namespace drizzled */