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

Revision 1, 8.9 kB (checked in by DenisG, 3 years ago)

Initial import

  • Property svn:eol-style set to native
Line 
1 //
2 // Softec
3 //
4 // Contact: Support@softec.st
5 //
6 // Designed by Denis Gervalle and Olivier Desaive
7 // Written by Denis Gervalle
8 //
9 // Copyright 2004 by SOFTEC. All rights reserved.
10 //
11 using System;
12 using System.Text;
13 using System.Runtime.InteropServices;
14 using System.Diagnostics;
15
16 namespace Softec.AprSharp
17 {
18     public unsafe struct AprTimeExp
19     {
20         private apr_time_exp_t *mTimeExp;
21
22         [StructLayout( LayoutKind.Sequential )]
23         private struct apr_time_exp_t
24         {
25             public int tm_usec;
26             public int tm_sec;
27             public int tm_min;
28             public int tm_hour;
29             public int tm_mday;
30             public int tm_mon;
31             public int tm_year;
32             public int tm_wday;
33             public int tm_yday;
34             public int tm_isdst;
35             public int tm_gmtoff;
36         }
37
38         #region Generic embedding functions of an IntPtr
39         private AprTimeExp(apr_time_exp_t *ptr)
40         {
41             mTimeExp = ptr;
42         }
43
44         public AprTimeExp(out GCHandle handle)
45         {
46             handle = GCHandle.Alloc(new apr_time_exp_t(),GCHandleType.Pinned);
47             mTimeExp = handle.AddrOfPinnedObject().ToPointer();
48         }
49
50         public AprTimeExp(IntPtr ptr)
51         {
52             mTimeExp = ptr.ToPointer();
53         }
54        
55         public bool IsNull()
56         {
57             return( mTimeExp == null );
58         }
59
60         private void CheckPtr()
61         {
62             if( mTimeExp == null )
63                 throw new AprNullReferenceException();
64         }
65
66         public void ClearPtr()
67         {
68             mTimeExp = null;
69         }
70
71         public static implicit operator IntPtr(AprTimeExp timeExp)
72         {
73             return new IntPtr(timeExp.mTimeExp);
74         }
75        
76         public static implicit operator AprTimeExp(IntPtr ptr)
77         {
78             return new AprTimeExp(ptr);
79         }
80        
81         public override string ToString()
82         {
83             return("[apr_time_exp_t:"+(new IntPtr(mTimeExp)).ToInt32().ToString("X")+"]");
84         }
85         #endregion
86        
87         #region Methods wrappers
88         public static AprTimeExp PoolAlloc(AprPool pool)
89         {
90             return(new AprTimeExp((apr_time_exp_t *)pool.Alloc(sizeof(apr_time_exp_t))));
91         }
92
93         public static AprTimeExp ManagedAlloc(out GCHandle handle)
94         {
95             return(new AprTimeExp(out handle));
96         }
97      
98         public void SetTimeTZ(long value, int tz)
99         {
100             Debug.Write(String.Format("apr_time_exp_tz({0:X},{1},{2})...",(new IntPtr(mTimeExp)).ToInt32(),value,tz));
101             int res = Apr.apr_time_exp_tz(new IntPtr(mTimeExp), value, tz);
102             if(res != 0)
103                 throw new AprException(res);
104             Debug.WriteLine("Done");
105         }
106        
107         public string ToString(string format)
108         {
109             StringBuilder str = new StringBuilder(256);
110             uint len;
111             Debug.Write(String.Format("apr_strftime({0:X},{1})...",(new IntPtr(mTimeExp)).ToInt32(),format));
112             int res = Apr.apr_strftime(str, out len, (uint)str.Capacity, format, new IntPtr(mTimeExp));
113             if(res != 0)
114                 throw new AprException(res);
115             Debug.WriteLine("Done");
116             return(str.ToString());
117         }
118
119         public long Time
120         {
121             get
122             {
123                 long time;
124                 Debug.Write(String.Format("apr_time_exp_get({0:X})...",(new IntPtr(mTimeExp)).ToInt32()));
125                 int res = Apr.apr_time_exp_get(out time, new IntPtr(mTimeExp));
126                 if(res != 0)
127                     throw new AprException(res);
128                 Debug.WriteLine(String.Format("Done({0})",time));
129                 return(time);
130             }
131             set
132             {
133                 CheckPtr();
134                 Debug.Write(String.Format("apr_time_exp_gmt({0:X},{1})...",(new IntPtr(mTimeExp)).ToInt32(),value));
135                 int res = Apr.apr_time_exp_gmt(new IntPtr(mTimeExp), value);
136                 if(res != 0)
137                     throw new AprException(res);
138                 Debug.WriteLine("Done");
139             }
140         }
141        
142         public long GmtTime
143         {
144             get
145             {
146                 CheckPtr();
147                 long time;
148                 Debug.Write(String.Format("apr_time_exp_gmt_get({0:X})...",(new IntPtr(mTimeExp)).ToInt32()));
149                 int res = Apr.apr_time_exp_gmt_get(out time, new IntPtr(mTimeExp));
150                 if(res != 0)
151                     throw new AprException(res);
152                 Debug.WriteLine(String.Format("Done({0})",time));
153                 return(time);
154             }
155             set
156             {
157                 CheckPtr();
158                 Debug.Write(String.Format("apr_time_exp_lt({0:X},{1})...",(new IntPtr(mTimeExp)).ToInt32(),value));
159                 int res = Apr.apr_time_exp_lt(new IntPtr(mTimeExp), value);
160                 if(res != 0)
161                     throw new AprException(res);
162                 Debug.WriteLine("Done");
163             }
164         }
165         #endregion
166        
167         #region Structure members wrapper (Properties)
168         public int MicroSeconds
169         {
170             get
171             {
172                 CheckPtr();
173                 return(mTimeExp->tm_usec);
174             }
175             set
176             {
177                 CheckPtr();
178                 if( value<0 || value>999999 )
179                     throw new AprArgumentOutOfRangeException("MicroSeconds", value, 0, 999999);
180                 mTimeExp->tm_usec=value;
181             }
182         }
183        
184         public int Seconds
185         {
186             get
187             {
188                 CheckPtr();
189                 return(mTimeExp->tm_sec);
190             }
191             set
192             {
193                 CheckPtr();
194                 if( value<0 || value>59 )
195                     throw new AprArgumentOutOfRangeException("Seconds", value, 0, 59);
196                 mTimeExp->tm_sec=value;
197             }
198         }
199
200         public int Minutes
201         {
202             get
203             {
204                 CheckPtr();
205                 return(mTimeExp->tm_min);
206             }
207             set
208             {
209                 CheckPtr();
210                 if( value<0 || value>59 )
211                     throw new AprArgumentOutOfRangeException("Minutes", value, 0, 59);
212                 mTimeExp->tm_min=value;
213             }
214         }
215
216         public int Hours
217         {
218             get
219             {
220                 CheckPtr();
221                 return(mTimeExp->tm_hour);
222             }
223             set
224             {
225                 CheckPtr();
226                 if( value<0 || value>23 )
227                     throw new AprArgumentOutOfRangeException("Hours",value, 0, 23);
228                 mTimeExp->tm_hour=value;
229             }
230         }
231
232         public int Day
233         {
234             get
235             {
236                 CheckPtr();
237                 return(mTimeExp->tm_mday);
238             }
239             set
240             {
241                 CheckPtr();
242                 if( value<1 || value>31 )
243                     throw new AprArgumentOutOfRangeException("Day",value, 1, 31);
244                 mTimeExp->tm_mday=value-1;
245             }
246         }
247
248         public int Month
249         {
250             get
251             {
252                 CheckPtr();
253                 return(mTimeExp->tm_mon+1);
254             }
255             set
256             {
257                 CheckPtr();
258                 if( value<1 || value>12 )
259                     throw new AprArgumentOutOfRangeException("Month",value, 1, 12);
260                 mTimeExp->tm_mon=value-1;
261             }
262         }
263
264         public int Year
265         {
266             get
267             {
268                 CheckPtr();
269                 return(mTimeExp->tm_year+1900);
270             }
271             set
272             {
273                 CheckPtr();
274                 if( value<1900 )
275                     throw new AprArgumentOutOfRangeException("Year",value, "Expect an integer value over 1900.");
276                 mTimeExp->tm_year=value-1900;
277             }
278         }
279
280         public int WeekDay
281         {
282             get
283             {
284                 CheckPtr();
285                 return(mTimeExp->tm_wday);
286             }
287         }
288
289         public int YearDay
290         {
291             get
292             {
293                 CheckPtr();
294                 return(mTimeExp->tm_yday);
295             }
296         }
297
298         public bool IsDaylightSaving
299         {
300             get
301             {
302                 CheckPtr();
303                 return(mTimeExp->tm_isdst != 0);
304             }
305             set
306             {
307                 CheckPtr();
308                 mTimeExp->tm_isdst=(value) ? 1 : 0;
309             }
310         }
311
312         public int TimeZone
313         {
314             get
315             {
316                 CheckPtr();
317                 return(mTimeExp->tm_gmtoff);
318             }
319             set
320             {
321                 CheckPtr();
322                 if( value<-43200 || value>43200 )
323                     throw new AprArgumentOutOfRangeException("GmtOffset",value, -43200, +43200);
324                 mTimeExp->tm_gmtoff=value;
325             }
326         }
327         #endregion
328     }
329 }
Note: See TracBrowser for help on using the browser.