summaryrefslogtreecommitdiff
path: root/sql/my_decimal.cc
diff options
context:
space:
mode:
authorSergei Golubchik <sergii@pisem.net>2011-06-07 18:13:02 +0200
committerSergei Golubchik <sergii@pisem.net>2011-06-07 18:13:02 +0200
commit9b98cae4cc44fa39813675b361b7aa65d129b29d (patch)
tree7f4baeea245f91006e88c62508c2859ed9390c2d /sql/my_decimal.cc
parent29af1aefe914da6aca73498c077f7e37a36d7de4 (diff)
parent4d128777dde904c5f0adab9b093e854c9c580d41 (diff)
downloadmariadb-git-9b98cae4cc44fa39813675b361b7aa65d129b29d.tar.gz
merge with 5.1-micro
Diffstat (limited to 'sql/my_decimal.cc')
-rw-r--r--sql/my_decimal.cc78
1 files changed, 64 insertions, 14 deletions
diff --git a/sql/my_decimal.cc b/sql/my_decimal.cc
index 6bf72e32dfe..93e1d2431af 100644
--- a/sql/my_decimal.cc
+++ b/sql/my_decimal.cc
@@ -16,6 +16,10 @@
#include "mysql_priv.h"
#include <time.h>
+#define DIG_BASE 1000000000
+#define DIG_PER_DEC1 9
+#define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
+
#ifndef MYSQL_CLIENT
/**
@@ -207,20 +211,69 @@ int str2my_decimal(uint mask, const char *from, uint length,
}
+/**
+ converts a decimal into a pair of integers - for integer and fractional parts
+
+ special version, for decimals representing number of seconds.
+ integer part cannot be larger that 1e18 (otherwise it's an overflow).
+ fractional part is microseconds.
+*/
+bool my_decimal2seconds(const my_decimal *d, ulonglong *sec, ulong *microsec)
+{
+ int pos;
+
+ if (d->intg)
+ {
+ pos= (d->intg-1)/DIG_PER_DEC1;
+ *sec= d->buf[pos];
+ if (pos > 0)
+ *sec+= static_cast<longlong>(d->buf[pos-1]) * DIG_BASE;
+ }
+ else
+ {
+ *sec=0;
+ pos= -1;
+ }
+
+ *microsec= d->frac ? static_cast<longlong>(d->buf[pos+1]) / (DIG_BASE/1000000) : 0;
+
+ if (pos > 1)
+ {
+ for (int i=0; i < pos-1; i++)
+ if (d->buf[i])
+ {
+ *sec= LONGLONG_MAX;
+ break;
+ }
+ }
+ return d->sign();
+}
+
+
+/**
+ converts a pair of integers (seconds, microseconds) into a decimal
+*/
+my_decimal *seconds2my_decimal(bool sign,
+ ulonglong sec, ulong microsec, my_decimal *d)
+{
+ d->init();
+ longlong2decimal(sec, d); // cannot fail
+ if (microsec)
+ {
+ d->buf[(d->intg-1) / DIG_PER_DEC1 + 1]= microsec * (DIG_BASE/1000000);
+ d->frac= 6;
+ }
+ ((decimal_t *)d)->sign= sign;
+ return d;
+}
+
+
my_decimal *date2my_decimal(MYSQL_TIME *ltime, my_decimal *dec)
{
- longlong date;
- date = (ltime->year*100L + ltime->month)*100L + ltime->day;
+ longlong date= (ltime->year*100L + ltime->month)*100L + ltime->day;
if (ltime->time_type > MYSQL_TIMESTAMP_DATE)
date= ((date*100L + ltime->hour)*100L+ ltime->minute)*100L + ltime->second;
- if (int2my_decimal(E_DEC_FATAL_ERROR, ltime->neg ? -date : date, FALSE, dec))
- return dec;
- if (ltime->second_part)
- {
- dec->buf[(dec->intg-1) / 9 + 1]= ltime->second_part * 1000;
- dec->frac= 6;
- }
- return dec;
+ return seconds2my_decimal(ltime->neg, date, ltime->second_part, dec);
}
@@ -236,7 +289,7 @@ void my_decimal_trim(ulong *precision, uint *scale)
/*
- Convert a decimal to an ulong with a descreptive error message
+ Convert a decimal to an ulong with a descriptive error message
*/
int my_decimal2int(uint mask, const decimal_t *d, bool unsigned_flag,
@@ -266,9 +319,6 @@ int my_decimal2int(uint mask, const decimal_t *d, bool unsigned_flag,
#ifndef DBUG_OFF
/* routines for debugging print */
-#define DIG_PER_DEC1 9
-#define ROUND_UP(X) (((X)+DIG_PER_DEC1-1)/DIG_PER_DEC1)
-
/* print decimal */
void
print_decimal(const my_decimal *dec)