Ejemplo n.º 1
0
        private DateTimeOffset ParseDateTimeOffset(JsonReader reader)
        {
            switch (reader.TokenType)
            {
            // Return the default value of DateTimeOffset of the JSON value is NULL
            case JsonToken.Null:
                return(default);

            // If the token type is an integer, we assume UNIX time regardles of the format of the converter
            case JsonToken.Integer:
                return(TimeUtils.GetDateTimeOffsetFromUnixTime((long)reader.Value));

            // If the token type is an integer, we assume UNIX time regardles of the format of the converter
            case JsonToken.Float:
                return(TimeUtils.GetDateTimeOffsetFromUnixTime((double)reader.Value));

            // Is the value already a date? JSON.net may automatically detect and parse some date formats
            case JsonToken.Date:

                switch (reader.Value)
                {
                case DateTime dt:
                    return(dt);

                case DateTimeOffset dto:
                    return(dto);

                default:
                    throw new JsonSerializationException("Value doesn't match an instance of DateTime or DateTimeOffset: " + reader.Value.GetType());
                }

            case JsonToken.String:

                // Get the value as a string
                string value = (string)reader.Value;

                // Parse the string using the format of the converter
                switch (Format)
                {
                case TimeFormat.Iso8601:
                    return(Iso8601Utils.Parse(value));

                case TimeFormat.Rfc822:
                    return(Rfc822Utils.Parse(value));

                case TimeFormat.Rfc2822:
                    return(Rfc2822Utils.Parse(value));

                case TimeFormat.UnixTime:
                    return(TimeUtils.GetDateTimeOffsetFromUnixTime(value));

                default:
                    throw new JsonSerializationException("Unsupported format " + Format);
                }

            default:
                throw new JsonSerializationException("Unexpected token type: " + reader.TokenType);
            }
        }
        internal static object ToFormat(DateTimeOffset value, TimeFormat format)
        {
            switch (format)
            {
            case TimeFormat.Iso8601:
                return(Iso8601Utils.ToString(value));

            case TimeFormat.Rfc822:
                return(Rfc822Utils.ToString(value));

            case TimeFormat.Rfc2822:
                return(Rfc2822Utils.ToString(value));

            case TimeFormat.UnixTime:
                return((long)UnixTimeUtils.ToSeconds(value));

            default:
                throw new ArgumentException("Unsupported format " + format, nameof(format));
            }
        }
Ejemplo n.º 3
0
 public static string ToRfc2822(EssentialsDateTime timestamp)
 {
     return(Rfc2822Utils.ToString(timestamp));
 }
Ejemplo n.º 4
0
 public static string ToRfc2822(DateTimeOffset timestamp)
 {
     return(Rfc2822Utils.ToString(timestamp));
 }