コード例 #1
0
        internal static string Serialize(Duration duration, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }
            if (duration is DesignatedDuration)
            {
                return DesignatedDurationSerializer.Serialize((DesignatedDuration)duration, options);
            }

            if (duration is CalendarDateTimeDuration)
            {
                return CalendarDateTimeDurationSerializer.Serialize((CalendarDateTimeDuration)duration, options);
            }

            if (duration is OrdinalDateTimeDuration)
            {
                return OrdinalDateTimeDurationSerializer.Serialize((OrdinalDateTimeDuration)duration, options);
            }

            if (duration is CalendarDateDuration)
            {
                return CalendarDateDurationSerializer.Serialize((CalendarDateDuration)duration, options);
            }

            if (duration is OrdinalDateDuration)
            {
                return OrdinalDateDurationSerializer.Serialize((OrdinalDateDuration)duration, options);
            }

            return TimeDurationSerializer.Serialize((TimeDuration)duration, options);
        }
コード例 #2
0
        internal static string Serialize(CalendarDateDuration calendarDateDuration, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            var output = new StringBuilder("P");

            if (calendarDateDuration.Centuries != null)
            {
                if (options.IsExpanded)
                {
                    if (calendarDateDuration.Centuries >= 0)
                    {
                        output.Append('+');
                    }
                }

                output.AppendFormat(calendarDateDuration.Centuries.Value.ToString("D" + (options.YearLength - 2)));

                return output.ToString();
            }

            if (options.IsExpanded)
            {
                if (calendarDateDuration.Years >= 0)
                {
                    output.Append('+');
                }
            }

            output.AppendFormat(calendarDateDuration.Years.ToString("D" + options.YearLength));

            if (calendarDateDuration.Months == null)
            {
                return output.ToString();
            }

            if (options.UseComponentSeparators)
            {
                output.Append('-');
            }

            output.AppendFormat("{0:D2}", calendarDateDuration.Months);

            if (calendarDateDuration.Days == null)
            {
                return output.ToString();
            }

            if (options.UseComponentSeparators)
            {
                output.Append('-');
            }

            output.AppendFormat("{0:D2}", calendarDateDuration.Days);

            return output.ToString();
        }
コード例 #3
0
        internal static string Serialize(OrdinalDateDuration ordinalDateDuration, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            var output = new StringBuilder("P");

            if (options.IsExpanded)
            {
                if (ordinalDateDuration.Years >= 0)
                {
                    output.Append('+');
                }
            }

            output.Append(ordinalDateDuration.Years.ToString("D" + options.YearLength));

            if (options.UseComponentSeparators)
            {
                output.Append('-');
            }

            output.AppendFormat("{0:D3}", ordinalDateDuration.Days);

            return output.ToString();
        }
コード例 #4
0
        internal static string Serialize(WeekDateTime dateTime, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            return dateTime.Date.ToString(options) + dateTime.Time.ToString(options);
        }
コード例 #5
0
        internal static string Serialize(CalendarDateTimeDuration dateTimeDuration, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            return dateTimeDuration.DateDuration.ToString(options) + dateTimeDuration.TimeDuration.ToString(options).Substring(1);
        }
コード例 #6
0
        internal static string Serialize(DesignatedDuration designatedDuration, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }
            var cultureInfo = options.DecimalSeparator == '.' ? CultureInfo.GetCultureInfo("en-US") : CultureInfo.GetCultureInfo("fr-FR");

            var output = new StringBuilder();

            output.Append('P');

            if (designatedDuration.Years != null)
            {
                output.AppendFormat(cultureInfo, "{0}Y", designatedDuration.Years);
            }

            if (designatedDuration.Months != null)
            {
                output.AppendFormat(cultureInfo, "{0}M", designatedDuration.Months);
            }

            if (designatedDuration.Days != null)
            {
                output.AppendFormat(cultureInfo, "{0}D", designatedDuration.Days);
            }

            if (designatedDuration.Hours != null || designatedDuration.Minutes != null || designatedDuration.Seconds != null)
            {
                output.Append('T');
            }

            if (designatedDuration.Hours != null)
            {
                output.AppendFormat(cultureInfo, "{0}H", designatedDuration.Hours);
            }

            if (designatedDuration.Minutes != null)
            {
                output.AppendFormat(cultureInfo, "{0}M", designatedDuration.Minutes);
            }

            if (designatedDuration.Seconds != null)
            {
                output.AppendFormat(cultureInfo, "{0}S", designatedDuration.Seconds);
            }

            if (designatedDuration.Weeks != null)
            {
                output.AppendFormat(cultureInfo, "{0}W", designatedDuration.Weeks);
            }

            return output.ToString();
        }
