Remove some deprecated libraries
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / TypeDecoder.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and Mono 
3  * 
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
9  *
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.
14  * 
15  *      Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16  *      All Rights Reserved.
17  */
18
19 using System;
20 using System.Globalization;
21
22 namespace FirebirdSql.Data.Common
23 {
24         internal sealed class TypeDecoder
25         {
26                 #region Constructors
27
28                 private TypeDecoder()
29                 {
30                 }
31
32                 #endregion
33
34                 #region Static Methods
35
36                 public static decimal DecodeDecimal(object value, int scale, int sqltype)
37                 {
38                         long divisor = 1;
39                         decimal returnValue = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
40
41                         if (scale < 0)
42                         {
43                                 divisor = (long)System.Math.Pow(10, scale * (-1));
44                         }
45
46                         switch (sqltype & ~1)
47                         {
48                                 case IscCodes.SQL_SHORT:
49                                 case IscCodes.SQL_LONG:
50                                 case IscCodes.SQL_QUAD:
51                                 case IscCodes.SQL_INT64:
52                                         returnValue = returnValue / divisor;
53                                         break;
54                         }
55
56                         return returnValue;
57                 }
58
59                 public static DateTime DecodeTime(int sql_time)
60                 {
61                         GregorianCalendar calendar = new GregorianCalendar();
62
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;
68
69                         return new DateTime(1970, 1, 1, hour, minute, second, millisecond, calendar);
70                 }
71
72                 public static DateTime DecodeDate(int sql_date)
73                 {
74                         int year, month, day, century;
75
76                         sql_date -= 1721119 - 2400001;
77                         century = (4 * sql_date - 1) / 146097;
78                         sql_date = 4 * sql_date - 1 - 146097 * century;
79                         day = sql_date / 4;
80
81                         sql_date = (4 * day + 3) / 1461;
82                         day = 4 * day + 3 - 1461 * sql_date;
83                         day = (day + 4) / 4;
84
85                         month = (5 * day - 3) / 153;
86                         day = 5 * day - 3 - 153 * month;
87                         day = (day + 5) / 5;
88
89                         year = 100 * century + sql_date;
90
91                         if (month < 10)
92                         {
93                                 month += 3;
94                         }
95                         else
96                         {
97                                 month -= 9;
98                                 year += 1;
99                         }
100
101                         DateTime date = new System.DateTime(year, month, day);
102
103                         return date.Date;
104                 }
105
106                 #endregion
107         }
108 }