2 * Firebird ADO.NET Data provider for .NET and Mono
4 * The contents of this file are subject to the Initial
5 * Developer's Public License Version 1.0 (the "License");
6 * you may not use this file except in compliance with the
7 * License. You may obtain a copy of the License at
8 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10 * Software distributed under the License is distributed on
11 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
12 * express or implied. See the License for the specific
13 * language governing rights and limitations under the License.
15 * Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16 * All Rights Reserved.
20 using System.Globalization;
22 namespace FirebirdSql.Data.Common
24 internal sealed class TypeDecoder
34 #region Static Methods
36 public static decimal DecodeDecimal(object value, int scale, int sqltype)
39 decimal returnValue = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
43 divisor = (long)System.Math.Pow(10, scale * (-1));
48 case IscCodes.SQL_SHORT:
49 case IscCodes.SQL_LONG:
50 case IscCodes.SQL_QUAD:
51 case IscCodes.SQL_INT64:
52 returnValue = returnValue / divisor;
59 public static DateTime DecodeTime(int sql_time)
61 GregorianCalendar calendar = new GregorianCalendar();
63 int millisInDay = sql_time / 10;
64 int hour = millisInDay / 3600000;
65 int minute = (millisInDay - hour * 3600000) / 60000;
66 int second = (millisInDay - hour * 3600000 - minute * 60000) / 1000;
67 int millisecond = millisInDay - hour * 3600000 - minute * 60000 - second * 1000;
69 return new DateTime(1970, 1, 1, hour, minute, second, millisecond, calendar);
72 public static DateTime DecodeDate(int sql_date)
74 int year, month, day, century;
76 sql_date -= 1721119 - 2400001;
77 century = (4 * sql_date - 1) / 146097;
78 sql_date = 4 * sql_date - 1 - 146097 * century;
81 sql_date = (4 * day + 3) / 1461;
82 day = 4 * day + 3 - 1461 * sql_date;
85 month = (5 * day - 3) / 153;
86 day = 5 * day - 3 - 153 * month;
89 year = 100 * century + sql_date;
101 DateTime date = new System.DateTime(year, month, day);