~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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 <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/function/time/make_datetime.h>
 
23
 
 
24
/**
 
25
  @todo
 
26
  OPTIMIZATION
 
27
  - Replace the switch with a function that should be called for each
 
28
  date type.
 
29
  - Remove sprintf and opencode the conversion, like we do in
 
30
  Field_datetime.
 
31
 
 
32
  The reason for this functions existence is that as we don't have a
 
33
  way to know if a datetime/time value has microseconds in them
 
34
  we are now only adding microseconds to the output if the
 
35
  value has microseconds.
 
36
 
 
37
  We can't use a standard make_date_time() for this as we don't know
 
38
  if someone will use %f in the format specifier in which case we would get
 
39
  the microseconds twice.
 
40
*/
 
41
 
 
42
bool make_datetime(date_time_format_types format, DRIZZLE_TIME *ltime,
 
43
                          String *str)
 
44
{
 
45
  char *buff;
 
46
  const CHARSET_INFO * const cs= &my_charset_bin;
 
47
  uint32_t length= MAX_DATE_STRING_REP_LENGTH;
 
48
 
 
49
  if (str->alloc(length))
 
50
    return 1;
 
51
  buff= (char*) str->ptr();
 
52
 
 
53
  switch (format) {
 
54
  case TIME_ONLY:
 
55
    length= cs->cset->snprintf(cs, buff, length, "%s%02d:%02d:%02d",
 
56
                               ltime->neg ? "-" : "",
 
57
                               ltime->hour, ltime->minute, ltime->second);
 
58
    break;
 
59
  case TIME_MICROSECOND:
 
60
    length= cs->cset->snprintf(cs, buff, length, "%s%02d:%02d:%02d.%06ld",
 
61
                               ltime->neg ? "-" : "",
 
62
                               ltime->hour, ltime->minute, ltime->second,
 
63
                               ltime->second_part);
 
64
    break;
 
65
  case DATE_ONLY:
 
66
    length= cs->cset->snprintf(cs, buff, length, "%04d-%02d-%02d",
 
67
                               ltime->year, ltime->month, ltime->day);
 
68
    break;
 
69
  case DATE_TIME:
 
70
    length= cs->cset->snprintf(cs, buff, length,
 
71
                               "%04d-%02d-%02d %02d:%02d:%02d",
 
72
                               ltime->year, ltime->month, ltime->day,
 
73
                               ltime->hour, ltime->minute, ltime->second);
 
74
    break;
 
75
  case DATE_TIME_MICROSECOND:
 
76
    length= cs->cset->snprintf(cs, buff, length,
 
77
                               "%04d-%02d-%02d %02d:%02d:%02d.%06ld",
 
78
                               ltime->year, ltime->month, ltime->day,
 
79
                               ltime->hour, ltime->minute, ltime->second,
 
80
                               ltime->second_part);
 
81
    break;
 
82
  }
 
83
 
 
84
  str->length(length);
 
85
  str->set_charset(cs);
 
86
  return 0;
 
87
}
 
88