2009-03-27 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Common / TypeEncoder.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 TypeEncoder
25         {
26                 #region Constructors
27
28                 private TypeEncoder()
29                 {
30                 }
31
32                 #endregion
33
34                 #region Static Methods
35
36                 public static object EncodeDecimal(decimal d, int scale, int sqltype)
37                 {
38                         long multiplier = 1;
39
40                         if (scale < 0)
41                         {
42                                 multiplier = (long)System.Math.Pow(10, scale * (-1));
43                         }
44
45                         switch (sqltype & ~1)
46                         {
47                                 case IscCodes.SQL_SHORT:
48                                         return (short)(d * multiplier);
49
50                                 case IscCodes.SQL_LONG:
51                                         return (int)(d * multiplier);
52
53                                 case IscCodes.SQL_QUAD:
54                                 case IscCodes.SQL_INT64:
55                                         return (long)(d * multiplier);
56
57                                 case IscCodes.SQL_DOUBLE:
58                                 default:
59                                         return d;
60                         }
61                 }
62
63                 public static int EncodeTime(DateTime d)
64                 {
65                         GregorianCalendar calendar = new GregorianCalendar();
66
67                         int millisInDay =
68                                 (int)(calendar.GetHour(d) * 3600000 +
69                                 calendar.GetMinute(d) * 60000 +
70                                 calendar.GetSecond(d) * 1000 +
71                                 calendar.GetMilliseconds(d)) * 10;
72
73                         return millisInDay;
74                 }
75
76                 public static int EncodeDate(DateTime d)
77                 {
78                         int day, month, year;
79                         int c, ya;
80
81                         GregorianCalendar calendar = new GregorianCalendar();
82
83                         day = calendar.GetDayOfMonth(d);
84                         month = calendar.GetMonth(d);
85                         year = calendar.GetYear(d);
86
87                         if (month > 2)
88                         {
89                                 month -= 3;
90                         }
91                         else
92                         {
93                                 month += 9;
94                                 year -= 1;
95                         }
96
97                         c = year / 100;
98                         ya = year - 100 * c;
99
100                         return ((146097 * c) / 4 +
101                                 (1461 * ya) / 4 +
102                                 (153 * month + 2) / 5 +
103                                 day + 1721119 - 2400001);
104                 }
105
106                 #endregion
107         }
108 }