Example #1
0
        /// <summary>
        /// Converts DateTime to String.
        /// </summary>
        internal static String ToString(DateTime value, JSonWriterDateFormat format)
        {
            DateTime valueAsUniversal = value.ToUniversalTime();
            long     milliseconds     = 0;

            switch (format)
            {
            case JSonWriterDateFormat.Default:
                return(valueAsUniversal.ToString("u", CultureInfo.InvariantCulture));

            case JSonWriterDateFormat.JavaScript:
                milliseconds = (valueAsUniversal.Ticks - DateTimeHelper.TicksAt1970) / TimeSpan.TicksPerMillisecond;

                // is before 1970-01-01?
                if (milliseconds < 0)
                {
                    // use a direct year-month-day constructor, if no hour-minute-second-millisecond set:
                    if ((valueAsUniversal.Ticks % TimeSpan.TicksPerDay) == 0)
                    {
                        return(string.Concat("\\/Date(", valueAsUniversal.Year, ",", valueAsUniversal.Month, ",", valueAsUniversal.Day, ")\\/"));
                    }

                    // or full date+time:
                    return(string.Concat("\\/Date(", valueAsUniversal.Year, ",", valueAsUniversal.Month, ",", valueAsUniversal.Day, ",",
                                         valueAsUniversal.Hour, ",", valueAsUniversal.Minute, ",", valueAsUniversal.Second, ",", valueAsUniversal.Millisecond, ")\\/"));
                }

                // or by default only miliseconds if date >= 1970-01-01:
                return(string.Concat("\\/Date(", milliseconds, ")\\/"));

            case JSonWriterDateFormat.UnixEpochSeconds:

                // is before 1970-01-01?
                if (valueAsUniversal.Ticks < DateTimeHelper.TicksAt1970)
                {
                    throw new FormatException("Date is too early, should be after 1970-01-01");
                }

                long seconds = (valueAsUniversal.Ticks - DateTimeHelper.TicksAt1970) / TimeSpan.TicksPerSecond;
                return(seconds.ToString());

            case JSonWriterDateFormat.UnixEpochMilliseconds:

                // is before 1970-01-01?
                if (valueAsUniversal.Ticks < DateTimeHelper.TicksAt1970)
                {
                    throw new FormatException("Date is too early, should be after 1970-01-01");
                }

                milliseconds = (valueAsUniversal.Ticks - DateTimeHelper.TicksAt1970) / TimeSpan.TicksPerMillisecond;
                return(milliseconds.ToString());

            case JSonWriterDateFormat.Ticks:
                return(valueAsUniversal.Ticks.ToString());

            default:
                throw new FormatException(string.Concat("Unsupported format specified (", format, ")"));
            }
        }
Example #2
0
 public void WriteMember(string name, DateTime value, JSonWriterDateFormat format)
 {
     _output.WriteMember(name, value, format);
 }
Example #3
0
 public void WriteValue(DateTime value, JSonWriterDateFormat format)
 {
     _output.WriteValue(value, format);
 }
Example #4
0
 /// <summary>
 /// Gets an indication, if given data-time format requires to be serialized as number or quoted string.
 /// </summary>
 internal static bool ToQuoteValue(JSonWriterDateFormat format)
 {
     return format == JSonWriterDateFormat.Default || format == JSonWriterDateFormat.JavaScript;
 }
