~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/type/time.cc

  • Committer: Brian Aker
  • Date: 2011-01-18 00:08:34 UTC
  • mto: (2097.1.1 drizzle-build)
  • mto: This revision was merged to the branch mainline in revision 2098.
  • Revision ID: brian@tangent.org-20110118000834-a1p3mves421202oy
Fix additional output, swaps for the valus.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1025
1025
 
1026
1026
static int my_time_to_str(const type::Time *l_time, char *to)
1027
1027
{
 
1028
  int32_t length;
1028
1029
  uint32_t extra_hours= 0;
1029
 
  return sprintf(to, "%s%02u:%02u:%02u",
1030
 
                         (l_time->neg ? "-" : ""),
1031
 
                         extra_hours+ l_time->hour,
1032
 
                         l_time->minute,
1033
 
                         l_time->second);
 
1030
 
 
1031
  length= sprintf(to, "%s%02u:%02u:%02u",
 
1032
                  (l_time->neg ? "-" : ""),
 
1033
                  extra_hours+ l_time->hour,
 
1034
                  l_time->minute,
 
1035
                  l_time->second);
 
1036
  if (length < 0)
 
1037
    return 0;
 
1038
 
 
1039
  return static_cast<size_t>(length); 
1034
1040
}
1035
1041
 
1036
1042
static int my_date_to_str(const type::Time *l_time, char *to)
1037
1043
{
1038
 
  return sprintf(to, "%04u-%02u-%02u",
 
1044
  int32_t length;
 
1045
  length= sprintf(to, "%04u-%02u-%02u",
1039
1046
                         l_time->year,
1040
1047
                         l_time->month,
1041
1048
                         l_time->day);
1042
 
}
1043
 
 
1044
 
static int my_datetime_to_str(const type::Time *l_time, char *to)
1045
 
{
1046
 
  return sprintf(to, "%04u-%02u-%02u %02u:%02u:%02u",
1047
 
                         l_time->year,
1048
 
                         l_time->month,
1049
 
                         l_time->day,
1050
 
                         l_time->hour,
1051
 
                         l_time->minute,
1052
 
                         l_time->second);
1053
 
}
1054
 
 
1055
 
 
1056
 
/*
1057
 
  Convert struct DATE/TIME/DATETIME value to string using built-in
1058
 
  MySQL time conversion formats.
1059
 
 
1060
 
  SYNOPSIS
1061
 
    my_TIME_to_string()
1062
 
 
1063
 
  NOTE
1064
 
    The string must have at least MAX_DATE_STRING_REP_LENGTH bytes reserved.
1065
 
*/
1066
 
 
1067
 
int my_TIME_to_str(const type::Time *l_time, char *to)
1068
 
{
1069
 
  switch (l_time->time_type) {
1070
 
  case type::DRIZZLE_TIMESTAMP_DATETIME:
1071
 
    return my_datetime_to_str(l_time, to);
1072
 
 
1073
 
  case type::DRIZZLE_TIMESTAMP_DATE:
1074
 
    return my_date_to_str(l_time, to);
1075
 
 
1076
 
  case type::DRIZZLE_TIMESTAMP_TIME:
1077
 
    return my_time_to_str(l_time, to);
1078
 
 
1079
 
  case type::DRIZZLE_TIMESTAMP_NONE:
1080
 
  case type::DRIZZLE_TIMESTAMP_ERROR:
1081
 
    to[0]='\0';
1082
 
  }
1083
 
 
1084
 
  return 0;
1085
 
}
 
1049
  if (length < 0)
 
1050
    return 0;
 
1051
 
 
1052
  return static_cast<size_t>(length); 
 
1053
}
 
1054
 
 
1055
static size_t my_datetime_to_str(const type::Time *l_time, char *to, size_t to_len)
 
1056
{
 
1057
  int32_t length;
 
1058
  length= snprintf(to, to_len,
 
1059
                   "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
 
1060
                   " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ".%06" PRIu32,
 
1061
                   l_time->year,
 
1062
                   l_time->month,
 
1063
                   l_time->day,
 
1064
                   l_time->hour,
 
1065
                   l_time->minute,
 
1066
                   l_time->second,
 
1067
                   l_time->second_part);
 
1068
  if (length < 0)
 
1069
    return 0;
 
1070
 
 
1071
  return static_cast<size_t>(length); 
 
1072
}
 
1073
 
1086
1074
 
1087
1075
namespace type {
1088
1076
 
1135
1123
 
1136
1124
void Time::convert(String &str, timestamp_t arg)
1137
1125
{
1138
 
  str.alloc(MAX_DATE_STRING_REP_LENGTH);
1139
 
  uint32_t length= 0;
1140
 
 
 
1126
  str.alloc(MAX_STRING_LENGTH);
 
1127
  size_t length= MAX_STRING_LENGTH;
 
1128
 
 
1129
  convert(str.c_ptr(), length, arg);
 
1130
 
 
1131
  str.length(length);
 
1132
  str.set_charset(&my_charset_bin);
 
1133
}
 
1134
 
 
1135
void Time::convert(char *str, size_t &to_length, timestamp_t arg)
 
1136
{
1141
1137
  switch (arg) {
1142
1138
  case DRIZZLE_TIMESTAMP_DATETIME:
1143
 
    length= (uint32_t) my_datetime_to_str(this, str.c_ptr());
 
1139
    to_length= my_datetime_to_str(this, str, to_length);
1144
1140
    break;
1145
1141
 
1146
1142
  case DRIZZLE_TIMESTAMP_DATE:
1147
 
    length= (uint32_t) my_date_to_str(this, str.c_ptr());
 
1143
    to_length= (uint32_t) my_date_to_str(this, str);
1148
1144
    break;
1149
1145
 
1150
1146
  case DRIZZLE_TIMESTAMP_TIME:
1151
 
    length= (uint32_t) my_time_to_str(this, str.c_ptr());
 
1147
    to_length= (uint32_t) my_time_to_str(this, str);
1152
1148
    break;
1153
1149
 
1154
1150
  case DRIZZLE_TIMESTAMP_NONE:
1155
1151
  case DRIZZLE_TIMESTAMP_ERROR:
1156
1152
    assert(0);
 
1153
    to_length= 0;
1157
1154
    break;
1158
1155
  }
1159
 
 
1160
 
  str.length(length);
1161
 
  str.set_charset(&my_charset_bin);
1162
1156
}
1163
1157
 
1164
1158
}