root/trunk/AprSharp/dev/src/AprTimeExp.cs

Revision 76, 10.4 kB (checked in by DenisG, 3 years ago)

[AprSharp] AprTimeExp?: Finally fix the calculation of YearDay? using our own

algorithm

  • Property svn:eol-style set to native
Line 
1 //  AprSharp, a wrapper library around the Apache Portable Runtime Library
2 #region Copyright (C) 2004 SOFTEC sa.
3 //
4 //  This library is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU Lesser General Public
6 //  License as published by the Free Software Foundation; either
7 //  version 2.1 of the License, or (at your option) any later version.
8 //
9 //  This library is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 //  Lesser General Public License for more details.
13 //
14 //  You should have received a copy of the GNU Lesser General Public
15 //  License along with this library; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 //  Sources, support options and lastest version of the complete library
19 //  is available from:
20 //              http://www.softec.st/AprSharp
21 //             
22 //
23 //  Initial authors :
24 //              Denis Gervalle
25 //              Olivier Desaive
26 #endregion
27 //
28 using System;
29 using System.Text;
30 using System.Runtime.InteropServices;
31 using System.Diagnostics;
32
33 namespace Softec.AprSharp
34 {
35     public unsafe struct AprTimeExp : IAprUnmanaged
36     {
37         private apr_time_exp_t *mTimeExp;
38
39         [StructLayout( LayoutKind.Sequential, Pack=4 )]
40         private struct apr_time_exp_t
41         {
42             public int tm_usec;
43             public int tm_sec;
44             public int tm_min;
45             public int tm_hour;
46             public int tm_mday;
47             public int tm_mon;
48             public int tm_year;
49             public int tm_wday;
50             public int tm_yday;
51             public int tm_isdst;
52             public int tm_gmtoff;
53         }
54
55         #region Generic embedding functions of an IntPtr
56         private AprTimeExp(apr_time_exp_t *ptr)
57         {
58             mTimeExp = ptr;
59         }
60
61         public AprTimeExp(out GCHandle handle)
62         {
63             handle = GCHandle.Alloc(new apr_time_exp_t(),GCHandleType.Pinned);
64             mTimeExp = (apr_time_exp_t *)handle.AddrOfPinnedObject().ToPointer();
65         }
66
67         public AprTimeExp(IntPtr ptr)
68         {
69             mTimeExp = (apr_time_exp_t *)ptr.ToPointer();
70         }
71        
72         public bool IsNull
73         {
74                 get
75                 {
76                 return( mTimeExp == null );
77             }
78         }
79
80         private void CheckPtr()
81         {
82             if( mTimeExp == null )
83                 throw new AprNullReferenceException();
84         }
85
86         public void ClearPtr()
87         {
88             mTimeExp = null;
89         }
90
91         public IntPtr ToIntPtr()
92         {
93             return new IntPtr(mTimeExp);
94         }
95
96                 public bool ReferenceEquals(IAprUnmanaged obj)
97                 {
98                         return(obj.ToIntPtr() == ToIntPtr());
99                 }
100                
101         public static implicit operator IntPtr(AprTimeExp timeExp)
102         {
103             return new IntPtr(timeExp.mTimeExp);
104         }
105        
106         public static implicit operator AprTimeExp(IntPtr ptr)
107         {
108             return new AprTimeExp(ptr);
109         }
110        
111         public override string ToString()
112         {
113             return("[apr_time_exp_t:"+(new IntPtr(mTimeExp)).ToInt32().ToString("X")+"]");
114         }
115         #endregion
116        
117         #region Methods wrappers
118         public static AprTimeExp Alloc(AprPool pool)
119         {
120             return(new AprTimeExp((apr_time_exp_t *)pool.CAlloc(sizeof(apr_time_exp_t))));
121         }
122
123         public static AprTimeExp Alloc(out GCHandle handle)
124         {
125             return(new AprTimeExp(out handle));
126         }
127      
128         public void SetTimeTZ(long value, int tz)
129         {
130             Debug.Write(String.Format("apr_time_exp_tz({0:X},{1},{2})...",(new IntPtr(mTimeExp)).ToInt32(),value,tz));
131             int res = Apr.apr_time_exp_tz(new IntPtr(mTimeExp), value, tz);
132             if(res != 0)
133                 throw new AprException(res);
134             Debug.WriteLine("Done");
135         }
136        
137         public string ToString(string format)
138         {
139                 return(ToString(format,256));
140         }
141
142         public string ToString(string format, int size)
143         {
144             StringBuilder str = new StringBuilder(size);
145             uint len;
146             Debug.Write(String.Format("apr_strftime({0:X},{1})...",(new IntPtr(mTimeExp)).ToInt32(),format));
147             int res = Apr.apr_strftime(str, out len, (uint)str.Capacity, format, new IntPtr(mTimeExp));
148             if(res != 0)
149                 throw new AprException(res);
150             Debug.WriteLine("Done");
151             return(str.ToString());
152         }
153
154         public long Time
155         {
156             get
157             {
158                 long time;
159                 Debug.Write(String.Format("apr_time_exp_get({0:X})...",(new IntPtr(mTimeExp)).ToInt32()));
160                 int res = Apr.apr_time_exp_get(out time, new IntPtr(mTimeExp));
161                 if(res != 0)
162                     throw new AprException(res);
163                 Debug.WriteLine(String.Format("Done({0})",time));
164                 return(time);
165             }
166             set
167             {
168                 CheckPtr();
169                 Debug.Write(String.Format("apr_time_exp_gmt({0:X},{1})...",(new IntPtr(mTimeExp)).ToInt32(),value));
170                 int res = Apr.apr_time_exp_gmt(new IntPtr(mTimeExp), value);
171                 if(res != 0)
172                     throw new AprException(res);
173                 Debug.WriteLine("Done");
174             }
175         }
176        
177         public long GmtTime
178         {
179             get
180             {
181                 CheckPtr();
182                 long time;
183                 Debug.Write(String.Format("apr_time_exp_gmt_get({0:X})...",(new IntPtr(mTimeExp)).ToInt32()));
184                 int res = Apr.apr_time_exp_gmt_get(out time, new IntPtr(mTimeExp));
185                 if(res != 0)
186                     throw new AprException(res);
187                 Debug.WriteLine(String.Format("Done({0})",time));
188                 return(time);
189             }
190             set
191             {
192                 CheckPtr();
193                 Debug.Write(String.Format("apr_time_exp_lt({0:X},{1})...",(new IntPtr(mTimeExp)).ToInt32(),value));
194                 int res = Apr.apr_time_exp_lt(new IntPtr(mTimeExp), value);
195                 if(res != 0)
196                     throw new AprException(res);
197                 Debug.WriteLine("Done");
198             }
199         }
200         #endregion
201        
202         #region Structure members wrapper (Properties)
203         public int MicroSeconds
204         {
205             get
206             {
207                 CheckPtr();
208                 return(mTimeExp->tm_usec);
209             }
210             set
211             {
212                 CheckPtr();
213                 if( value<0 || value>999999 )
214                     throw new AprArgumentOutOfRangeException("MicroSeconds", value, 0, 999999);
215                 mTimeExp->tm_usec=value;
216             }
217         }
218        
219         public int Seconds
220         {
221             get
222             {
223                 CheckPtr();
224                 return(mTimeExp->tm_sec);
225             }
226             set
227             {
228                 CheckPtr();
229                 if( value<0 || value>59 )
230                     throw new AprArgumentOutOfRangeException("Seconds", value, 0, 59);
231                 mTimeExp->tm_sec=value;
232             }
233         }
234
235         public int Minutes
236         {
237             get
238             {
239                 CheckPtr();
240                 return(mTimeExp->tm_min);
241             }
242             set
243             {
244                 CheckPtr();
245                 if( value<0 || value>59 )
246                     throw new AprArgumentOutOfRangeException("Minutes", value, 0, 59);
247                 mTimeExp->tm_min=value;
248             }
249         }
250
251         public int Hours
252         {
253             get
254             {
255                 CheckPtr();
256                 return(mTimeExp->tm_hour);
257             }
258             set
259             {
260                 CheckPtr();
261                 if( value<0 || value>23 )
262                     throw new AprArgumentOutOfRangeException("Hours",value, 0, 23);
263                 mTimeExp->tm_hour=value;
264             }
265         }
266
267         public int Day
268         {
269             get
270             {
271                 CheckPtr();
272                 return(mTimeExp->tm_mday);
273             }
274             set
275             {
276                 CheckPtr();
277                 if( value<1 || value>31 )
278                     throw new AprArgumentOutOfRangeException("Day",value, 1, 31);
279                 mTimeExp->tm_mday=value;
280             }
281         }
282
283         public int Month
284         {
285             get
286             {
287                 CheckPtr();
288                 return(mTimeExp->tm_mon+1);
289             }
290             set
291             {
292                 CheckPtr();
293                 if( value<1 || value>12 )
294                     throw new AprArgumentOutOfRangeException("Month",value, 1, 12);
295                 mTimeExp->tm_mon=value-1;
296             }
297         }
298
299         public int Year
300         {
301             get
302             {
303                 CheckPtr();
304                 return(mTimeExp->tm_year+1900);
305             }
306             set
307             {
308                 CheckPtr();
309                 if( value<1970 )
310                     throw new AprArgumentOutOfRangeException("Year",value, "Expect an integer value over 1900.");
311                 mTimeExp->tm_year=value-1900;
312             }
313         }
314
315         public int WeekDay
316         {
317             get
318             {
319                 CheckPtr();
320                 return(mTimeExp->tm_wday);
321             }
322         }
323
324         public int YearDay
325         {
326             get
327             {
328                 CheckPtr();
329                 int m = Month;
330                 int y = Year;
331                 y = (y%4 == 0 && (y%100 != 0 || y%400 == 0)) ? 1 : 0;
332                 return(Day + ((979 * (m-12*((m-14)/12)) - 2918) >> 5) + y - 307 + (365 *((m+9)/12)));
333                 //return(mTimeExp->tm_yday);
334             }
335         }
336
337         public bool IsDaylightSaving
338         {
339             get
340             {
341                 CheckPtr();
342                 return(mTimeExp->tm_isdst != 0);
343             }
344             set
345             {
346                 CheckPtr();
347                 mTimeExp->tm_isdst=(value) ? 1 : 0;
348             }
349         }
350
351         public int TimeZone
352         {
353             get
354             {
355                 CheckPtr();
356                 return(mTimeExp->tm_gmtoff);
357             }
358             set
359             {
360                 CheckPtr();
361                 if( value<-43200 || value>43200 )
362                     throw new AprArgumentOutOfRangeException("GmtOffset",value, -43200, +43200);
363                 mTimeExp->tm_gmtoff=value;
364             }
365         }
366         #endregion
367     }
368 }
Note: See TracBrowser for help on using the browser.