/// <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, ")")); } }
public void WriteMember(string name, DateTime value, JSonWriterDateFormat format) { _output.WriteMember(name, value, format); }
public void WriteValue(DateTime value, JSonWriterDateFormat format) { _output.WriteValue(value, format); }
/// <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; }
/// <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, ")")); } }
/// <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)); }
/// <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)); }
public void WriteMember(string name, DateTime value, JSonWriterDateFormat format) { throw new NotImplementedException(); }
public void WriteValue(DateTime value, JSonWriterDateFormat format) { throw new NotImplementedException(); }
/// <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); }