Skip to content

Instantly share code, notes, and snippets.

@wayne-o
Created March 17, 2012 20:07
Show Gist options
  • Save wayne-o/2064852 to your computer and use it in GitHub Desktop.
Save wayne-o/2064852 to your computer and use it in GitHub Desktop.
ZonedDateTimeTests
using System;
using Newtonsoft.Json;
using NUnit.Framework;
using NodaTime.Serialization.JsonNet;
namespace NodaTime.Serialization.Test.JsonNet
{
[TestFixture]
public class ZonedDateTimeTests
{
const string expectedJson = "\"{\\\"LocalDateTime\\\":\\\"\\\\\\\"2012-01-01T19:04:05\\\\\\\"\\\",\\\"Offset\\\":\\\"-288000000000\\\",\\\"Zone\\\":\\\"America/Los_Angeles\\\"}\"";
[Test]
public void JsonNet_Can_Serialize_ZonedDateTime()
{
/* Arrange */
var instant = Instant.FromUtc(2012, 1, 2, 3, 4, 5);
const string americaLosAngeles = "America/Los_Angeles";
var dateTimeZone = DateTimeZone.ForId(americaLosAngeles);
var zoneDateTime = new ZonedDateTime(instant, dateTimeZone);
/* Act */
var json = JsonConvert.SerializeObject(zoneDateTime, new NodaZoneDateTimeConverter());
/* Assert */
Assert.AreEqual(expectedJson, json);
}
[Test]
public void JsonNet_Can_Deserialize_ZonedDateTime()
{
/* Arrange */
/* Act */
var zoneDateTime = JsonConvert.DeserializeObject<ZonedDateTime>(expectedJson, new NodaZoneDateTimeConverter());
/* Assert */
var instant = Instant.FromUtc(2012, 1, 2, 3, 4, 5);
const string americaLosAngeles = "America/Los_Angeles";
var dateTimeZone = DateTimeZone.ForId(americaLosAngeles);
var expectedLocalDateTime = new ZonedDateTime(instant, dateTimeZone);
Assert.AreEqual(expectedLocalDateTime, zoneDateTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment