/// <summary> /// Returns a string representation of the date/time in an ISO-8601-like format. The function will /// omit higher-precision parts whose values are zeroes, as permitted by the standard. /// </summary> /// <param name="datetime">Date/time to convert.</param> /// <param name="format">One of the several pre-defined formats to use.</param> /// <param name="minPrecision">Minimum precision of the resulting string. The actual precision is determined by what's available in the date/time, bounded by this parameter.</param> /// <param name="maxPrecision">Maximum precision of the resulting string. Any higher-precision parts are truncated.</param> /// <param name="includeTimezone">Specifies whether a suffix indicating date/time kind (local/utc/unspecified) and, for local times, a UTC offset, is appended.</param> public static string ToIsoStringOptimal(this DateTime datetime, IsoDateFormat format = IsoDateFormat.HumanReadable, IsoDatePrecision minPrecision = IsoDatePrecision.Days, IsoDatePrecision maxPrecision = IsoDatePrecision.Full, bool includeTimezone = false) { if (minPrecision > maxPrecision) { throw new ArgumentException("Minimum precision must not exceed maximum precision.", nameof(maxPrecision)); } IsoDatePrecision precision; if (datetime.Nanosecond() % 1000000 != 0 && maxPrecision > IsoDatePrecision.Milliseconds) { precision = IsoDatePrecision.Full; } else if (datetime.Millisecond != 0 && maxPrecision > IsoDatePrecision.Seconds) { precision = IsoDatePrecision.Milliseconds; } else if (datetime.Second != 0 && maxPrecision > IsoDatePrecision.Minutes) { precision = IsoDatePrecision.Seconds; } else if (datetime.Minute != 0 || datetime.Hour != 0 && maxPrecision > IsoDatePrecision.Days) { precision = IsoDatePrecision.Minutes; } else { precision = IsoDatePrecision.Days; } if (precision < minPrecision) { precision = minPrecision; } if (precision > maxPrecision) { precision = maxPrecision; } return(datetime.ToIsoString(precision, format, includeTimezone: includeTimezone)); }
/// <summary> /// Returns a string representation of the date/time in an ISO-8601-like format. Use this if the result must be round-trippable /// without losing any information. The function will omit higher-precision parts whose values are zeroes, as permitted by the standard. /// </summary> /// <param name="datetime">Date/time to convert.</param> /// <param name="format">One of the several pre-defined formats to use.</param> /// <param name="minPrecision">Minimum precision of the resulting string. The actual precision is determined by what's available in the date/time, bounded by this parameter.</param> public static string ToIsoStringRoundtrip(this DateTime datetime, IsoDateFormat format = IsoDateFormat.HumanReadable, IsoDatePrecision minPrecision = IsoDatePrecision.Days) { return datetime.ToIsoStringOptimal(format, minPrecision: minPrecision, maxPrecision: IsoDatePrecision.Full, includeTimezone: true); }
/// <summary> /// Returns a string representation of the date/time in an ISO-8601-like format. The function will /// omit higher-precision parts whose values are zeroes, as permitted by the standard. /// </summary> /// <param name="datetime">Date/time to convert.</param> /// <param name="format">One of the several pre-defined formats to use.</param> /// <param name="minPrecision">Minimum precision of the resulting string. The actual precision is determined by what's available in the date/time, bounded by this parameter.</param> /// <param name="maxPrecision">Maximum precision of the resulting string. Any higher-precision parts are truncated.</param> /// <param name="includeTimezone">Specifies whether a suffix indicating date/time kind (local/utc/unspecified) and, for local times, a UTC offset, is appended.</param> public static string ToIsoStringOptimal(this DateTime datetime, IsoDateFormat format = IsoDateFormat.HumanReadable, IsoDatePrecision minPrecision = IsoDatePrecision.Days, IsoDatePrecision maxPrecision = IsoDatePrecision.Full, bool includeTimezone = false) { if (minPrecision > maxPrecision) throw new ArgumentException("Minimum precision must not exceed maximum precision.", "maxPrecision"); IsoDatePrecision precision; if (datetime.Nanosecond() % 1000000 != 0 && maxPrecision > IsoDatePrecision.Milliseconds) precision = IsoDatePrecision.Full; else if (datetime.Millisecond != 0 && maxPrecision > IsoDatePrecision.Seconds) precision = IsoDatePrecision.Milliseconds; else if (datetime.Second != 0 && maxPrecision > IsoDatePrecision.Minutes) precision = IsoDatePrecision.Seconds; else if (datetime.Minute != 0 || datetime.Hour != 0 && maxPrecision > IsoDatePrecision.Days) precision = IsoDatePrecision.Minutes; else precision = IsoDatePrecision.Days; if (precision < minPrecision) precision = minPrecision; if (precision > maxPrecision) precision = maxPrecision; return datetime.ToIsoString(precision, format, includeTimezone: includeTimezone); }
/// <summary>Returns a string representation of the date/time in an ISO-8601 compatible (or very close) format.</summary> /// <param name="datetime">Date/time to convert.</param> /// <param name="precision">Which date/time components are to be included. The values are truncated, not rounded.</param> /// <param name="format">One of the several pre-defined formats to use.</param> /// <param name="includeTimezone">Specifies whether a suffix indicating date/time kind (local/utc/unspecified) and, for local times, a UTC offset, is appended.</param> public static string ToIsoString(this DateTime datetime, IsoDatePrecision precision = IsoDatePrecision.Seconds, IsoDateFormat format = IsoDateFormat.HumanReadable, bool includeTimezone = false) { switch (format) { case IsoDateFormat.HumanReadable: return datetime.ToIsoStringCustom(precision, charInDate: '-', charInTime: ':', charBetween: ' ', includeTimezone: includeTimezone); case IsoDateFormat.Compact: return datetime.ToIsoStringCustom(precision, charInDate: null, charInTime: null, charBetween: 'T', includeTimezone: includeTimezone); case IsoDateFormat.CompactReadable: return datetime.ToIsoStringCustom(precision, charInDate: null, charInTime: null, charBetween: '-', includeTimezone: includeTimezone); case IsoDateFormat.FilenameReadable: return datetime.ToIsoStringCustom(precision, charInDate: '.', charInTime: '.', charBetween: '-', includeTimezone: includeTimezone); case IsoDateFormat.Iso8601: return datetime.ToIsoStringCustom(precision, charInDate: '-', charInTime: ':', charBetween: 'T', includeTimezone: includeTimezone); default: throw new InternalErrorException("usbwdg"); } }
/// <summary> /// Returns a string representation of the date/time in an ISO-8601-like format. Use this if the result must be round-trippable /// without losing any information. The function will omit higher-precision parts whose values are zeroes, as permitted by the standard. /// </summary> /// <param name="datetime">Date/time to convert.</param> /// <param name="format">One of the several pre-defined formats to use.</param> /// <param name="minPrecision">Minimum precision of the resulting string. The actual precision is determined by what's available in the date/time, bounded by this parameter.</param> public static string ToIsoStringRoundtrip(this DateTime datetime, IsoDateFormat format = IsoDateFormat.HumanReadable, IsoDatePrecision minPrecision = IsoDatePrecision.Days) { return(datetime.ToIsoStringOptimal(format, minPrecision: minPrecision, maxPrecision: IsoDatePrecision.Full, includeTimezone: true)); }
/// <summary>Returns a string representation of the date/time in an ISO-8601 compatible (or very close) format.</summary> /// <param name="datetime">Date/time to convert.</param> /// <param name="precision">Which date/time components are to be included. The values are truncated, not rounded.</param> /// <param name="format">One of the several pre-defined formats to use.</param> /// <param name="includeTimezone">Specifies whether a suffix indicating date/time kind (local/utc/unspecified) and, for local times, a UTC offset, is appended.</param> public static string ToIsoString(this DateTime datetime, IsoDatePrecision precision = IsoDatePrecision.Seconds, IsoDateFormat format = IsoDateFormat.HumanReadable, bool includeTimezone = false) { switch (format) { case IsoDateFormat.HumanReadable: return(datetime.ToIsoStringCustom(precision, charInDate: '-', charInTime: ':', charBetween: ' ', includeTimezone: includeTimezone)); case IsoDateFormat.Compact: return(datetime.ToIsoStringCustom(precision, charInDate: null, charInTime: null, charBetween: 'T', includeTimezone: includeTimezone)); case IsoDateFormat.CompactReadable: return(datetime.ToIsoStringCustom(precision, charInDate: null, charInTime: null, charBetween: '-', includeTimezone: includeTimezone)); case IsoDateFormat.FilenameReadable: return(datetime.ToIsoStringCustom(precision, charInDate: '.', charInTime: '.', charBetween: '-', includeTimezone: includeTimezone)); case IsoDateFormat.Iso8601: return(datetime.ToIsoStringCustom(precision, charInDate: '-', charInTime: ':', charBetween: 'T', includeTimezone: includeTimezone)); default: throw new Exception("usbwdg"); } }