Example #5
0
        /// <summary>
        /// Converts DateTime to String.
        /// </summary>
        internal static String ToString(DateTime value, JSonWriterDateFormat format)
        {
            DateTime valueAsUniversal = value.ToUniversalTime();
            long milliseconds = 0;

            switch (format)
            {
                case JSonWriterDateFormat.Default:
                    return valueAsUniversal.ToString("u", CultureInfo.InvariantCulture);

                case JSonWriterDateFormat.JavaScript:
                    milliseconds = (valueAsUniversal.Ticks - DateTimeHelper.TicksAt1970) / TimeSpan.TicksPerMillisecond;

                    // is before 1970-01-01?
                    if (milliseconds < 0)
                    {
                        // use a direct year-month-day constructor, if no hour-minute-second-millisecond set:
                        if ((valueAsUniversal.Ticks % TimeSpan.TicksPerDay) == 0)
                            return string.Concat("\\/Date(", valueAsUniversal.Year, ",", valueAsUniversal.Month, ",", valueAsUniversal.Day, ")\\/");

                        // or full date+time:
                        return string.Concat("\\/Date(", valueAsUniversal.Year, ",", valueAsUniversal.Month, ",", valueAsUniversal.Day, ",",
                                             valueAsUniversal.Hour, ",", valueAsUniversal.Minute, ",", valueAsUniversal.Second, ",", valueAsUniversal.Millisecond, ")\\/");
                    }

                    // or by default only miliseconds if date >= 1970-01-01:
                    return string.Concat("\\/Date(", milliseconds, ")\\/");

                case JSonWriterDateFormat.UnixEpochSeconds:

                    // is before 1970-01-01?
                    if (valueAsUniversal.Ticks < DateTimeHelper.TicksAt1970)
                        throw new FormatException("Date is too early, should be after 1970-01-01");

                    long seconds = (valueAsUniversal.Ticks - DateTimeHelper.TicksAt1970) / TimeSpan.TicksPerSecond;
                    return seconds.ToString();

                case JSonWriterDateFormat.UnixEpochMilliseconds:

                    // is before 1970-01-01?
                    if (valueAsUniversal.Ticks < DateTimeHelper.TicksAt1970)
                        throw new FormatException("Date is too early, should be after 1970-01-01");

                    milliseconds = (valueAsUniversal.Ticks - DateTimeHelper.TicksAt1970) / TimeSpan.TicksPerMillisecond;
                    return milliseconds.ToString();

                case JSonWriterDateFormat.Ticks:
                    return valueAsUniversal.Ticks.ToString();

                default:
                    throw new FormatException(string.Concat("Unsupported format specified (", format, ")"));
            }
        }
Example #6
0
 /// <summary>
 /// Writes DateTime value.
 /// It can be used only as an array element or value for object member.
 /// Before writing, the date is converted into universal time representation and written as string.
 /// </summary>
 public void WriteValue(DateTime value, JSonWriterDateFormat format)
 {
     WriteValueInternal(ToString(value, format), ToQuoteValue(format));
 }
Example #7
0
 /// <summary>
 /// Writes a member with a value.
 /// It requires to be put inside an object.
 /// Before writing, the date is converted into universal time representation and written as string.
 /// </summary>
 public void WriteMember(string name, DateTime value, JSonWriterDateFormat format)
 {
     WriteMemberInternal(name, ToString(value, format), ToQuoteValue(format));
 }
Example #8
0
 public void WriteMember(string name, DateTime value, JSonWriterDateFormat format)
 {
     throw new NotImplementedException();
 }
Example #9
0
 public void WriteValue(DateTime value, JSonWriterDateFormat format)
 {
     throw new NotImplementedException();
 }
Example #10
0
 public void WriteMember(string name, DateTime value, JSonWriterDateFormat format)
 {
     throw new NotImplementedException();
 }
Example #11
0
 public void WriteValue(DateTime value, JSonWriterDateFormat format)
 {
     throw new NotImplementedException();
 }
Example #12
0
 /// <summary>
 /// Gets an indication, if given data-time format requires to be serialized as number or quoted string.
 /// </summary>
 internal static bool ToQuoteValue(JSonWriterDateFormat format)
 {
     return(format == JSonWriterDateFormat.Default || format == JSonWriterDateFormat.JavaScript);
 }
Example #13
0
 /// <summary>
 /// Writes DateTime value.
 /// It can be used only as an array element or value for object member.
 /// Before writing, the date is converted into universal time representation and written as string.
 /// </summary>
 public void WriteValue(DateTime value, JSonWriterDateFormat format)
 {
     WriteValueInternal(ToString(value, format), ToQuoteValue(format));
 }
Example #14
0
 /// <summary>
 /// Writes a member with a value.
 /// It requires to be put inside an object.
 /// Before writing, the date is converted into universal time representation and written as string.
 /// </summary>
 public void WriteMember(string name, DateTime value, JSonWriterDateFormat format)
 {
     WriteMemberInternal(name, ToString(value, format), ToQuoteValue(format));
 }
Example #15
0
 public void WriteMember(string name, DateTime value, JSonWriterDateFormat format)
 {
     _output.WriteMember(name, value, format);
 }
Example #16
0
 public void WriteValue(DateTime value, JSonWriterDateFormat format)
 {
     _output.WriteValue(value, format);
 }