コード例 #7
0
        internal static string Serialize(StartEndTimeInterval interval, ISO8601Options startOptions, ISO8601Options endOptions)
        {
            if (startOptions == null)
            {
                startOptions = ISO8601Options.Default;
            }

            if (endOptions == null)
            {
                endOptions = ISO8601Options.Default;
            }

            return TimePointSerializer.Serialize(interval.Start, startOptions) + "/" + TimePointSerializer.Serialize(interval.End, endOptions);
        }
コード例 #8
0
        internal static string Serialize(DurationEndTimeInterval interval, ISO8601Options durationOptions, ISO8601Options endOptions)
        {
            if (durationOptions == null)
            {
                durationOptions = ISO8601Options.Default;
            }

            if (endOptions == null)
            {
                endOptions = ISO8601Options.Default;
            }

            return DurationSerializer.Serialize(interval.Duration, durationOptions) + "/" + TimePointSerializer.Serialize(interval.End, endOptions);
        }
コード例 #9
0
        internal static string Serialize(StartDurationTimeInterval interval, ISO8601Options startOptions, ISO8601Options durationOptions)
        {
            if (startOptions == null)
            {
                startOptions = ISO8601Options.Default;
            }

            if (durationOptions == null)
            {
                durationOptions = ISO8601Options.Default;
            }

            return TimePointSerializer.Serialize(interval.Start, startOptions) + "/" + DurationSerializer.Serialize(interval.Duration, durationOptions);
        }
コード例 #10
0
        internal static string Serialize(WeekDate weekDate, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            if (weekDate.Year < 0 || weekDate.Year > 9999)
            {
                options = (ISO8601Options)options.Clone();
                options.IsExpanded = true;
            }

            var output = new StringBuilder();

            if (options.IsExpanded)
            {
                if (weekDate.Year >= 0)
                {
                    output.Append('+');
                }
            }

            output.Append(weekDate.Year.ToString("D" + options.YearLength));

            if (options.UseComponentSeparators)
            {
                output.Append('-');
            }

            output.Append('W');
            output.Append(weekDate.Week.ToString("D2"));

            if (weekDate.Precision == WeekDatePrecision.Day)
            {
                if (options.UseComponentSeparators)
                {
                    output.Append('-');
                }

                output.Append(weekDate.Day);
            }

            return output.ToString();
        }
コード例 #11
0
        internal static string Serialize(TimeDuration timeDuration, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }
            var numberOptions = new NumberFormatInfo { NumberDecimalSeparator = options.DecimalSeparator.ToString() };

            var output = new StringBuilder("PT");

            if (timeDuration.Minutes == null)
            {
                output.AppendFormat(timeDuration.Hours.ToString("F" + options.FractionLength, numberOptions));

                return output.ToString();
            }

            output.AppendFormat("{0:##}", timeDuration.Hours);

            if (options.UseComponentSeparators)
            {
                output.Append(':');
            }

            if (timeDuration.Seconds == null)
            {
                output.AppendFormat(timeDuration.Minutes.Value.ToString("F" + options.FractionLength, numberOptions));

                return output.ToString();
            }

            output.AppendFormat("{0:##}", timeDuration.Minutes);

            if (options.UseComponentSeparators)
            {
                output.Append(':');
            }

            output.AppendFormat(timeDuration.Seconds.Value.ToString("F" + options.FractionLength, numberOptions));

            return output.ToString();
        }
