/// <summary> /// Implements the following function /// string date2:month-name(string, string?) /// See http://www.xmland.net/exslt/doc/GDNDatesAndTimes-month-name.xml /// </summary> /// <remarks>THIS FUNCTION IS NOT PART OF EXSLT!!!</remarks> /// <returns>The month name of the specified date according to /// specified culture or the empty string if the input date is invalid or /// the culture isn't supported.</returns> public string monthName(string d, string culture) { try { DateTZ date = new DateTZ(d); CultureInfo ci = new CultureInfo(culture); return(ci.DateTimeFormat.GetMonthName(date.d.Month)); } catch (Exception) { return(""); } }
/// <summary> /// Implements the following function /// string date2:day-abbreviation(string, string) /// See http://www.xmland.net/exslt/doc/GDNDatesAndTimes-day-abbreviation.xml /// </summary> /// <remarks>THIS FUNCTION IS NOT PART OF EXSLT!!!</remarks> /// <returns>The abbreviated day name of the specified date according to /// specified culture or the empty string if the input date is invalid or /// the culture isn't supported.</returns> public string dayAbbreviation(string d, string culture) { try { DateTZ date = new DateTZ(d); CultureInfo ci = new CultureInfo(culture); return(ci.DateTimeFormat.GetAbbreviatedDayName(date.d.DayOfWeek)); } catch (Exception) { return(""); } }
/// <summary> /// Implements the following function /// string date:date(string) /// </summary> /// <returns>The date part of the specified date or the empty string if the /// date is invalid</returns> public string date(string d) { try { DateTZ dtz = new DateTZ(d); return dtz.ToString(); } catch (FormatException) { return ""; } }
/// <summary> /// Implements the following function /// string date:date() /// </summary> /// <returns>The current date</returns> public string date() { DateTZ dtz = new DateTZ(); return dtz.ToString(); }
/// <summary> /// Initialize the structure with the current date, time and timezone /// </summary> public static ExsltDateTime ParseDate(string d) { // Try each potential class, from most specific to least specific. // First DateTimeTZ try { DateTimeTZ t = new DateTimeTZ(d); return t; } catch (FormatException) { } // Next Date try { DateTZ t = new DateTZ(d); return t; } catch (FormatException) { } // Next YearMonth try { YearMonth t = new YearMonth(d); return t; } catch (FormatException) { } // Finally Year -- don't catch the exception for the last type { YearTZ t = new YearTZ(d); return t; } }
/// <summary> /// Implements the following function /// string:date:difference(string, string) /// </summary> /// <param name="start">The start date</param> /// <param name="end">The end date</param> /// <returns>A positive difference if start is before end otherwise a negative /// difference. The difference is in the ISO 8601 date difference format as either /// [-]P[yY][mM] /// or /// [-]P[dD][T][hH][mM][sS] /// P means a difference and is required /// At least one of Y M D H M S is required /// If a higher order component is 0, it is suppressed (for example, if there are 0 years and /// 1 month, then Y is suppressed. If there are 1 years and 0 months, M is not suppressed) /// If the input format is yyyy-MM or yyyy, then the output is [-]P[yY][mM] /// If the input format includes days, hours, minutes or seconds, then the output is /// [-]P[dD][T][hH][mM][sS]. /// If there H M and S are all 0, the T is suppressed. /// </returns> public string difference(string start, string end) { try { ExsltDateTime startdate = ExsltDateTimeFactory.ParseDate(start); ExsltDateTime enddate = ExsltDateTimeFactory.ParseDate(end); // The rules are pretty tricky. basically, interpret both strings as the least- // specific format if (Object.ReferenceEquals(startdate.GetType(), typeof(YearTZ)) || Object.ReferenceEquals(enddate.GetType(), typeof(YearTZ))) { StringBuilder retString = new StringBuilder(""); int yearDiff = enddate.d.Year - startdate.d.Year; if (yearDiff < 0) retString.Append('-'); retString.Append('P'); retString.Append(Math.Abs(yearDiff)); retString.Append('Y'); return retString.ToString(); } else if (Object.ReferenceEquals(startdate.GetType(), typeof(YearMonth)) || Object.ReferenceEquals(enddate.GetType(), typeof(YearMonth))) { StringBuilder retString = new StringBuilder(""); int yearDiff = enddate.d.Year - startdate.d.Year; int monthDiff = enddate.d.Month - startdate.d.Month; // Borrow from the year if necessary if ((yearDiff > 0) && (Math.Sign(monthDiff) == -1)) { yearDiff--; monthDiff += 12; } else if ((yearDiff < 0) && (Math.Sign(monthDiff) == 1)) { yearDiff++; monthDiff -= 12; } if ((yearDiff < 0) || ((yearDiff == 0) && (monthDiff < 0))) { retString.Append('-'); } retString.Append('P'); if (yearDiff != 0) { retString.Append(Math.Abs(yearDiff)); retString.Append('Y'); } retString.Append(Math.Abs(monthDiff)); retString.Append('M'); return retString.ToString(); } else { // Simulate casting to the most truncated format. i.e. if one // Arg is DateTZ and the other is DateTimeTZ, get rid of the time // for both. if (Object.ReferenceEquals(startdate.GetType(), typeof(DateTZ)) || Object.ReferenceEquals(enddate.GetType(), typeof(DateTZ))) { startdate = new DateTZ(startdate.d.ToString("yyyy-MM-dd")); enddate = new DateTZ(enddate.d.ToString("yyyy-MM-dd")); } TimeSpan ts = enddate.d.Subtract(startdate.d); return XmlConvert.ToString(ts); } } catch (FormatException) { return ""; } }
/// <summary> /// Implements the following function /// string date:day-abbreviation(string) /// </summary> /// <returns>The abbreviated English name of the day of the specified date or the /// empty string if the input date is invalid</returns> public string dayAbbreviation(string d) { try { DateTZ date = new DateTZ(d); return dayAbbreviation((int)date.d.DayOfWeek); } catch (FormatException) { return ""; } }
/// <summary> /// Implements the following function /// number date:day-of-week-in-month(string) /// </summary> /// <returns>The day part of the specified date or NaN if the /// date is invalid</returns> public double dayOfWeekInMonth(string d) { try { DateTZ date = new DateTZ(d); return this.dayOfWeekInMonth(date.d.Day); } catch (FormatException) { return System.Double.NaN; } }
/// <summary> /// Implements the following function /// number date:day-in-week(string) /// </summary> /// <returns>The day in the week of the specified date or NaN if the /// date is invalid. The current day in the week. 1=Sunday, 2=Monday,...,7=Saturday /// </returns> public double dayInWeek(string d) { try { DateTZ date = new DateTZ(d); return ((int)date.d.DayOfWeek) + 1; } catch (FormatException) { return System.Double.NaN; } }
/// <summary> /// Implements the following function /// number date:day-in-year(string) /// </summary> /// <returns> /// The day part of the specified date or NaN if the date is invalid /// </returns> public double dayInYear(string d) { try { DateTZ date = new DateTZ(d); return date.d.DayOfYear; } catch (FormatException) { return System.Double.NaN; } }
/// <summary> /// Implements the following function /// number date:week-in-year(string) /// </summary> /// <returns>The week part of the specified date or the empty string if the /// date is invalid</returns> /// <remarks>Does not support dates in the format of the xs:yearMonth or /// xs:gYear types. This method uses the Calendar.GetWeekOfYear() method /// with the CalendarWeekRule and FirstDayOfWeek of the current culture. /// THE RESULTS OF CALLING THIS FUNCTION VARIES ACROSS CULTURES</remarks> public double weekInYear(string d) { try { DateTZ dtz = new DateTZ(d); return weekInYear(dtz.d); } catch (FormatException) { return System.Double.NaN; } }
/// <summary> /// Implements the following function /// string date2:month-name(string, string?) /// See http://www.xmland.net/exslt/doc/GDNDatesAndTimes-month-name.xml /// </summary> /// <remarks>THIS FUNCTION IS NOT PART OF EXSLT!!!</remarks> /// <returns>The month name of the specified date according to /// specified culture or the empty string if the input date is invalid or /// the culture isn't supported.</returns> public string monthName(string d, string culture) { try { DateTZ date = new DateTZ(d); CultureInfo ci = new CultureInfo(culture); return ci.DateTimeFormat.GetMonthName(date.d.Month); } catch (Exception) { return ""; } }
/// <summary> /// Implements the following function /// string date2:day-abbreviation(string, string) /// See http://www.xmland.net/exslt/doc/GDNDatesAndTimes-day-abbreviation.xml /// </summary> /// <remarks>THIS FUNCTION IS NOT PART OF EXSLT!!!</remarks> /// <returns>The abbreviated day name of the specified date according to /// specified culture or the empty string if the input date is invalid or /// the culture isn't supported.</returns> public string dayAbbreviation(string d, string culture) { try { DateTZ date = new DateTZ(d); CultureInfo ci = new CultureInfo(culture); return ci.DateTimeFormat.GetAbbreviatedDayName(date.d.DayOfWeek); } catch (Exception) { return ""; } }