Beispiel #1
0
        public void Serialize_UTC_Date()
        {
            var d = new DateTime(2001, 01, 01);

            d.ToJson().Print();
            "\"\\/Date(978325200000-0000)\\/\"".Print();
            d.ToUnixTime().ToString().Print();
            d.ToUnixTimeMs().ToString().Print();

            Assert.That(d.ToJson(), Is.EqualTo("\"\\/Date(978325200000-0000)\\/\""));
        }
		public static string ToWcfJsonDate(DateTime dateTime)
		{
			if (JsConfig.DateHandler == JsonDateHandler.ISO8601)
			{
				return EscapedWcfJsonPrefix + dateTime.ToString("o", CultureInfo.InvariantCulture) + EscapedWcfJsonSuffix;
			}

			var timestamp = dateTime.ToUnixTimeMs();
			var offset = dateTime.Kind == DateTimeKind.Utc
				? string.Empty
				: TimeZoneInfo.Local.GetUtcOffset(dateTime).ToTimeOffsetString();

			return EscapedWcfJsonPrefix + timestamp + offset + EscapedWcfJsonSuffix;
		}
        public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)
        {
            if (JsConfig.AssumeUtc && dateTime.Kind == DateTimeKind.Unspecified)
            {
                dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
            }

            if (JsConfig.DateHandler == JsonDateHandler.ISO8601)
            {
                writer.Write(dateTime.ToString("o", CultureInfo.InvariantCulture));
                return;
            }

            var timestamp = dateTime.ToUnixTimeMs();
            string offset = null;
            if (dateTime.Kind != DateTimeKind.Utc)
            {
                if (JsConfig.DateHandler == JsonDateHandler.TimestampOffset && dateTime.Kind == DateTimeKind.Unspecified)
                    offset = UnspecifiedOffset;
                else
                    offset = LocalTimeZone.GetUtcOffset(dateTime).ToTimeOffsetString();
            }
            else
            {
                // Normally the JsonDateHandler.TimestampOffset doesn't append an offset for Utc dates, but if
                // the JsConfig.AppendUtcOffset is set then we will
                if (JsConfig.DateHandler == JsonDateHandler.TimestampOffset && JsConfig.AppendUtcOffset.HasValue && JsConfig.AppendUtcOffset.Value)
                    offset = UtcOffset;
            }

            writer.Write(EscapedWcfJsonPrefix);
            writer.Write(timestamp);
            if (offset != null)
            {
                writer.Write(offset);
            }
            writer.Write(EscapedWcfJsonSuffix);
        }
 public void Can_Serialize_new_DateTime()
 {
     var newDateTime = new DateTime();
     var convertedUnixTimeMs = newDateTime.ToUnixTimeMs();
     Assert.That(convertedUnixTimeMs.FromUnixTimeMs(), Is.EqualTo(newDateTime));
 }
 public static string ToWcfJsonDate(DateTime dateTime)
 {
     return EscapedWcfJsonPrefix + dateTime.ToUnixTimeMs() + "+0000)\\/";
 }
		private void AssertDateIsEqual(DateTime dateTime)
		{
			//Don't test short dates without time to UTC as you lose precision
			var shortDateStr = dateTime.ToString(DateTimeSerializer.ShortDateTimeFormat);
			var shortDateTimeStr = dateTime.ToUniversalTime().ToString(DateTimeSerializer.XsdDateTimeFormatSeconds);
			var longDateTimeStr = dateTime.ToUniversalTime().ToString(DateTimeSerializer.XsdDateTimeFormat);
			var shortestDateStr = DateTimeSerializer.ToShortestXsdDateTimeString(dateTime);

			Log("{0} | {1} | {2}  [{3}]",
			    shortDateStr, shortDateTimeStr, longDateTimeStr, shortestDateStr);

			var shortDate = DateTimeSerializer.ParseShortestXsdDateTime(shortDateStr);
			var shortDateTime = DateTimeSerializer.ParseShortestXsdDateTime(shortDateTimeStr);
			var longDateTime = DateTimeSerializer.ParseShortestXsdDateTime(longDateTimeStr);

			Assert.That(shortDate, Is.EqualTo(dateTime.Date));

			var shortDateTimeUtc = shortDateTime.ToUniversalTime();
			Assert.That(shortDateTimeUtc, Is.EqualTo(
				new DateTime(
					shortDateTimeUtc.Year, shortDateTimeUtc.Month, shortDateTimeUtc.Day,
					shortDateTimeUtc.Hour, shortDateTimeUtc.Minute, shortDateTimeUtc.Second,
					shortDateTimeUtc.Millisecond, DateTimeKind.Utc)));
			
			Assert.That(longDateTime.ToUniversalTime(), Is.EqualTo(dateTime.ToUniversalTime()));

			var toDateTime = DateTimeSerializer.ParseShortestXsdDateTime(shortestDateStr);
			AssertDatesAreEqual(toDateTime, dateTime);

			var unixTime = dateTime.ToUnixTimeMs();
			var fromUnixTime = DateTimeExtensions.FromUnixTimeMs(unixTime);
			AssertDatesAreEqual(fromUnixTime, dateTime);
		}
        public static void WriteWcfJsonDate(TextWriter writer, DateTime dateTime)
        {
            if (JsConfig.AssumeUtc && dateTime.Kind == DateTimeKind.Unspecified)
            {
                dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
            }

            if (JsConfig.DateHandler == JsonDateHandler.ISO8601)
            {
                writer.Write(dateTime.ToString("o", CultureInfo.InvariantCulture));
                return;
            }

            var timestamp = dateTime.ToUnixTimeMs();
            string offset = null;
            if (dateTime.Kind != DateTimeKind.Utc)
            {
                if (JsConfig.DateHandler == JsonDateHandler.TimestampOffset && dateTime.Kind == DateTimeKind.Unspecified)
                    offset = UnspecifiedOffset;
                else
                    offset = LocalTimeZone.GetUtcOffset(dateTime).ToTimeOffsetString();
            }

            writer.Write(EscapedWcfJsonPrefix);
            writer.Write(timestamp);
            if (offset != null)
            {
                writer.Write(offset);
            }
            writer.Write(EscapedWcfJsonSuffix);
        }