Exemplo n.º 1
0
        public static string ToStringISO8601(
            this DateTimeUtcAndLocal @this,
            UtcAndLocalTemplate template = DefaultUtcAndLocalTemplate,
            ISO8601Template dateTemplate = DefaultISO8601Template,
            ISO8601Format format         = DefaultISO8601Format)
        {
            var utcString   = template != UtcAndLocalTemplate.Local ? @this.Utc.ToStringISO8601(dateTemplate, format) : null;
            var localString = template != UtcAndLocalTemplate.Utc ? @this.Local.ToStringISO8601(dateTemplate, format) : null;

            string twoDatesFormatString;

            switch (format)
            {
            case ISO8601Format.Pretty:
                twoDatesFormatString = "{0} / {1}";
                break;

            case ISO8601Format.Formal:
                twoDatesFormatString = "{0} {1}";
                break;

            case ISO8601Format.FileSystemPathFriendly:
                twoDatesFormatString = "{0}--{1}";
                break;

            default:
                throw new ArgumentException("Illegal ISO8601Format");
            }

            switch (template)
            {
            case UtcAndLocalTemplate.Utc:
                return(utcString);

            case UtcAndLocalTemplate.Local:
                return(localString);

            case UtcAndLocalTemplate.UtcAndLocal:
                return(string.Format(twoDatesFormatString, utcString, localString));

            case UtcAndLocalTemplate.LocalAndUtc:
                return(string.Format(twoDatesFormatString, localString, utcString));

            default:
                throw new ArgumentException("Illegal UtcAndLocalTemplate");
            }
        }
Exemplo n.º 2
0
        public static string ToStringISO8601(this DateTime @this, ISO8601Template template = DefaultISO8601Template, ISO8601Format format = DefaultISO8601Format)
        {
            var suffix = @this.Kind == DateTimeKind.Utc ? "Z" : "";

            var dateFormatString  = "yyyy-MM-dd";
            var timeFormatString  = format != ISO8601Format.FileSystemPathFriendly ? "HH\\:mm\\:ss" : "HH\\_mm\\_ss";
            var dateTimeSeparator = format == ISO8601Format.Pretty ? ' ' : 'T';

            switch (template)
            {
            case ISO8601Template.DateOnly:
                return(@this.ToString(dateFormatString + suffix));

            case ISO8601Template.TimeOnly:
                return(@this.ToString(timeFormatString + suffix));

            case ISO8601Template.DateTime:
                return(@this.ToString(string.Format("{0}{1}{2}{3}", dateFormatString, dateTimeSeparator, timeFormatString, suffix)));

            default:
                throw new ArgumentException("Illegal ISO8601Template");
            }
        }