1: using System;
2: using System.Globalization;
3: using System.Web;
4:
5: /*
6: * Attention
7: * This library has been written by: Anas Reslan Bahsas if you are going to use it please dont remove this line.
8: * you have to add this class to a asp.net web project to work well. I will be grateful to receive any commments or
9: * suggestion to anasbahsas@hotmail.com
10: *
11: * http://www.aawsat.com/
12: */
13:
14: namespace Ia.Cl.Model
15: {
16: ////////////////////////////////////////////////////////////////////////////
17:
18: /// <summary publish="true">
19: /// Hijri date handler class.
20: /// </summary>
21: /// <remarks>
22: /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
23: ///
24: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
25: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
26: ///
27: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
28: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
29: ///
30: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
31: ///
32: /// Copyright notice: This notice may not be removed or altered from any source distribution.
33: /// </remarks>
34: public class Hijri
35: {
36: private static HttpContext cur;
37:
38: private const int startGreg = 1900;
39: private const int endGreg = 2100;
40: private static string[] allFormats = { "yyyy/MM/dd", "yyyy/M/d", "dd/MM/yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "yyyy-MM-dd", "yyyy-M-d", "dd-MM-yyyy", "d-M-yyyy", "dd-M-yyyy", "d-MM-yyyy", "yyyy MM dd", "yyyy M d", "dd MM yyyy", "d M yyyy", "dd M yyyy", "d MM yyyy" };
41: private static CultureInfo arCul;
42: private static CultureInfo enCul;
43: private static HijriCalendar h;
44: private static GregorianCalendar g;
45:
46: ////////////////////////////////////////////////////////////////////////////
47:
48: /// <summary>
49: ///
50: /// </summary>
51: public Hijri()
52: {
53: cur = HttpContext.Current;
54:
55: arCul = new CultureInfo("ar-SA");
56: enCul = new CultureInfo("en-US");
57:
58: h = new HijriCalendar();
59: g = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
60:
61: arCul.DateTimeFormat.Calendar = h;
62: }
63:
64: ////////////////////////////////////////////////////////////////////////////
65:
66: /// <summary>
67: /// Check if string is hijri date and then return true
68: /// </summary>
69: /// <param name="hijri"></param>
70: /// <returns></returns>
71: public static bool IsHijri(string hijri)
72: {
73: if (hijri.Length <= 0)
74: {
75: cur.Trace.Warn("IsHijri Error: Date String is Empty");
76: return false;
77: }
78: try
79: {
80: DateTime tempDate = DateTime.ParseExact(hijri, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
81: if (tempDate.Year >= startGreg && tempDate.Year <= endGreg) return true;
82: else return false;
83: }
84: catch (Exception ex)
85: {
86: cur.Trace.Warn("IsHijri Error :" + hijri.ToString() + "\n" + ex.Message);
87: return false;
88: }
89: }
90:
91: ////////////////////////////////////////////////////////////////////////////
92:
93: /// <summary>
94: /// Check if string is Gregorian date and then return true
95: /// </summary>
96: /// <param name="greg"></param>
97: /// <returns></returns>
98: public static bool IsGreg(string greg)
99: {
100: if (greg.Length <= 0)
101: {
102: cur.Trace.Warn("IsGreg :Date String is Empty");
103: return false;
104: }
105: try
106: {
107: DateTime tempDate = DateTime.ParseExact(greg, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
108: if (tempDate.Year >= startGreg && tempDate.Year <= endGreg) return true;
109: else return false;
110: }
111: catch (Exception ex)
112: {
113: cur.Trace.Warn("IsGreg Error :" + greg.ToString() + "\n" + ex.Message);
114: return false;
115: }
116: }
117:
118: ////////////////////////////////////////////////////////////////////////////
119:
120: /// <summary>
121: /// Return Formatted Hijri date string
122: /// </summary>
123: /// <param name="date"></param>
124: /// <param name="format"></param>
125: /// <returns></returns>
126: public static string FormatHijri(string date, string format)
127: {
128: if (date.Length <= 0)
129: {
130: cur.Trace.Warn("Format :Date String is Empty");
131: return "";
132: }
133: try
134: {
135: DateTime tempDate = DateTime.ParseExact(date, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
136: return tempDate.ToString(format, arCul.DateTimeFormat);
137:
138: }
139: catch (Exception ex)
140: {
141: cur.Trace.Warn("Date :\n" + ex.Message);
142: return "";
143: }
144: }
145:
146: ////////////////////////////////////////////////////////////////////////////
147:
148: /// <summary>
149: /// Returned Formatted Gregorian date string
150: /// </summary>
151: /// <param name="date"></param>
152: /// <param name="format"></param>
153: /// <returns></returns>
154: public static string FormatGreg(string date, string format)
155: {
156: if (date.Length <= 0)
157: {
158: cur.Trace.Warn("Format :Date String is Empty");
159: return "";
160: }
161: try
162: {
163: DateTime tempDate = DateTime.ParseExact(date, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
164: return tempDate.ToString(format, enCul.DateTimeFormat);
165: }
166: catch (Exception ex)
167: {
168: cur.Trace.Warn("Date :\n" + ex.Message);
169: return "";
170: }
171: }
172:
173: ////////////////////////////////////////////////////////////////////////////
174:
175: /// <summary>
176: /// Return Today Gregorian date and return it in yyyy/MM/dd format
177: /// </summary>
178: /// <returns></returns>
179: public static string GDateNow()
180: {
181: try
182: {
183: return DateTime.UtcNow.AddHours(3).ToString("yyyy-MM-dd", enCul.DateTimeFormat);
184: }
185: catch (Exception ex)
186: {
187: cur.Trace.Warn("GDateNow :\n" + ex.Message);
188: return "";
189: }
190: }
191:
192: ////////////////////////////////////////////////////////////////////////////
193:
194: /// <summary>
195: /// Return formatted today Gregorian date based on your format
196: /// </summary>
197: /// <param name="format"></param>
198: /// <returns></returns>
199: public static string GDateNow(string format)
200: {
201: try
202: {
203: return DateTime.UtcNow.AddHours(3).ToString(format, enCul.DateTimeFormat);
204: }
205: catch (Exception ex)
206: {
207: cur.Trace.Warn("GDateNow :\n" + ex.Message);
208: return "";
209: }
210: }
211:
212: ////////////////////////////////////////////////////////////////////////////
213:
214: /// <summary>
215: /// Return Today Hijri date and return it in yyyy-MM-dd format
216: /// </summary>
217: /// <returns></returns>
218: public string HDateNow()
219: {
220: try
221: {
222: return DateTime.UtcNow.AddHours(3).ToString("yyyy-MM-dd", arCul.DateTimeFormat);
223: }
224: catch (Exception ex)
225: {
226: cur.Trace.Warn("HDateNow :\n" + ex.Message);
227: return "";
228: }
229: }
230:
231: ////////////////////////////////////////////////////////////////////////////
232:
233: /// <summary>
234: /// Return formatted today hijri date based on your format
235: /// </summary>
236: /// <param name="format"></param>
237: /// <returns></returns>
238: public static string HDateNow(string format)
239: {
240: try
241: {
242: return DateTime.UtcNow.AddHours(3).ToString(format, arCul.DateTimeFormat);
243: }
244: catch (Exception ex)
245: {
246: cur.Trace.Warn("HDateNow :\n" + ex.Message);
247: return "";
248: }
249: }
250:
251: ////////////////////////////////////////////////////////////////////////////
252:
253: /// <summary>
254: /// Convert Hijri Date to it's equivalent Gregorian Date
255: /// </summary>
256: /// <param name="hijri"></param>
257: /// <returns></returns>
258: public static string HijriToGreg(string hijri)
259: {
260: if (hijri.Length <= 0)
261: {
262: cur.Trace.Warn("HijriToGreg :Date String is Empty");
263: return "";
264: }
265: try
266: {
267: DateTime tempDate = DateTime.ParseExact(hijri, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
268: return tempDate.ToString("yyyy-MM-dd", enCul.DateTimeFormat);
269: }
270: catch (Exception ex)
271: {
272: cur.Trace.Warn("HijriToGreg :" + hijri.ToString() + "\n" + ex.Message);
273: return "";
274: }
275: }
276:
277: ////////////////////////////////////////////////////////////////////////////
278:
279: /// <summary>
280: /// Convert Hijri Date to it's equivalent Gregorian Date
281: /// and return it in specified format
282: /// </summary>
283: /// <param name="hijri"></param>
284: /// <param name="format"></param>
285: /// <returns></returns>
286: public static string HijriToGreg(string hijri, string format)
287: {
288: if (hijri.Length <= 0)
289: {
290: cur.Trace.Warn("HijriToGreg :Date String is Empty");
291: return "";
292: }
293: try
294: {
295: DateTime tempDate = DateTime.ParseExact(hijri, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
296: return tempDate.ToString(format, enCul.DateTimeFormat);
297:
298: }
299: catch (Exception ex)
300: {
301: cur.Trace.Warn("HijriToGreg :" + hijri.ToString() + "\n" + ex.Message);
302: return "";
303: }
304: }
305:
306: ////////////////////////////////////////////////////////////////////////////
307:
308: /// <summary>
309: /// Convert Gregoian Date to it's equivalent Hijir Date
310: /// </summary>
311: /// <param name="greg"></param>
312: /// <returns></returns>
313: public static string GregToHijri(string greg)
314: {
315: if (greg.Length <= 0)
316: {
317: cur.Trace.Warn("GregToHijri :Date String is Empty");
318: return "";
319: }
320: try
321: {
322: DateTime tempDate = DateTime.ParseExact(greg, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
323: return tempDate.ToString("yyyy-MM-dd", arCul.DateTimeFormat);
324: }
325: catch (Exception ex)
326: {
327: cur.Trace.Warn("GregToHijri :" + greg.ToString() + "\n" + ex.Message);
328: return "";
329: }
330: }
331:
332: ////////////////////////////////////////////////////////////////////////////
333:
334: /// <summary>
335: /// Convert Hijri Date to it's equivalent Gregorian Date and
336: /// return it in specified format
337: /// </summary>
338: /// <param name="greg"></param>
339: /// <param name="format"></param>
340: /// <returns></returns>
341: public static string GregToHijri(string greg, string format)
342: {
343: if (greg.Length <= 0)
344: {
345: cur.Trace.Warn("GregToHijri :Date String is Empty");
346: return "";
347: }
348: try
349: {
350: DateTime tempDate = DateTime.ParseExact(greg, allFormats, enCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
351: return tempDate.ToString(format, arCul.DateTimeFormat);
352: }
353: catch (Exception ex)
354: {
355: cur.Trace.Warn("GregToHijri :" + greg.ToString() + "\n" + ex.Message);
356: return "";
357: }
358: }
359:
360: ////////////////////////////////////////////////////////////////////////////
361:
362: /// <summary>
363: /// Return Gregrian Date Time as digit stamp
364: /// </summary>
365: /// <returns></returns>
366: public static string GTimeStamp()
367: {
368: return GDateNow("yyyyMMddHHmmss");
369: }
370:
371: ////////////////////////////////////////////////////////////////////////////
372: /// <summary>
373: /// Return Hijri Date Time as digit stamp
374: /// </summary>
375: /// <returns></returns>
376: public static string HTimeStamp()
377: {
378: return HDateNow("yyyyMMddHHmmss");
379: }
380:
381: ////////////////////////////////////////////////////////////////////////////
382:
383: /// <summary>
384: /// Compare two instaces of string date
385: /// and return indication of thier values
386: /// </summary>
387: /// <param name="d1"></param>
388: /// <param name="d2"></param>
389: /// <returns>positive d1 is greater than d2,
390: /// negative d1 is smaller than d2, 0 both are equal</returns>
391: public static int Compare(string d1, string d2)
392: {
393: try
394: {
395: DateTime date1 = DateTime.ParseExact(d1, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
396: DateTime date2 = DateTime.ParseExact(d2, allFormats, arCul.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces);
397: return DateTime.Compare(date1, date2);
398: }
399: catch (Exception ex)
400: {
401: cur.Trace.Warn("Compare :" + "\n" + ex.Message);
402: return -1;
403: }
404: }
405:
406: ////////////////////////////////////////////////////////////////////////////
407: ////////////////////////////////////////////////////////////////////////////
408: }
409: }
410: