/// <summary> /// /// </summary> /// <param name="mappings"></param> /// <param name="outputDateTimeKind"></param> /// <param name="customOffset"></param> public CustomXmlTransformer(IEnumerable <CustomXmlTransformRule> mappings, OutputDateTimeKind outputDateTimeKind, TimeSpan?customOffset) { _outputDateTimeKind = outputDateTimeKind; _customOffset = customOffset; foreach (CustomXmlTransformRule mapping in mappings) { _mappings[mapping.Key] = mapping; } }
/// <summary> /// /// </summary> /// <param name="outputDateTimeKind"></param> /// <param name="customOffset"></param> /// <param name="includeDatePart"></param> /// <param name="includeTimePart"></param> /// <param name="includeZonePart"></param> /// <returns></returns> public string ToString(OutputDateTimeKind outputDateTimeKind, TimeSpan?customOffset, bool?includeDatePart, bool?includeTimePart, bool?includeZonePart) // true=include, false=exclude, null=same as input { if ((outputDateTimeKind == OutputDateTimeKind.ConvertToCustom || outputDateTimeKind == OutputDateTimeKind.UnspecifiedOrCustom) && customOffset == null) { throw new ArgumentNullException(nameof(customOffset)); } switch (outputDateTimeKind) { case OutputDateTimeKind.SameAsInput: switch (InputDateTimeKind) { case DateTimeKind.Local: outputDateTimeKind = OutputDateTimeKind.ConvertToCustom; customOffset = ZonePart; break; case DateTimeKind.Utc: outputDateTimeKind = OutputDateTimeKind.ConvertToUniversal; break; default: outputDateTimeKind = OutputDateTimeKind.ConvertToUnspecified; break; } break; case OutputDateTimeKind.UnspecifiedOrUniversal: switch (InputDateTimeKind) { case DateTimeKind.Local: case DateTimeKind.Utc: outputDateTimeKind = OutputDateTimeKind.ConvertToUniversal; break; default: outputDateTimeKind = OutputDateTimeKind.ConvertToUnspecified; break; } break; case OutputDateTimeKind.UnspecifiedOrLocal: switch (InputDateTimeKind) { case DateTimeKind.Local: case DateTimeKind.Utc: outputDateTimeKind = OutputDateTimeKind.ConvertToLocalTime; break; default: outputDateTimeKind = OutputDateTimeKind.ConvertToUnspecified; break; } break; case OutputDateTimeKind.UnspecifiedOrCustom: switch (InputDateTimeKind) { case DateTimeKind.Local: case DateTimeKind.Utc: outputDateTimeKind = OutputDateTimeKind.ConvertToCustom; break; default: outputDateTimeKind = OutputDateTimeKind.ConvertToUnspecified; break; } break; } // convert to output zone DateTimeOffset outputValue = InputAsDateTimeOffset; switch (outputDateTimeKind) { case OutputDateTimeKind.ConvertToUnspecified: break; case OutputDateTimeKind.ConvertToUniversal: outputValue = InputAsDateTimeOffset.ToUniversalTime(); break; case OutputDateTimeKind.ConvertToLocalTime: outputValue = InputAsDateTimeOffset.ToLocalTime(); break; case OutputDateTimeKind.ConvertToCustom: if (customOffset != null) { outputValue = InputAsDateTimeOffset.ToOffset(customOffset.Value); } break; default: throw new NotImplementedException(); } string result = ""; // date part handling if (includeDatePart.HasValue) { if (includeDatePart.Value) { // forcibly include result += outputValue.Date.ToString("yyyy-MM-dd"); if ((includeTimePart.HasValue && includeTimePart.Value) || (TimePart.HasValue && (TimePart.Value != TimeSpan.Zero))) { result += "T"; } } else { // forcibly exclude } } else { // default if (DatePart.HasValue) { result += outputValue.Date.ToString("yyyy-MM-dd"); if ((includeTimePart.HasValue && includeTimePart.Value) || (TimePart.HasValue && (TimePart.Value != TimeSpan.Zero))) { result += "T"; } } } // time part handling if (includeTimePart.HasValue) { if (includeTimePart.Value) { // forcibly include TimeSpan tod = outputValue.TimeOfDay; if (tod.Milliseconds == 0) { result += tod.ToString(@"hh\:mm\:ss"); } else { result += tod.ToString(@"hh\:mm\:ss\.fffffff"); } } else { // forcibly exclude } } else { // default if (TimePart.HasValue) { if (DatePart.HasValue && TimePart.Value != TimeSpan.Zero || !DatePart.HasValue) { TimeSpan tod = outputValue.TimeOfDay; if (tod.Milliseconds == 0) { result += tod.ToString(@"hh\:mm\:ss"); } else { result += tod.ToString(@"hh\:mm\:ss\.fffffff"); } } } } // zone part handling if (includeZonePart.HasValue) { if (includeZonePart.Value) { // forcibly include if (outputValue.DateTime.Kind == DateTimeKind.Utc) { result += "Z"; } else { if (outputValue.Offset > TimeSpan.Zero) { result += "+" + outputValue.Offset.ToString(@"hh\:mm"); } else { result += "-" + outputValue.Offset.ToString(@"hh\:mm"); } } } else { // forcibly exclude } } else { // default switch (outputDateTimeKind) { case OutputDateTimeKind.ConvertToUniversal: result += "Z"; break; case OutputDateTimeKind.ConvertToLocalTime: case OutputDateTimeKind.ConvertToCustom: if (outputValue.Offset > TimeSpan.Zero) { result += "+" + outputValue.Offset.ToString(@"hh\:mm"); } else { result += "-" + outputValue.Offset.ToString(@"hh\:mm"); } break; } } return(result); }
/// <summary> /// /// </summary> /// <param name="outputDateTimeKind"></param> /// <param name="customOffset"></param> /// <returns></returns> public string ToString(OutputDateTimeKind outputDateTimeKind, TimeSpan?customOffset) { return(ToString(outputDateTimeKind, customOffset, null, null, null)); }