コード例 #12
0
        internal static string Serialize(TimePoint timePoint, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            if (timePoint is CalendarDateTime)
            {
                return ((CalendarDateTime)timePoint).ToString(options);
            }

            if (timePoint is OrdinalDateTime)
            {
                return ((OrdinalDateTime)timePoint).ToString(options);
            }

            if (timePoint is WeekDateTime)
            {
                return ((WeekDateTime)timePoint).ToString(options);
            }

            if (timePoint is CalendarDate)
            {
                return ((CalendarDate)timePoint).ToString(options);
            }

            if (timePoint is OrdinalDate)
            {
                return ((OrdinalDate)timePoint).ToString(options);
            }

            if (timePoint is WeekDate)
            {
                return ((WeekDate)timePoint).ToString(options);
            }

            return ((Time)timePoint).ToString(options);
        }
コード例 #13
0
        internal static string Serialize(TimeInterval timeInterval, ISO8601Options leftOptions, ISO8601Options rightOptions)
        {
            if (leftOptions == null)
            {
                leftOptions = ISO8601Options.Default;
            }

            if (rightOptions == null)
            {
                rightOptions = ISO8601Options.Default;
            }

            if (timeInterval is StartEndTimeInterval)
            {
                return ((StartEndTimeInterval)timeInterval).ToString(leftOptions, rightOptions);
            }

            if (timeInterval is StartDurationTimeInterval)
            {
                return ((StartDurationTimeInterval)timeInterval).ToString(leftOptions, rightOptions);
            }

            return ((DurationEndTimeInterval)timeInterval).ToString(leftOptions, rightOptions);
        }
コード例 #14
0
ファイル: TimeSerializer.cs プロジェクト: nharren/ISO-8601
        internal static string Serialize(Time time, ISO8601Options options)
        {
            if (options == null)
            {
                options = ISO8601Options.Default;
            }

            var output = new StringBuilder();

            if (options.UseTimeDesignator)
            {
                output.Append('T');
            }

            var hourParts = time.Hour.ToString().Split('.', ',');

            if (time.Precision == TimePrecision.Hour && hourParts.Length > 1)
            {
                output.Append($"{int.Parse(hourParts[0]).ToString("D2")}{options.DecimalSeparator}{int.Parse(hourParts[1]).ToString("D" + options.FractionLength)}");
            }
            else
            {
                output.Append(int.Parse(hourParts[0]).ToString("D2"));
            }

            if (time.Precision != TimePrecision.Hour)
            {
                if (options.UseComponentSeparators)
                {
                    output.Append(":");
                }

                var minuteParts = time.Minute.ToString().Split('.', ',');

                if (time.Precision == TimePrecision.Minute && minuteParts.Length > 1)
                {
                    output.Append($"{int.Parse(minuteParts[0]).ToString("D2")}{options.DecimalSeparator}{int.Parse(minuteParts[1]).ToString("D" + options.FractionLength)}");
                }
                else
                {
                    output.Append(int.Parse(minuteParts[0]).ToString("D2"));
                }
            }

            if (time.Precision == TimePrecision.Second)
            {
                if (options.UseComponentSeparators)
                {
                    output.Append(":");
                }

                var secondParts = time.Second.ToString().Split('.', ',');

                if (secondParts.Length > 1)
                {
                    output.Append($"{int.Parse(secondParts[0]).ToString("D2")}{options.DecimalSeparator}{int.Parse(secondParts[1]).ToString("D" + options.FractionLength)}");
                }
                else
                {
                    output.Append(int.Parse(secondParts[0]).ToString("D2"));
                }
            }

            if (options.UseUtcOffset)
            {
                if (time.UtcOffset.Hours == 0 && time.UtcOffset.Minutes == 0)
                {
                    output.Append('Z');
                }
                else
                {
                    if (time.UtcOffset.Hours >= 0)
                    {
                        output.Append('+');
                    }

                    output.Append(time.UtcOffset.Hours.ToString("D2"));

                    if (time.UtcOffset.Minutes != 0)
                    {
                        if (options.UseComponentSeparators)
                        {
                            output.Append(':');
                        }

                        output.Append(time.UtcOffset.Minutes.ToString("D2"));
                    }
                }
            }

            return output.ToString();
        }