//public static bool TryCreateDate(NamedValues<ZValue> values, out Date date, out DateType dateType, NamedValues<ZValue> param = null) public static bool TryCreateDate(NamedValues <ZValue> values, out Date date, out DateType dateType) { date = Date.MinValue; dateType = DateType.Unknow; DateValues dateValues = DateValues.GetDateValues(values); //if (!values.ContainsKey("day_near_current_date") && !values.ContainsKey("month") && !values.ContainsKey("month1") && !values.ContainsKey("month2") && !values.ContainsKey("year")) if (dateValues.dayNearCurrentDate == null && dateValues.month == null && dateValues.year == null) { //values.SetError("error creating Date unknow day_near_current_date and month (month1 or month2)"); //Trace.WriteLine("error creating Date unknow day_near_current_date unknow month (month1 and month2) and unknow year (year1 and year2)"); return(false); } //if (values.ContainsKey("day_near_current_date")) if (dateValues.dayNearCurrentDate != null) { //return TryCreateDateFromDay(values, out date, out dateType, param); return(TryCreateDateFromDay(dateValues, out date, out dateType)); } else { //return TryCreateDateFromYearMonthDay(values, out date, out dateType, param); return(TryCreateDateFromYearMonthDay(dateValues, out date, out dateType)); } }
//public static bool TryCreateDateFromDay(NamedValues<ZValue> values, out Date date, out DateType dateType, NamedValues<ZValue> param = null) public static bool TryCreateDateFromDay(DateValues dateValues, out Date date, out DateType dateType, NamedValues <ZValue> param = null) { date = Date.MinValue; dateType = DateType.Unknow; //int day; //ZValue v = values["day_near_current_date"]; //if (v is ZString) //{ // string s = (string)v; // s = s.Replace('o', '0'); // s = s.Replace('O', '0'); // if (!int.TryParse(s, out day)) // { // //throw new PBException("error creating Date day_near_current_date is'nt a number : \"{0}\"", o); // values.SetError("error creating Date day_near_current_date is'nt a number : \"{0}\"", v); // return false; // } //} //else if (v is ZInt) // day = (int)v; //else //{ // //throw new PBException("error creating Date day_near_current_date should be a string number or an int : {0}", o); // values.SetError("error creating Date day_near_current_date should be a string number or an int : {0}", v); // return false; //} //if (!TryGetDateFromDay(day, out date)) if (!TryGetDateFromDay((int)dateValues.dayNearCurrentDate, out date)) { //throw new PBException("error creating Date bad day_near_current_date : {0}", day); //values.SetError("error creating Date bad day_near_current_date : {0}", day); Trace.WriteLine("error creating Date bad day_near_current_date : {0}", dateValues.dayNearCurrentDate); return(false); } dateType = DateType.Day; return(true); }
//public static bool TryCreateDateFromYearMonthDay(NamedValues<ZValue> values, out Date date, out DateType dateType, NamedValues<ZValue> param = null) public static bool TryCreateDateFromYearMonthDay(DateValues dateValues, out Date date, out DateType dateType) { date = Date.MinValue; dateType = DateType.Unknow; int year; int month = 1; int day = 1; bool foundYear = false; bool foundMonth = false; bool foundDay = false; //ZValue v; //if (values.ContainsKey("year")) //{ // foundYear = true; // v = values["year"]; // if (v is ZString) // { // if (!int.TryParse((string)v, out year)) // { // values.SetError("error creating Date year is'nt a number : \"{0}\"", v); // return false; // } // } // else if (v is ZInt) // year = (int)v; // else // { // values.SetError("error creating Date year should be a string number or an int : {0}", v); // return false; // } //} //else // year = Date.Today.Year; if (dateValues.year != null) { foundYear = true; year = (int)dateValues.year; } else year = Date.Today.Year; //v = null; //if (values.ContainsKey("month1")) //{ // v = values["month1"]; // if (v is ZString && string.IsNullOrEmpty((string)v)) // v = null; //} //if (v == null && values.ContainsKey("month2")) //{ // v = values["month2"]; // if (v is ZString && string.IsNullOrEmpty((string)v)) // v = null; //} //if (v == null && values.ContainsKey("month")) //{ // v = values["month"]; // if (v is ZString && string.IsNullOrEmpty((string)v)) // v = null; //} //if (v != null) //{ // foundMonth = true; // if (v is ZString) // { // if (!int.TryParse((string)v, out month)) // { // month = GetMonthNumber((string)v); // if (month == 0) // { // values.SetError("error creating Date invalid month : \"{0}\"", v); // return false; // } // } // } // else if (v is ZInt) // month = (int)v; // else // { // values.SetError("error creating Date month should be a string number or an int : {0}", v); // return false; // } //} if (dateValues.month != null) { foundMonth = true; month = (int)dateValues.month; if (month < 1 || month > 12) return false; } //if (values.ContainsKey("day")) //{ // foundDay = true; // v = values["day"]; // if (v is ZString) // { // if (!int.TryParse((string)v, out day)) // { // values.SetError("error creating Date day is'nt a number : \"{0}\"", v); // return false; // } // } // else if (v is ZInt) // { // day = (int)v; // } // else // { // values.SetError("error creating Date day should be a string number or an int : {0}", v); // return false; // } //} if (dateValues.day != null) { foundDay = true; day = (int)dateValues.day; } if (year < 100) year = zdate.ToFourDigitYear(year); if (day < 1 || day > DateTime.DaysInMonth(year, month)) return false; Date date3 = new Date(year, month, day); if (!foundYear) { if (date3 > Date.Today && month > Date.Today.Month + 2) { if (day > DateTime.DaysInMonth(year - 1, month)) return false; date3 = new Date(year - 1, month, day); } } date = date3; if (foundDay) dateType = DateType.Day; else if (foundMonth) dateType = DateType.Month; else dateType = DateType.Year; return true; }
//public static bool TryCreateDateFromDay(NamedValues<ZValue> values, out Date date, out DateType dateType, NamedValues<ZValue> param = null) public static bool TryCreateDateFromDay(DateValues dateValues, out Date date, out DateType dateType, NamedValues<ZValue> param = null) { date = Date.MinValue; dateType = DateType.Unknow; //int day; //ZValue v = values["day_near_current_date"]; //if (v is ZString) //{ // string s = (string)v; // s = s.Replace('o', '0'); // s = s.Replace('O', '0'); // if (!int.TryParse(s, out day)) // { // //throw new PBException("error creating Date day_near_current_date is'nt a number : \"{0}\"", o); // values.SetError("error creating Date day_near_current_date is'nt a number : \"{0}\"", v); // return false; // } //} //else if (v is ZInt) // day = (int)v; //else //{ // //throw new PBException("error creating Date day_near_current_date should be a string number or an int : {0}", o); // values.SetError("error creating Date day_near_current_date should be a string number or an int : {0}", v); // return false; //} //if (!TryGetDateFromDay(day, out date)) if (!TryGetDateFromDay((int)dateValues.dayNearCurrentDate, out date)) { //throw new PBException("error creating Date bad day_near_current_date : {0}", day); //values.SetError("error creating Date bad day_near_current_date : {0}", day); Trace.WriteLine("error creating Date bad day_near_current_date : {0}", dateValues.dayNearCurrentDate); return false; } dateType = DateType.Day; return true; }
public static DateValues GetDateValues(NamedValues<ZValue> values) { DateValues dateValues = new DateValues(); ZValue v; if (values.ContainsKey("day_near_current_date")) { v = values["day_near_current_date"]; //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { string s = (string)v; s = s.Replace('o', '0'); s = s.Replace('O', '0'); int day; if (int.TryParse(s, out day)) dateValues.dayNearCurrentDate = day; else { //values.SetError("error creating Date day_near_current_date is'nt a number : \"{0}\"", v); Trace.WriteLine("error creating Date day_near_current_date is'nt a number : \"{0}\"", v); //return false; } } else if (v is ZInt) //day = (int)v; dateValues.dayNearCurrentDate = (int)v; else { //values.SetError("error creating Date day_near_current_date should be a string number or an int : {0}", v); Trace.WriteLine("error creating Date day_near_current_date should be a string number or an int : {0}", v); //return false; } } //string name = null; //if (values.ContainsKey("year")) // name = "year"; //else if (values.ContainsKey("year1")) // name = "year1"; //else if (values.ContainsKey("year2")) // name = "year2"; v = null; if (values.ContainsKey("year1")) { v = values["year1"]; if (v is ZString && string.IsNullOrEmpty((string)v)) v = null; } if (v == null && values.ContainsKey("year2")) { v = values["year2"]; if (v is ZString && string.IsNullOrEmpty((string)v)) v = null; } if (v == null && values.ContainsKey("year")) { v = values["year"]; if (v is ZString && string.IsNullOrEmpty((string)v)) v = null; } if (v != null) { //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { int year; if (int.TryParse((string)v, out year)) dateValues.year = year; else { //values.SetError("error creating Date year is'nt a number : \"{0}\"", v); Trace.WriteLine("error creating Date year is'nt a number : \"{0}\"", v); //return false; } } else if (v is ZInt) dateValues.year = (int)v; else if (v != null) { //values.SetError("error creating Date year should be a string number or an int : {0}", v); Trace.WriteLine("error creating Date year should be a string number or an int : {0}", v); //return false; } } v = null; if (values.ContainsKey("month1")) { v = values["month1"]; if (v is ZString && string.IsNullOrEmpty((string)v)) v = null; } if (v == null && values.ContainsKey("month2")) { v = values["month2"]; if (v is ZString && string.IsNullOrEmpty((string)v)) v = null; } if (v == null && values.ContainsKey("month")) { v = values["month"]; if (v is ZString && string.IsNullOrEmpty((string)v)) v = null; } if (v != null) { //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { int month; if (int.TryParse((string)v, out month)) dateValues.month = month; else { month = zdate.GetMonthNumber((string)v); if (month != 0) dateValues.month = month; else { //values.SetError("error creating Date invalid month : \"{0}\"", v); Trace.WriteLine("error creating Date invalid month : \"{0}\"", v); //return false; } } } else if (v is ZInt) dateValues.month = (int)v; else if (v != null) { //values.SetError("error creating Date month should be a string number or an int : {0}", v); Trace.WriteLine("error creating Date month should be a string number or an int : {0}", v); //return false; } } if (values.ContainsKey("day")) { v = values["day"]; //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { int day; //if ((string)v == "1er") if (string.Equals((string)v, "1er", StringComparison.InvariantCultureIgnoreCase)) dateValues.day = 1; else if (int.TryParse((string)v, out day)) dateValues.day = day; else { //values.SetError("error creating Date day is'nt a number : \"{0}\"", v); Trace.WriteLine("error creating Date day is'nt a number : \"{0}\"", v); //return false; } } else if (v is ZInt) { dateValues.day = (int)v; } else if (v != null) { //values.SetError("error creating Date day should be a string number or an int : {0}", v); //Trace.WriteLine("error creating Date day should be a string number or an int : value {0} type {1}", v != null ? v : "(null)", v != null ? v.GetType().zName() : "(null)"); Trace.WriteLine("error creating Date day should be a string number or an int : value {0} type {1}", v, v.GetType().zGetTypeName()); //return false; } } return dateValues; }
//public static bool TryCreateDateFromYearMonthDay(NamedValues<ZValue> values, out Date date, out DateType dateType, NamedValues<ZValue> param = null) public static bool TryCreateDateFromYearMonthDay(DateValues dateValues, out Date date, out DateType dateType) { date = Date.MinValue; dateType = DateType.Unknow; int year; int month = 1; int day = 1; bool foundYear = false; bool foundMonth = false; bool foundDay = false; //ZValue v; //if (values.ContainsKey("year")) //{ // foundYear = true; // v = values["year"]; // if (v is ZString) // { // if (!int.TryParse((string)v, out year)) // { // values.SetError("error creating Date year is'nt a number : \"{0}\"", v); // return false; // } // } // else if (v is ZInt) // year = (int)v; // else // { // values.SetError("error creating Date year should be a string number or an int : {0}", v); // return false; // } //} //else // year = Date.Today.Year; if (dateValues.year != null) { foundYear = true; year = (int)dateValues.year; } else { year = Date.Today.Year; } //v = null; //if (values.ContainsKey("month1")) //{ // v = values["month1"]; // if (v is ZString && string.IsNullOrEmpty((string)v)) // v = null; //} //if (v == null && values.ContainsKey("month2")) //{ // v = values["month2"]; // if (v is ZString && string.IsNullOrEmpty((string)v)) // v = null; //} //if (v == null && values.ContainsKey("month")) //{ // v = values["month"]; // if (v is ZString && string.IsNullOrEmpty((string)v)) // v = null; //} //if (v != null) //{ // foundMonth = true; // if (v is ZString) // { // if (!int.TryParse((string)v, out month)) // { // month = GetMonthNumber((string)v); // if (month == 0) // { // values.SetError("error creating Date invalid month : \"{0}\"", v); // return false; // } // } // } // else if (v is ZInt) // month = (int)v; // else // { // values.SetError("error creating Date month should be a string number or an int : {0}", v); // return false; // } //} if (dateValues.month != null) { foundMonth = true; month = (int)dateValues.month; if (month < 1 || month > 12) { return(false); } } //if (values.ContainsKey("day")) //{ // foundDay = true; // v = values["day"]; // if (v is ZString) // { // if (!int.TryParse((string)v, out day)) // { // values.SetError("error creating Date day is'nt a number : \"{0}\"", v); // return false; // } // } // else if (v is ZInt) // { // day = (int)v; // } // else // { // values.SetError("error creating Date day should be a string number or an int : {0}", v); // return false; // } //} if (dateValues.day != null) { foundDay = true; day = (int)dateValues.day; } if (year < 100) { year = zdate.ToFourDigitYear(year); } if (day < 1 || day > DateTime.DaysInMonth(year, month)) { return(false); } Date date3 = new Date(year, month, day); if (!foundYear) { if (date3 > Date.Today && month > Date.Today.Month + 2) { if (day > DateTime.DaysInMonth(year - 1, month)) { return(false); } date3 = new Date(year - 1, month, day); } } date = date3; if (foundDay) { dateType = DateType.Day; } else if (foundMonth) { dateType = DateType.Month; } else { dateType = DateType.Year; } return(true); }
public static DateValues GetDateValues(NamedValues <ZValue> values) { DateValues dateValues = new DateValues(); ZValue v; if (values.ContainsKey("day_near_current_date")) { v = values["day_near_current_date"]; //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { string s = (string)v; s = s.Replace('o', '0'); s = s.Replace('O', '0'); int day; if (int.TryParse(s, out day)) { dateValues.dayNearCurrentDate = day; } else { //values.SetError("error creating Date day_near_current_date is'nt a number : \"{0}\"", v); Trace.WriteLine("error creating Date day_near_current_date is'nt a number : \"{0}\"", v); //return false; } } else if (v is ZInt) { //day = (int)v; dateValues.dayNearCurrentDate = (int)v; } else { //values.SetError("error creating Date day_near_current_date should be a string number or an int : {0}", v); Trace.WriteLine("error creating Date day_near_current_date should be a string number or an int : {0}", v); //return false; } } //string name = null; //if (values.ContainsKey("year")) // name = "year"; //else if (values.ContainsKey("year1")) // name = "year1"; //else if (values.ContainsKey("year2")) // name = "year2"; v = null; if (values.ContainsKey("year1")) { v = values["year1"]; if (v is ZString && string.IsNullOrEmpty((string)v)) { v = null; } } if (v == null && values.ContainsKey("year2")) { v = values["year2"]; if (v is ZString && string.IsNullOrEmpty((string)v)) { v = null; } } if (v == null && values.ContainsKey("year")) { v = values["year"]; if (v is ZString && string.IsNullOrEmpty((string)v)) { v = null; } } if (v != null) { //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { int year; if (int.TryParse((string)v, out year)) { dateValues.year = year; } else { //values.SetError("error creating Date year is'nt a number : \"{0}\"", v); Trace.WriteLine("error creating Date year is'nt a number : \"{0}\"", v); //return false; } } else if (v is ZInt) { dateValues.year = (int)v; } else if (v != null) { //values.SetError("error creating Date year should be a string number or an int : {0}", v); Trace.WriteLine("error creating Date year should be a string number or an int : {0}", v); //return false; } } v = null; if (values.ContainsKey("month1")) { v = values["month1"]; if (v is ZString && string.IsNullOrEmpty((string)v)) { v = null; } } if (v == null && values.ContainsKey("month2")) { v = values["month2"]; if (v is ZString && string.IsNullOrEmpty((string)v)) { v = null; } } if (v == null && values.ContainsKey("month")) { v = values["month"]; if (v is ZString && string.IsNullOrEmpty((string)v)) { v = null; } } if (v != null) { //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { int month; if (int.TryParse((string)v, out month)) { dateValues.month = month; } else { month = zdate.GetMonthNumber((string)v); if (month != 0) { dateValues.month = month; } else { //values.SetError("error creating Date invalid month : \"{0}\"", v); Trace.WriteLine("error creating Date invalid month : \"{0}\"", v); //return false; } } } else if (v is ZInt) { dateValues.month = (int)v; } else if (v != null) { //values.SetError("error creating Date month should be a string number or an int : {0}", v); Trace.WriteLine("error creating Date month should be a string number or an int : {0}", v); //return false; } } if (values.ContainsKey("day")) { v = values["day"]; //if (v is ZStringArray) // v = ((ZStringArray)v).SelectArrayValue(); if (v is ZString) { int day; //if ((string)v == "1er") if (string.Equals((string)v, "1er", StringComparison.InvariantCultureIgnoreCase)) { dateValues.day = 1; } else if (int.TryParse((string)v, out day)) { dateValues.day = day; } else { //values.SetError("error creating Date day is'nt a number : \"{0}\"", v); Trace.WriteLine("error creating Date day is'nt a number : \"{0}\"", v); //return false; } } else if (v is ZInt) { dateValues.day = (int)v; } else if (v != null) { //values.SetError("error creating Date day should be a string number or an int : {0}", v); //Trace.WriteLine("error creating Date day should be a string number or an int : value {0} type {1}", v != null ? v : "(null)", v != null ? v.GetType().zName() : "(null)"); Trace.WriteLine("error creating Date day should be a string number or an int : value {0} type {1}", v, v.GetType().zGetTypeName()); //return false; } } return(dateValues); }