public static bool ParseTime(string text, out int hour, out int minute, out int seconds) { try { DateTime dt = DateTime.Parse(text); hour = dt.Hour; minute = dt.Minute; seconds = dt.Second; return(true); } catch { } seconds = 0; minute = 0; hour = 0; string[] ss = text.Split(' '); string s = ss[0]; string[] parts = s.Split(':'); if (parts.Length < 2) { return(false); } try { hour = MultilangExtentions.ParseInt(parts[0]); minute = MultilangExtentions.ParseInt(parts[1]); if (parts.Length > 2) { seconds = MultilangExtentions.ParseInt(parts[2]); } } catch { return(false); } if (ss.Length > 1) { s = ss[1].Trim().ToLower(); if (s.StartsWith("p") || s.StartsWith("ب")) { hour += 12; } } return(true); }
public static PersianDateTime Parse(string input) { //براي تاريخ درست كار مي كند و براي ساعت بايد تكميل گردد. int y = 0, m = 1, d = 1, ho = 0, mi = 0, se = 0; string s = input.Trim(); string[] ss = s.Split(' '); string time = ""; if (ss.Length >= 2) { s = ss[0]; //رشته ساعت كه بايد Parse گردد time = string.Join(" ", ss, 1, ss.Length - 1); ParseTime(time, out ho, out mi, out se); } else if (s.IndexOf(':') > 0) { PersianDateTime p = new PersianDateTime(); ParseTime(s, out p.hour, out p.minute, out p.second); return(p); } ss = s.Split('/', '\\', '-'); if (ss.Length > 3) { throw new ArgumentException("Input string is not in a correct format."); } if (ss.Length == 1 && s.Length == 8) { ss = new string[3]; ss[0] = s.Substring(0, 4); ss[1] = s.Substring(4, 2); ss[2] = s.Substring(6, 2); } y = MultilangExtentions.ParseInt(ss[0]); if (ss.Length > 1) { m = MultilangExtentions.ParseInt(ss[1]); } if (ss.Length > 2) { d = MultilangExtentions.ParseInt(ss[2]); } if (d > 31) { if (y < 31) { int t = y; y = d; d = t; } else { throw new ArgumentException("Input string is not in a correct format."); } } if (y < 100) { y += 1300; } if (y > 1800) { return((PersianDateTime)System.DateTime.Parse(input)); } return(new PersianDateTime(y, m, d, ho, mi, se)); }