Updated Firebird provider with latest sources from sourceforge
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / TypeEncoder.cs
1 /*\r
2  *  Firebird ADO.NET Data provider for .NET and Mono \r
3  * \r
4  *     The contents of this file are subject to the Initial \r
5  *     Developer's Public License Version 1.0 (the "License"); \r
6  *     you may not use this file except in compliance with the \r
7  *     License. You may obtain a copy of the License at \r
8  *     http://www.firebirdsql.org/index.php?op=doc&id=idpl\r
9  *\r
10  *     Software distributed under the License is distributed on \r
11  *     an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either \r
12  *     express or implied.  See the License for the specific \r
13  *     language governing rights and limitations under the License.\r
14  * \r
15  *  Copyright (c) 2002, 2005 Carlos Guzman Alvarez\r
16  *  All Rights Reserved.\r
17  */\r
18 \r
19 using System;\r
20 using System.Globalization;\r
21 \r
22 namespace FirebirdSql.Data.Common\r
23 {\r
24         internal sealed class TypeEncoder\r
25         {\r
26                 #region Constructors\r
27 \r
28                 private TypeEncoder()\r
29                 {\r
30                 }\r
31 \r
32                 #endregion\r
33 \r
34                 #region Static Methods\r
35 \r
36                 public static object EncodeDecimal(decimal d, int scale, int sqltype)\r
37                 {\r
38                         long multiplier = 1;\r
39 \r
40                         if (scale < 0)\r
41                         {\r
42                                 multiplier = (long)System.Math.Pow(10, scale * (-1));\r
43                         }\r
44 \r
45                         switch (sqltype & ~1)\r
46                         {\r
47                                 case IscCodes.SQL_SHORT:\r
48                                         return (short)(d * multiplier);\r
49 \r
50                                 case IscCodes.SQL_LONG:\r
51                                         return (int)(d * multiplier);\r
52 \r
53                                 case IscCodes.SQL_QUAD:\r
54                                 case IscCodes.SQL_INT64:\r
55                                         return (long)(d * multiplier);\r
56 \r
57                                 case IscCodes.SQL_DOUBLE:\r
58                                 default:\r
59                                         return d;\r
60                         }\r
61                 }\r
62 \r
63                 public static int EncodeTime(DateTime d)\r
64                 {\r
65                         GregorianCalendar calendar = new GregorianCalendar();\r
66 \r
67                         int millisInDay =\r
68                                 (int)(calendar.GetHour(d) * 3600000 +\r
69                                 calendar.GetMinute(d) * 60000 +\r
70                                 calendar.GetSecond(d) * 1000 +\r
71                                 calendar.GetMilliseconds(d)) * 10;\r
72 \r
73                         return millisInDay;\r
74                 }\r
75 \r
76                 public static int EncodeDate(DateTime d)\r
77                 {\r
78                         int day, month, year;\r
79                         int c, ya;\r
80 \r
81                         GregorianCalendar calendar = new GregorianCalendar();\r
82 \r
83                         day = calendar.GetDayOfMonth(d);\r
84                         month = calendar.GetMonth(d);\r
85                         year = calendar.GetYear(d);\r
86 \r
87                         if (month > 2)\r
88                         {\r
89                                 month -= 3;\r
90                         }\r
91                         else\r
92                         {\r
93                                 month += 9;\r
94                                 year -= 1;\r
95                         }\r
96 \r
97                         c = year / 100;\r
98                         ya = year - 100 * c;\r
99 \r
100                         return ((146097 * c) / 4 +\r
101                                 (1461 * ya) / 4 +\r
102                                 (153 * month + 2) / 5 +\r
103                                 day + 1721119 - 2400001);\r
104                 }\r
105 \r
106                 #endregion\r
107         }\r
108 }